Deploy a FastAPI app on Vercel
Deploy a FastAPI app to Vercel with the Python runtime and Vercel Functions.
Vercel looks for a FastAPI instance named app at supported entrypoints in
your repository.
Create a FastAPI app or use an existing one:
Initialize a new FastAPI project with the Vercel CLI init command:
vc init fastapiThis clones the FastAPI example repository in a directory called fastapi.
To run a FastAPI application on Vercel, define an app instance that initializes FastAPI at a supported entrypoint:
app.py,index.py,server.py,main.py,wsgi.py, orasgi.py- the same filenames inside
src/,app/, orapi/
For example:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Python": "on Vercel"}To point Vercel to a FastAPI app in a custom module, set tool.vercel.entrypoint in pyproject.toml:
[tool.vercel]
entrypoint = "backend.server:app"The tool.vercel.entrypoint value tells Vercel to look for a FastAPI instance named app in ./backend/server.py.
The build property in [tool.vercel.scripts] defines the Build Command for FastAPI deployments. It runs after dependencies are installed and before your application is deployed.
[tool.vercel.scripts]
build = "python build.py"For example:
def main():
print("Running build command...")
with open("build.txt", "w") as f:
f.write("BUILD_COMMAND")
if __name__ == "__main__":
main()If you define a Build
Command in
vercel.json or in the Project Settings dashboard, it takes precedence over a
build script in pyproject.toml.
Use vercel dev to run your application locally.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
vercel devDeploy the project by connecting your Git repository or by using the Vercel CLI:
vc deployTo serve static assets, place them in the public/** directory. Vercel serves
those files from the CDN with default headers
unless you override them in vercel.json.
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
app = FastAPI()
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
# /vercel.svg is automatically served when included in the public/** directory.
return RedirectResponse("/vercel.svg", status_code=307)app.mount("/public", ...) is not needed and should not be used.You can use FastAPI lifespan events to manage startup and shutdown logic, such as initializing and closing database connections.
from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup logic
print("Starting up...")
await startup_tasks()
yield
# Shutdown logic
await cleanup_tasks()
app = FastAPI(lifespan=lifespan)Cleanup logic during shutdown is limited to a maximum of 500ms after receiving the SIGTERM signal. Logs printed during the shutdown step will not appear in the Vercel dashboard.
When you deploy a FastAPI app to Vercel, it becomes a single Vercel Function. Vercel uses Fluid compute by default, so the function scales with traffic.
To configure that function, add an entry to the functions
object in vercel.json keyed by your
resolved entrypoint file. For example, to let an app defined in app/main.py
run for up to 60 seconds, set maxDuration:
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"functions": {
"app/main.py": {
"maxDuration": 60
}
}
}For more options, see Configuring
functions and the functions
property.
All Vercel Functions limitations apply to FastAPI applications, including:
- Application size: The FastAPI application becomes a single bundle, which has a standard bundle size limit of 500MB. Large Functions support Python bundles up to 5GB on Fluid compute when enabled (public beta). Our bundling process removes
__pycache__and.pycfiles from the deployment's bundle to reduce size, but does not perform application bundling.
For more about deploying FastAPI on Vercel, see:
Was this helpful?