Python projects now support routing rules
Mirrored from Vercel — AI for archival readability. Support the source by reading on the original site.
Python projects can now use routing rules to set response headers or rewrite requests to internal paths, including apps built with FastAPI, Django, and Flask.
The Vercel CDN evaluates rules before requests reach your application, so changes apply without a new deployment.
For example, this FastAPI app serves a /new route:
from fastapi import FastAPI
app = FastAPI()
@app.get("/new")def new_route() -> dict[str, str]: return {"message": "New route!"}Defines the /new route the rewrite below targets
To send requests for /old to that route, create a rewrite from the CDN tab in your project dashboard or with the Vercel CLI:
# Add a rewrite (staged automatically)vercel routes add --ai "Rewrite /old to /new" --yes
# Review staged changesvercel routes list --diff
# Publish to productionvercel routes publishCreating and publishing a rewrite with the Vercel CLI
Published rules take effect immediately across all regions, and you can roll back to a previous version from the History tab.
You can also manage rules from Python with the vercel SDK, which wraps the Vercel REST API. add_route stages the rule, and update_route_version publishes it:
from vercel.client import Vercelfrom vercel.project_routes import RewriteRoute
vercel = Vercel(access_token="...")
added = vercel.project_routes.add_route( project_id="prj_123", route=RewriteRoute(name="Rewrite /old to /new", source="/old", destination="/new"),Stages and publishes the same rewrite from Python
Rewrites can also be defined in vercel.json:
Rewrites /old to /new at the deployment level
Project-level rules live outside your deployment and are evaluated before its routes, so a published rule takes precedence over a vercel.json rewrite for the same path. Each surface fits a different workflow:
How you define it | Takes effect | Best for |
|---|---|---|
CDN tab in the dashboard | On publish | Editing and testing rules visually |
Vercel CLI | On publish | One-off changes from the terminal |
| On publish | Automation and CI/CD |
| On each deployment | Rewrites that ship with the application code |
Update Vercel CLI to the latest version and read the routing documentation to get started.
Discussion (0)
Sign in to join the discussion. Free account, 30 seconds — email code or GitHub.
Sign in →No comments yet. Sign in and be the first to say something.