Skip to main content
All guidesGuide

Step-by-step: deploy your FastAPI AI backend to Railway, Render, or Fly.io.

Getting a FastAPI backend running on localhost is easy. Getting it deployed to production with a managed database, environment variables, HTTPS, and proper process management is where most developers get stuck. This guide covers deploying to the three most developer-friendly platforms.

1

Prepare your Dockerfile

Create a production Dockerfile with multi-stage builds, proper Python path setup, and a non-root user for security.

step-1.py
FROM python:3.12-slim AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

FROM base AS production
COPY . .
RUN adduser --disabled-password --no-create-home appuser
USER appuser
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2"]
2

Deploy to Railway

Railway auto-detects your Dockerfile, provisions a PostgreSQL database, and gives you a public URL. Set environment variables in the Railway dashboard.

step-2.py
# Install Railway CLI
npm install -g @railway/cli

# Login and initialize
railway login
railway init

# Add PostgreSQL
railway add --plugin postgresql

# Deploy
railway up

# Set environment variables
railway variables set OPENAI_API_KEY=sk-...
railway variables set JWT_SECRET=your-secret-here
railway variables set STRIPE_SECRET_KEY=sk_live_...
3

Deploy to Render

Create a render.yaml blueprint that defines your web service and database together. Render handles builds, deploys, and managed PostgreSQL.

step-3.py
# render.yaml
services:
  - type: web
    name: fastapi-ai-api
    runtime: docker
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: api-db
          property: connectionString
      - key: OPENAI_API_KEY
        sync: false
    healthCheckPath: /health

databases:
  - name: api-db
    plan: starter
    databaseName: api_db
4

Deploy to Fly.io

Fly.io deploys your Docker container to edge locations worldwide. Create a fly.toml config and deploy with a single command.

step-4.py
# Install Fly CLI and launch
fly launch --name my-ai-api --region iad

# Create a PostgreSQL cluster
fly postgres create --name my-ai-db

# Attach the database
fly postgres attach my-ai-db

# Set secrets
fly secrets set OPENAI_API_KEY=sk-...
fly secrets set JWT_SECRET=your-secret-here

# Deploy
fly deploy
5

Run database migrations on deploy

Use Alembic to run migrations automatically during deployment. Add a release command or pre-start script that applies pending migrations.

step-5.py
# In your Dockerfile or deploy hook
CMD alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000

# Or as a Railway/Render release command:
# alembic upgrade head

Skip the setup, start building

Everything in this guide is already implemented and configured in FastAPI AI Kit. Clone the repo and start building your product features immediately.

Ready to ship your AI backend this weekend?

Join developers who skipped weeks of boilerplate and went straight to building.

Read the docs
No subscriptions · One-time payment · Lifetime updates