You can now write routing middleware in Python for FastAPI, Flask, Django, and other Python frameworks. It runs before requests reach your app, and can rewrite, redirect, or answer them.
To set it up, point the proxy entrypoint in vercel.toml at your proxy:
[proxy]entrypoint = "proxy:proxy"proxy:proxy refers to the proxy attribute of proxy.py.
Your middleware is an ASGI or WSGI app that runs separately from your main app, with only the packages in the vercel.proxy dependency group of your pyproject.toml. Add the packages your middleware imports to that group:
uv add --group vercel.proxy vercel-proxyThe vercel-proxy package provides utilities for writing routing middleware on Vercel, including a Proxy app that routes requests by path:
from urllib.parse import quotefrom vercel.proxy import ContinueResponse, Proxy, RedirectResponse, Request, Responseproxy = Proxy()
@proxy.route("/old-blog/{slug}")def old_blog(request: Request) -> Response: slug = quote(request.path_params["slug"]) return RedirectResponse(f"/blog/{slug}", status_code=308)
@proxy.route("/api/{path:path}")def api(request: Request) -> Response: return ContinueResponse(request_headers={"x-api-version": "2"})Requests that match no route continue to your app unchanged.
For more information, see the Routing Middleware Python reference.
Reference Architecture
Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.
- Agent stack: Build and operate agents with models, tools, and durable workflows.
- Web applications: Choose rendering, data, authentication, and caching approaches.
- Backends: Choose a runtime, connect services, and run background work.
Build with prompts
Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.