FastAPI Starter Template
Production-ready FastAPI boilerplate with async SQLAlchemy, JWT auth, and Docker — ship your API in hours, not weeks.
What You Get
- Async-first architecture — SQLAlchemy 2.0 async engine, async session management
- JWT authentication — Register, login, refresh tokens, role-based access control
- User management — Full CRUD with pagination, soft-delete, and admin routes
- Rate limiting — In-memory sliding window middleware (swap for Redis in production)
- Database migrations — Alembic with async support, auto-generate from models
- Docker ready — Multi-stage Dockerfile, docker-compose with PostgreSQL & Redis
- Test suite — AsyncClient fixtures, auth helpers, database isolation
- Clean project structure — Separation of concerns with dependency injection
File Tree
fastapi-starter-template/
├── app/
│ ├── main.py # FastAPI app entry point
│ ├── config.py # Pydantic settings from env vars
│ ├── models/
│ │ ├── base.py # SQLAlchemy Base + mixins
│ │ └── user.py # User model
│ ├── schemas/
│ │ └── user.py # Pydantic request/response schemas
│ ├── api/
│ │ ├── deps.py # Shared dependencies
│ │ └── v1/
│ │ ├── auth.py # Auth routes (register/login/refresh)
│ │ └── users.py # User CRUD routes
│ ├── core/
│ │ ├── security.py # JWT + password hashing
│ │ └── database.py # Async engine + session
│ └── middleware/
│ └── rate_limit.py # Rate limiting middleware
├── tests/
│ ├── conftest.py # Test fixtures
│ └── test_auth.py # Auth endpoint tests
├── docker/
│ ├── Dockerfile # Multi-stage build
│ └── docker-compose.yml # Full stack
├── alembic/
│ └── env.py # Async migration env
└── pyproject.toml # Project configuration
Getting Started
1. Clone and configure
cp .env.example .env
# Edit .env with your database credentials
2. Run with Docker (recommended)
docker compose -f docker/docker-compose.yml up -d
3. Or run locally
pip install -e ".[dev]"
alembic upgrade head
uvicorn app.main:app --reload
4. Create your first user
import httpx
async with httpx.AsyncClient(base_url="http://localhost:8000") as client:
# Register
resp = await client.post("/api/v1/auth/register", json={
"email": "dev@example.com",
"password": "supersecret123"
})
print(resp.json()) # {"id": 1, "email": "dev@example.com", ...}
# Login
resp = await client.post("/api/v1/auth/login", json={
"email": "dev@example.com",
"password": "supersecret123"
})
token = resp.json()["access_token"]
# Access protected route
resp = await client.get("/api/v1/auth/me", headers={
"Authorization": f"Bearer {token}"
})
print(resp.json())
Requirements
- Python 3.11+
- PostgreSQL 15+
- Redis 7+ (optional, for production rate limiting)
Architecture
┌─────────────────────────────────────────────────┐
│ Client │
└──────────────────────┬──────────────────────────┘
│ HTTP
┌──────────────────────▼──────────────────────────┐
│ FastAPI Application │
│ ┌──────────┐ ┌───────────┐ ┌──────────────┐ │
│ │ CORS │→ │Rate Limit │→ │ Exception │ │
│ │Middleware │ │Middleware │ │ Handlers │ │
│ └──────────┘ └───────────┘ └──────────────┘ │
│ ┌──────────────────────────────────────────┐ │
│ │ API Router (v1) │ │
│ │ /auth/register /auth/login /users/ │ │
│ └──────────────────┬───────────────────────┘ │
│ ┌──────────────────▼───────────────────────┐ │
│ │ Dependencies Layer │ │
│ │ get_db() get_current_user() require_ │ │
│ │ admin() │ │
│ └──────────────────┬───────────────────────┘ │
│ ┌──────────────────▼───────────────────────┐ │
│ │ Core Services │ │
│ │ security.py database.py │ │
│ └──────────────────┬───────────────────────┘ │
└──────────────────────┼──────────────────────────┘
│
┌─────────────┼─────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌─────────┐
│PostgreSQL│ │ Redis │ │ Alembic │
│ (data) │ │ (cache) │ │ (migr.) │
└──────────┘ └──────────┘ └─────────┘
Running Tests
pytest tests/ -v --tb=short
Related Products
- Django SaaS Boilerplate — Multi-tenant SaaS starter with Django
- OAuth & Auth Library — OAuth 2.0 and JWT authentication library
- Python Testing Toolkit — Pytest fixtures, factories, and test patterns
This is 1 of 14 resources in the Python Developer Pro toolkit. Get the complete [FastAPI Starter Template] with all files, templates, and documentation for $39.
Or grab the entire Python Developer Pro bundle (14 products) for $159 — save 30%.
Top comments (0)