Async-first FastAPI starter for services that need structured configuration, pooled I/O, consistent error payloads, and predictable local setup.
This repository is a base template, not a finished product. It gives you the application shell, config system, HTTP and database plumbing, logging, route discovery, and test scaffolding. Domain models, migrations, and real authentication are still yours to add.
- FastAPI app with lifespan-based startup and shutdown
- Async SQLAlchemy engine and session management
- Shared
httpx.AsyncClientpool - Route auto-discovery from
app/routes - Centralized exception handlers with JSON error responses
- Pydantic settings and secret-backed configuration helpers
- Structured logging with request correlation IDs
- Docker, Docker Compose, Makefile, Ruff, MyPy, and pytest setup
fastapi-backend/
├── app/
│ ├── core/ # config, auth, DB, logging, route discovery
│ ├── env_files/ # shipped env files and local secret template
│ ├── models/ # shared SQLAlchemy base and mixins
│ ├── repository/ # repository implementations
│ ├── routes/ # auto-discovered APIRouter modules
│ ├── schemas/ # shared schema helpers
│ ├── main.py # FastAPI entrypoint
│ └── main_config.py # Pydantic settings models and cached instances
├── tests/
├── Dockerfile
├── docker-compose.yml
├── Makefile
└── pyproject.toml
- Python 3.11+
uv
uv sync --extra devAlternative:
make devcp app/env_files/.secrets_local.example app/env_files/.secrets_localUpdate app/env_files/.secrets_local with your local credentials.
docker compose up -d postgresmake runThe application starts through python -m app.main so the custom logging setup stays active.
GET /api/GET /api/healthGET /api/health/readyGET /api/auth-examples/meGET /api/auth-examples/me/activeGET /api/auth-examples/admin-onlyGET /api/auth-examples/editor-or-admin
OpenAPI docs are exposed at http://localhost:8000/docs and http://localhost:8000/redoc by default.
The auth example routes are wired for dependency composition only. They intentionally return 401 until you replace app/core/auth.py with real token validation or override the dependency in tests.
Configuration lives in app/main_config.py and is loaded through Pydantic settings.
- Shipped env files:
app/env_files/.env_baseandapp/env_files/.env_local - For other environments, create
app/env_files/.env_<env>or provide values through environment variables - Environment variables override file values
- Environment-specific files override the shared base file
- Secret-backed fields resolve through
app/core/secrets.pyand can load from.secrets_<env>or environment variables, depending on the configured secret loader
Key config groups:
Settings: process-level host, port, reload, workers, environmentFastAPIConfig: docs URLs, root path, FastAPI debug flagDatabaseConfig: async SQLAlchemy URL and pool tuningCORSConfig: allowed origins, methods, headers, credentialsLoggingConfig: log format and per-library levelsJWTConfig: placeholder JWT settings for future auth work
Guardrails already in place:
FASTAPI_DEBUGis rejected in production- Default CORS methods and headers are explicit instead of wildcarded
- The auth dependency fails closed until a real backend is implemented
app/main.pyconfigures logging before creating the FastAPI appapp/core/lifespan.pyinitializes the database pool and shared HTTP client on startup, then disposes both on shutdownapp/core/route_discovery.pyimports every non-private Python module underapp/routesand includes any exportedrouterapp/core/exceptions/handlers.pynormalizes app errors, validation failures, and unexpected exceptions into a shared JSON shape
make checkEquivalent direct commands:
ruff check app tests
mypy app
pytest -qRun the local stack:
docker compose up --buildThe image also starts the app through python -m app.main so the runtime configuration and logging behavior stay consistent.
- Add domain models under
app/models - Add repositories under
app/repository - Add route modules under
app/routes - Replace the placeholder auth dependency with real JWT validation
- Add service-specific tests beyond the shared exception and config coverage
- Add migrations, CI, and observability pieces for your environment
- Alembic migrations
- Real authentication and token issuance
- Background jobs or queue integrations
- Metrics and tracing exporters
- Caching or message broker integrations
MIT