Skip to main content

FastAPI Auth Starter

A clean architecture FastAPI project starter with authentication (WorkOS), PostgreSQL and Alembic migrations.

Quick Start

Create a new project from this template:

Install the package (globally or in a separate environment):

uv pip install fastapi-auth-starter

Initialize a new project in a new directory:

fastapi-auth-starter init my-project

Or initialize in the current directory:

fastapi-auth-starter init .

Note: The fastapi-auth-starter CLI tool is a scaffolding utility, not a runtime dependency. If you install it in your project's virtual environment and then run uv sync, it will be removed (this is expected behavior). Install it globally to avoid this.

Project Structure

fastapi_auth_starter/
├── app/
│   ├── __init__.py
│   ├── main.py              # FastAPI application entry point
│   ├── api/
│   │   └── v1/
│   │       ├── api.py       # API router aggregation
│   │       └── routes/
│   │           └── health.py  # Health check endpoint
│   ├── core/
│   │   ├── config.py        # Application configuration
│   │   └── database.py      # Database connection and session management
│   ├── models/              # SQLAlchemy database models
│   ├── services/            # Business logic services
│   └── db/                  # Database utilities
├── alembic/                 # Database migration scripts
├── alembic.ini              # Alembic configuration
├── pyproject.toml           # Project dependencies (uv)
└── README.md

Features

  • ✅ Clean architecture with separation of concerns
  • ✅ FastAPI with async SQLAlchemy
  • ✅ WorkOS authentication integration
  • ✅ PostgreSQL database support
  • ✅ Alembic for database migrations
  • ✅ Health check endpoint
  • ✅ Dependency injection with FastAPI
  • ✅ Environment-based configuration

Prerequisites

  • Python 3.12+ (3.13 for local dev, 3.12 for Vercel deployment)
  • uv package manager
  • PostgreSQL database (local or remote)

Setup

1. Install Dependencies

Dependencies are managed with uv. They are automatically installed when you run commands with uv run.

2. Configure Environment Variables

Important: Copy the example environment file and configure your settings:

cp .env.example .env

Then edit .env with your configuration:

# Database Configuration
DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/fastapi_auth

# API Configuration (optional - defaults are fine)
API_V1_PREFIX=/api/v1
PROJECT_NAME=FastAPI Auth Starter
VERSION=0.1.0

Note:

  • The .env file is gitignored and should not be committed
  • The application uses asyncpg for async operations, but Alembic uses psycopg2 for migrations (sync driver)
  • All sensitive configuration should be in .env file, not hardcoded

3. Set Up Database

You can use PostgreSQL from any source - local, cloud providers (AWS RDS, Neon, Supabase, etc.), or managed services.

Option A: Local PostgreSQL

If using a local PostgreSQL instance, create the database:

createdb fastapi_auth

Or using PostgreSQL client:

CREATE DATABASE fastapi_auth;

Option B: Cloud/Managed Database

If using a cloud provider (AWS RDS, Neon, Supabase, Railway, etc.):

  • Create a PostgreSQL database through your provider's dashboard
  • Copy the connection string provided by your provider
  • Update the DATABASE_URL in your .env file with the connection string

Important:

  • The DATABASE_URL in your .env file should point to your database (local or cloud)
  • Ensure the database is accessible from where you're running the application
  • The connection string format should be: postgresql+asyncpg://user:password@host:port/database

4. Run Migrations

Create your initial migration:

# Create initial migration
uv run alembic revision --autogenerate -m "Initial migration"

# Apply migrations
uv run alembic upgrade head

Running the Application

Development Server

uv run uvicorn app.main:app --reload

The API will be available at:

  • API: http://localhost:8000
  • Docs: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

Health Check

Test the health endpoint:

curl http://localhost:8000/api/v1/health

Expected response:

{
  "status": "healthy",
  "message": "Service is running"
}

Development

Adding New Routes

  1. Create a new route file in app/api/v1/routes/
  2. Import and include the router in app/api/v1/api.py

Example:

# app/api/v1/routes/users.py
from fastapi import APIRouter

router = APIRouter(prefix="/users", tags=["users"])

@router.get("/")
async def get_users():
    return {"users": []}

Then add to app/api/v1/api.py:

from app.api.v1.routes import users
api_router.include_router(users.router)

Adding Database Models

  1. Create model in app/models/
  2. Import in app/models/__init__.py
  3. Import in alembic/env.py (for autogenerate)

Example:

# app/models/user.py
from sqlalchemy import Column, Integer, String
from app.core.database import Base

class User(Base):
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True)
    email = Column(String, unique=True, index=True)

Creating Migrations

# Auto-generate migration from model changes
uv run alembic revision --autogenerate -m "Description of changes"

# Create empty migration
uv run alembic revision -m "Description of changes"

Applying Migrations

# Apply all pending migrations
uv run alembic upgrade head

# Rollback one migration
uv run alembic downgrade -1

# Rollback to specific revision
uv run alembic downgrade <revision>

Architecture

Layers

  • API Layer (app/api/): Route handlers, request/response models
  • Service Layer (app/services/): Business logic, domain operations
  • Model Layer (app/models/): SQLAlchemy database models
  • Core Layer (app/core/): Configuration, database setup, utilities

Dependency Injection

FastAPI's dependency injection is used throughout:

  • Database sessions via get_db() dependency
  • Configuration via settings object
  • Custom dependencies in app/core/dependencies.py (create as needed)

Deployment

Vercel Deployment

  1. Install Dev Dependencies Locally:

    uv sync  # Installs all dependencies including dev
    
  2. Set Environment Variables in Vercel:

    • Go to your project settings in Vercel
    • Add DATABASE_URL environment variable with your PostgreSQL connection string
    • Format: postgresql+asyncpg://user:password@host:port/database
  3. Deploy:

    vercel --prod
    

Note:

  • Runtime dependencies don't include psycopg2-binary or alembic (only needed for local migrations)
  • Python 3.12 is used (Vercel doesn't support 3.13 yet)
  • Make sure to run migrations on your database before deploying

References

License

MIT

Metadata

Release files for fastapi-auth-starter 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-auth-starter 1.0.5
File Size Uploaded
fastapi_auth_starter-1.0.5.tar.gz 119.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-auth-starter 1.0.5
File Interpreter ABI Platform
fastapi_auth_starter-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 172.5 kB

Release files / fastapi_auth_starter-1.0.5.tar.gz

Download URL fastapi_auth_starter-1.0.5.tar.gz
Size 119.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4bb7b1a871adaf715297c20f3fdd2293cc36e4b3916105f7724d2959fe307e93
BLAKE2b-256 checksum
How to use checksums
fe1e6894c68bf5efbf20b07e373e61494e6d0a1d410f6f0338f1408af8223d18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2025.

Transparency log

Release files / fastapi_auth_starter-1.0.5-py3-none-any.whl

Download URL fastapi_auth_starter-1.0.5-py3-none-any.whl
Size 52.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
775fe44916bc3429b08daf23bde0244c612e008241b9fa8852dcb693bb265f69
BLAKE2b-256 checksum
How to use checksums
d244794eb8dce296333f1469e856ab1777ee056273b294af2e965cd5ffe30426
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page