name, description
| name |
description |
| python-fastapi |
FastAPI REST API template principles. SQLAlchemy, Pydantic, Alembic. |
FastAPI API Template
Versions reflect the latest stable line verified 2026-05. Pin to the current stable when scaffolding.
Tech Stack
| Component |
Technology |
| Framework |
FastAPI |
| Language |
Python 3.12+ (current stable 3.14) |
| ORM |
SQLAlchemy 2.0 (async) |
| Validation |
Pydantic v2 |
| Migrations |
Alembic |
| Auth |
JWT + passlib |
Directory Structure
Domain/module layout (scales better than file-type for non-trivial apps). Each domain owns its router, schemas, models, service.
Key Concepts
| Concept |
Description |
| Domain modules |
Each feature folder owns router + schemas + models + service |
| Async |
async/await throughout (AsyncSession, async_sessionmaker) |
| Dependency Injection |
FastAPI Depends (validation, auth, DB session) |
| Pydantic v2 |
Validation + serialization |
| SQLAlchemy 2.0 |
Async sessions |
API Structure
| Layer |
Responsibility |
| Routers |
HTTP handling |
| Dependencies |
Auth, validation |
| Services |
Business logic |
| Models |
Database entities |
| Schemas |
Request/response |
Setup Steps
python -m venv venv
source venv/bin/activate
pip install fastapi uvicorn "sqlalchemy[asyncio]" alembic pydantic pydantic-settings
- Create
.env
alembic upgrade head
uvicorn src.main:app --reload
Best Practices
- Use async everywhere (AsyncSession, async dependencies; wrap sync SDKs in
run_in_threadpool)
- Per-module
BaseSettings over one global config
- Pydantic v2 for validation
- SQLAlchemy 2.0 async sessions
- Alembic migrations: static, reversible, descriptive slugs
- pytest-asyncio for tests; use
dependency_overrides to mock