> ## Documentation Index
> Fetch the complete documentation index at: https://docs.slipway.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy a FastAPI app

> Deploy a FastAPI service with Uvicorn and Postgres.

FastAPI runs on slipway as a `web` service on the Python runtime.

## Prerequisites

A FastAPI app in a GitHub or GitLab repository, with `uvicorn` in your requirements.

## Add slipway.yaml

```yaml slipway.yaml theme={"system"}
version: 1

databases:
  - name: db
    engine: postgres
    version: "18"
    diskSizeGB: 10

services:
  - name: api
    runtime: python
    runtimeVersion: "3.12"
    buildCommand: pip install -r requirements.txt
    command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
    dependsOn: [db]
    ports:
      - port: 8000
        public: true
    healthCheckPath: /healthz
    env:
      DATABASE_URL: ${service.db.url}
```

Bind to `0.0.0.0`. Uvicorn defaults to `127.0.0.1`, which is unreachable from outside the
container.

## Add a health endpoint

```python app/main.py theme={"system"}
from fastapi import FastAPI

app = FastAPI()

@app.get("/healthz")
def healthz():
    return {"status": "ok"}
```

Keep it cheap. A check that queries the database fails the deploy whenever the database is
slow.

## Run migrations

```yaml slipway.yaml theme={"system"}
    preDeploy: ["alembic", "upgrade", "head"]
```

## Workers

For a Uvicorn worker count, prefer running more replicas over more processes in one
container, so the platform can balance them:

```yaml slipway.yaml theme={"system"}
    replicas: 3
```

Or autoscale:

```yaml slipway.yaml theme={"system"}
    scaling:
      min: 2
      max: 10
      targetCPUPercent: 70
```

See [Scaling](/services/scaling).

## Add a background worker

```yaml slipway.yaml theme={"system"}
databases:
  - name: queue
    engine: redis

services:
  - name: worker
    type: worker
    runtime: python
    runtimeVersion: "3.12"
    buildCommand: pip install -r requirements.txt
    command: ["celery", "-A", "app.worker", "worker", "-l", "info"]
    env:
      DATABASE_URL: ${service.db.url}
      CELERY_BROKER_URL: ${service.queue.url}
```

## Next steps

> **[Secrets](/slipway-yaml/secrets-and-variables)**
>
> Keep API keys out of the repository.

> **[PR previews](/previews/pr-previews)**
>
> Give every pull request its own copy.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.