Deploy a FastAPI App
This tutorial walks you through deploying a FastAPI application on Temps with PostgreSQL, Uvicorn, and automatic HTTPS. By the end, you will have a production FastAPI app with automatic deployments on every push.
What you will build
- A FastAPI app deployed from a Git repository
- PostgreSQL database provisioned and connected
- Uvicorn as the production ASGI server
- Automatic HTTPS and deployment on every push
Time required: 10 minutes.
Prerequisites:
- A running Temps instance (install guide)
- A FastAPI app in a Git repository
- (Optional) A custom domain
Step 1: Prepare your app
Add production dependencies
Make sure your requirements.txt includes a production ASGI server:
requirements.txt
fastapi>=0.115.0
uvicorn[standard]>=0.32.0
sqlalchemy>=2.0
psycopg2-binary>=2.9
alembic>=1.14
python-dotenv>=1.0
Configure database connection
Use environment variables for your database connection:
app/database.py
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, DeclarativeBase
DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///./app.db')
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
class Base(DeclarativeBase):
pass
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
Add a health check endpoint
app/main.py
from fastapi import FastAPI
app = FastAPI()
@app.get('/health')
def health():
return {'status': 'ok'}
# ... your existing routes
Create a Dockerfile
Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
Step 2: Create a project
- Open the Temps dashboard
- Click New Project
- Enter a name (e.g.,
my-fastapi-app) - Click Create
Or use the CLI:
bunx @temps-sdk/cli projects create -n "my-fastapi-app" -d "FastAPI application"
Step 3: Provision a database
Create a managed PostgreSQL database:
- Go to your project → Services
- Click Add Service → PostgreSQL
- Temps creates the database and sets
DATABASE_URLautomatically
Or via CLI:
bunx @temps-sdk/cli services create -p my-fastapi-app -t postgres -n "fastapi-db"
The DATABASE_URL environment variable is automatically injected into your app's environment.
Step 4: Connect your repository
- Go to your project → Settings → Git
- Connect your GitHub, GitLab, Bitbucket, or Gitea account
- Select your repository and branch
Or via CLI:
bunx @temps-sdk/cli projects git -p my-fastapi-app --owner yourorg --repo my-fastapi-app --branch main --preset python -y
Step 5: Set environment variables
Go to your project → Environment Variables and add:
| Variable | Value | Notes |
|---|---|---|
SECRET_KEY | (generate a random key) | For JWT or session signing |
ENVIRONMENT | production | Controls debug mode |
DATABASE_URL is set automatically by the managed PostgreSQL service.
Step 6: Deploy
Push to your connected branch, or deploy manually:
bunx @temps-sdk/cli deploy my-fastapi-app -b main -e production -y
Temps will:
- Detect your Python project
- Build the Docker image
- Start Uvicorn on the configured port
- Run health checks before routing traffic
- Issue an SSL certificate for your domain
Step 7: Run migrations
After your first deploy, run Alembic migrations:
bunx @temps-sdk/cli exec my-fastapi-app -- alembic upgrade head
For subsequent deployments, add migrations to your start command:
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4"]
What to do next
- Add a custom domain — point your domain to this deployment
- Set up preview deployments — get a preview URL for every pull request
- Add monitoring — uptime checks and alerts
- Set up backups — automated database backups