Does FastAPI Work With Vercel?
FastAPI can run on Vercel, but only through serverless functions—Vercel is optimized for frontend frameworks, not Python backends.
Quick Facts
How FastAPI Works With Vercel
FastAPI works on Vercel by deploying as Python serverless functions using Vercel's `/api` directory. You create a `vercel.json` configuration and structure your FastAPI app as a function handler that Vercel invokes for each request. The framework automatically handles routing, dependency injection, and request/response serialization within the serverless execution model. However, this approach has significant trade-offs: each request spins up a new Python runtime (cold starts add 1-5 seconds), there's a 10-second execution timeout on Hobby plans, and you lose persistent connections. Vercel is fundamentally designed for Next.js, static sites, and lightweight edge functions, not Python web frameworks. If you need a persistent Python backend, dedicated platforms like Railway, Render, or AWS Lambda offer better performance and developer experience. Vercel shines when combining a Next.js frontend with lightweight FastAPI endpoints for specific API routes, not as a primary backend host.
Best Use Cases
FastAPI on Vercel Serverless Function
pip install fastapi# api/index.py
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/api/hello")
async def hello(name: str = "World"):
return JSONResponse({"message": f"Hello, {name}!"})
@app.get("/api/health")
async def health():
return JSONResponse({"status": "ok"})
# Vercel handler - called by serverless runtime
from mangum import Mangum
handler = Mangum(app)Known Issues & Gotchas
Cold starts make FastAPI slow; first request to an endpoint takes 3-5 seconds
Fix: Accept cold starts as inherent to serverless, or use Vercel Pro's 'Warm Functions' feature. Consider moving to Railway/Render for production APIs.
10-second execution timeout on free/Hobby plans will kill long-running requests
Fix: Upgrade to Pro ($20/month) for 60-second timeout, or redesign endpoints to complete faster. Offload heavy work to background job queues.
No persistent in-memory state; each invocation is stateless and isolated
Fix: Use external databases (PostgreSQL, Redis) instead of in-memory caches. FastAPI's startup events won't work reliably.
WebSocket support is limited on Vercel serverless; long-polling required
Fix: Avoid WebSockets in FastAPI. Use polling endpoints or move WebSocket features to a dedicated server.
Alternatives
- •Next.js API Routes + Node.js backend (native Vercel support, zero cold starts, simpler DX)
- •FastAPI on Railway/Render (true Python backend with persistent processes, better for production APIs)
- •AWS Lambda + API Gateway (more control, better for enterprise, steeper learning curve)
Resources
Related Compatibility Guides
Explore more compatibility guides