FastAPI on Vercel
FastAPI is a modern, high-performance, web framework for building APIs with Python based on standard Python type hints. You can deploy a FastAPI app to Vercel with zero configuration.
You can quickly deploy a FastAPI application to Vercel by creating a FastAPI app or using an existing one:
Get started by initializing a new FastAPI project using Vercel CLI init command:
vc init fastapi
This will clone the FastAPI example repository in a directory called fastapi
.
To run a FastAPI application on Vercel, define an app
instance that initializes FastAPI
at any of the following entrypoints:
app.py
index.py
server.py
src/app.py
src/index.py
src/server.py
app/app.py
app/index.py
app/server.py
For example:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Python": "on Vercel"}
Use vercel dev
to run your application locally.
vercel dev
To deploy, connect your Git repository or use Vercel CLI:
vc deploy
To serve static assets:
-
Place them in the
public/**
directory. They will be served as a part of our Edge Network using default headers unless otherwise specified invercel.json
. -
Use
app.mount()
to mount thepublic
directory.
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, HTMLResponse
app = FastAPI()
app.mount("/public", StaticFiles(directory="public"), name="public")
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
return FileResponse("public/vercel.svg")
When you deploy a FastAPI app to Vercel, the application becomes a single Vercel Function and uses Fluid compute by default. This means your FastAPI app will automatically scale up and down based on traffic.
All Vercel Functions limitations apply to FastAPI applications, including:
- Application size: The FastAPI application becomes a single bundle, which must fit within the 250MB limit of Vercel Functions. Our bundling process removes
__pycache__
and.pyc
files from the deployment's bundle to reduce size, but does not perform application bundling.
Learn more about deploying FastAPI projects on Vercel with the following resources:
Was this helpful?