DEV Community

Thesius Code
Thesius Code

Posted on • Originally published at datanest-stores.pages.dev

FastAPI Starter Template

FastAPI Starter Template

Production-ready FastAPI boilerplate with async SQLAlchemy, JWT auth, and Docker — ship your API in hours, not weeks.

Python 3.11+
FastAPI
License: MIT


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
Enter fullscreen mode Exit fullscreen mode

Getting Started

1. Clone and configure

cp .env.example .env
# Edit .env with your database credentials
Enter fullscreen mode Exit fullscreen mode

2. Run with Docker (recommended)

docker compose -f docker/docker-compose.yml up -d
Enter fullscreen mode Exit fullscreen mode

3. Or run locally

pip install -e ".[dev]"
alembic upgrade head
uvicorn app.main:app --reload
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

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.) │
   └──────────┘  └──────────┘  └─────────┘
Enter fullscreen mode Exit fullscreen mode

Running Tests

pytest tests/ -v --tb=short
Enter fullscreen mode Exit fullscreen mode

Related Products


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.

Get the Full Kit →

Or grab the entire Python Developer Pro bundle (14 products) for $159 — save 30%.

Get the Complete Bundle →


Related Articles

Top comments (0)