PreviewDrop for FastAPI
Deploy previews for FastAPI apps
Push a branch, get a live preview URL — including interactive /docs. PreviewDrop builds your FastAPI container and posts the link to the pull request in under 60 seconds.
TL;DR
Add a Dockerfile, connect your repo, push. PreviewDrop builds the image and posts a TLS-terminated PR preview URL with Uvicorn running on the other end. The interactive
/docs and /redoc endpoints work out of the box — useful for sharing API previews with non-engineers.Dockerfile
Drop this at the root of your repo. PreviewDrop picks it up on the next push and builds it into a container behind a TLS-terminated subdomain.
DockerfileFROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8000 CMD sh -c "uvicorn main:app --host 0.0.0.0 --port ${PORT:-8000}"
Environment variables
Set these in the Variables tab of your PreviewDrop project. They're encrypted at rest and injected into every preview container at start.
| Variable | Example value | Note |
|---|---|---|
| DATABASE_URL | postgresql+asyncpg://user:pass@host:5432/db | Used by SQLAlchemy / databases. Omit if the preview does not need a real DB. |
| SECRET_KEY | replace-me-32-char-random-string | For JWT / session signing if your app uses it. Any long random string works for previews. |
| ENVIRONMENT | preview | Optional — lets your app detect it is running as a preview and adjust behaviour. |
Common gotchas
Bind to 0.0.0.0, not 127.0.0.1
Uvicorn defaults to
127.0.0.1 which is only reachable inside the container. The Dockerfile CMD uses --host 0.0.0.0 so the port is reachable from outside. Without this the preview URL returns a connection refused error.Run Alembic migrations at container start
If you use Alembic, run migrations before Uvicorn starts. Replace the CMD with:
sh -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port $PORT". Preview containers start fresh each build — migrations don't persist unless you point at an external database.CORS origins change per preview
If a separate frontend calls your API, the preview URL changes on every push. Use a permissive CORS policy for preview containers (e.g. allow
*.previews.previewdrop.dev), or pass the frontend's preview URL as an env var via per-branch overrides in PreviewDrop project settings.Replace main:app if your entrypoint is different
The CMD uses
main:app — the app FastAPI instance in main.py. Adjust to match your project structure: app.main:app for a package layout, src.api:app etc.Why not Vercel or Netlify?
Vercel's Python runtime runs FastAPI via ASGI adapters, but with a 10-second execution limit per request. Background tasks, WebSocket endpoints, and long-running streaming responses don't fit. Netlify has no Python runtime at all. PreviewDrop runs your Dockerfile: Uvicorn as a real persistent process — WebSockets, background tasks, streaming responses, and the interactive
/docs endpoint all work in the PR deploy preview exactly as they do in production.Preview your FastAPI app in 5 minutes
Connect a repo, push a branch, get a URL. No credit card, no trial clock.
Start free