Skip to main content

Deployment

FastAPI AI Kit ships with a production Dockerfile and step-by-step guides for the most common hosting platforms. You can also export to any Docker-capable host.

Before you deploy

  1. Generate a production SECRET_KEY: openssl rand -hex 32
  2. Provision a managed PostgreSQL database and note the connection string
  3. Provision a managed Redis instance (Upstash is free-tier friendly)
  4. Set all required environment variables in your host's dashboard

Railway

Railway is the fastest path to production.

# Install Railway CLI
npm install -g @railway/cli

# Authenticate
railway login

# Link a new project
railway init

# Provision Postgres and Redis add-ons via the Railway dashboard,
# then connect their env vars automatically

# Deploy
railway up

Railway reads your Dockerfile automatically. Alembic migrations run on startup via the CMD in the Dockerfile.

Render

  1. Create a new Web Service on Render, pointing at your repo.
  2. Set Build Command: pip install -r requirements.txt
  3. Set Start Command: uvicorn app.main:app --host 0.0.0.0 --port $PORT
  4. Add a PostgreSQL and Redis service from the Render dashboard.
  5. Inject the env vars from the Render dashboard.

Tip: Use Render's pre-deploy commands to run alembic upgrade head before traffic is served.

Fly.io

# Install flyctl
brew install flyctl

# Launch (auto-detects Dockerfile)
fly launch

# Scale to at least 256 MB memory for LLM workloads
fly scale memory 512

# Set secrets
fly secrets set SECRET_KEY=... OPENAI_API_KEY=... DATABASE_URL=...

# Deploy
fly deploy

Fly gives you edge regions and built-in private networking between your API and Postgres/Redis.

VPS (any Docker host)

# On your server
git clone https://github.com/fastapikit/starter /opt/my-api
cd /opt/my-api
cp .env.example .env   # fill in production values

# Start everything
docker-compose -f docker-compose.prod.yml up -d

# Run migrations
docker-compose exec api alembic upgrade head

The docker-compose.prod.yml file removes development mounts and sets restart: always on all services.

Health checks

The /healthz endpoint checks database and Redis connectivity:

{ "status": "ok", "db": "connected", "redis": "connected", "version": "1.2.0" }

Configure your host's health check to hit /healthz every 30 seconds.

Scaling

The API is stateless — horizontal scaling works out of the box. Run multiple API replicas behind a load balancer; all state lives in Postgres and Redis. Background workers (Celery) scale independently from the API.