Vercel now supports lifespan events for FastAPI apps. This allows you to define logic that can execute on startup and graceful shutdown—such as managing database connections or flushing external logs.
from contextlib import asynccontextmanager
from fastapi import FastAPI
async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)
Deploy FastAPI on Vercel or visit the FastAPI on Vercel documentation.