A production-grade FastAPI backend template with modern architecture, full async support, and enterprise-level patterns.
- 🔐 Authentication & Authorization - JWT-based authentication with refresh tokens
- ⚡ Full Async Stack - AsyncIO throughout (database, HTTP clients)
- 🎯 Exception-Driven Architecture - Structured error handling with middleware
- 📊 Structured Logging - JSON logging for production, colored for dev
- 🔍 Request Tracing - Unique request IDs for debugging
- 🛡️ Production-Ready Security - Multiple middleware layers, validated config
- 🔄 Database Migrations - Alembic integration
- 📧 Email Services - SMTP integration ready
- 🧪 Async Testing - Pytest with async fixtures
- Framework: FastAPI (async-native)
- Database: SQLAlchemy 2.0 + SQLModel (async with psycopg/aiosqlite)
- Authentication: JWT (python-jose) with access/refresh tokens
- Password Hashing: Passlib with bcrypt
- Validation: Pydantic v2 with field validators
- HTTP Client: HTTPX (async)
- Logging: Structured JSON logging for production
- Testing: Pytest with pytest-asyncio
- Migrations: Alembic
- Type Checking: MyPy
fastapi-backend/
├── alembic/ # Database migrations
├── common/ # Shared modules
│ ├── exceptions/ # Custom exception hierarchy (auth, SDK, etc.)
│ └── response_models/ # Pydantic response models
├── config/ # Application configuration (validated with Pydantic)
├── controllers/ # Request handlers (optional controller layer)
├── database/ # Database models and async session management
│ └── models/ # SQLModel ORM models
├── middlewares/ # Custom middleware (exception handler, logging, etc.)
├── routes/ # API routes (auth, dashboard, etc.)
│ └── auth/ # Authentication endpoints
├── SDK/ # SDK modules for external services
│ └── base/ # Base resource class with exception handling
├── services/ # Business logic layer (async services)
│ └── auth/ # Authentication service
├── tests/ # Test suite (async tests with pytest-asyncio)
├── utils/ # Utility functions
│ ├── app_factory.py # FastAPI app creation with middleware stack
│ ├── auth.py # JWT utilities
│ ├── logging_config.py # Structured logging setup
│ └── fetch_client.py # Async HTTP client
├── workers/ # Background workers (async base classes)
├── main.py # Application entry point
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── ARCHITECTURE.md # Detailed architecture documentation
└── REFACTORING_SUMMARY.md # Refactoring guide and improvements
- Python 3.11+
- PostgreSQL (or SQLite for development)
- Redis (for background tasks)
- Clone the repository:
git clone <repository-url>
cd fastapi-backend- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Configure environment variables:
cp .env.example .env
# Edit .env with your configuration- Initialize the database:
alembic upgrade headDevelopment mode:
python main.pyOr with uvicorn directly:
uvicorn main:app --reload --host 0.0.0.0 --port 8000The API will be available at:
- API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Alternative Documentation: http://localhost:8000/redoc
# Run all tests (32 tests)
pytest
# Run with verbose output
pytest -v
# Run with coverage
pytest --cov=. --cov-report=html
# Run specific test file
pytest tests/test_auth.py -vTest Coverage: 35 tests across 6 modules (auth, dashboard, health, models, services, utils)
Create a new migration:
alembic revision --autogenerate -m "Description of changes"Apply migrations:
alembic upgrade headRollback migration:
alembic downgrade -1Once the application is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Key environment variables (see .env.example for complete list):
Application
APP_NAME: Application nameENVIRONMENT: development, staging, or productionDEBUG: Enable debug mode (boolean)LOG_LEVEL: DEBUG, INFO, WARNING, ERROR, CRITICAL
Database
DATABASE_URL: Database connection string (use async drivers:sqlite+aiosqlite://orpostgresql+psycopg://)
Security
SECRET_KEY: JWT signing key (32+ characters in production)ACCESS_TOKEN_EXPIRE_MINUTES: Access token expiration (default: 30)REFRESH_TOKEN_EXPIRE_DAYS: Refresh token expiration (default: 7)
EMAIL_HOST,EMAIL_PORT,EMAIL_USER,EMAIL_PASSWORD: SMTP configurationEMAIL_FROM,EMAIL_FROM_NAME: Default sender
CORS
CORS_ORIGINS: Allowed origins (JSON array)
This project follows senior-level production patterns:
- Full async/await stack (routes, services, database)
- Non-blocking I/O for 5-10x better throughput
- AsyncPG for PostgreSQL, aiosqlite for SQLite
- Custom exception hierarchy (
common/exceptions/) - No silent failures (no
return Nonepatterns) - Global exception handler middleware for consistent HTTP responses
- JSON logging in production (ELK/Datadog/CloudWatch ready)
- Colored console output in development
- Request ID tracking for distributed tracing
- Sensitive data filtering
- Request ID - Unique ID for each request
- Logging - Request/response timing
- Exception Handler - Converts exceptions to HTTP responses
- CORS - Cross-origin resource sharing
- GZip - Response compression
- Trusted Host - Security (production only)
# Pattern 1: Routes (dependency injection)
@router.get("/users")
async def get_users(db: AsyncSession = Depends(get_db)):
result = await db.execute(select(User))
return result.scalars().all()
# Pattern 2: Background tasks (context manager)
async with async_session_scope() as db:
user = await db.get(User, user_id)See ARCHITECTURE.md for detailed documentation.
This project follows PEP 8 guidelines and uses type hints throughout:
# Type checking
mypy .- Define models in
routes/(Pydantic request/response models) - Create service in
services/(async business logic) - Add route in
routes/(FastAPI endpoint) - Handle exceptions - Raise custom exceptions, middleware handles conversion
- Add tests in
tests/(async tests with pytest-asyncio)
Example:
# services/user/__init__.py
async def get_user_profile(db: AsyncSession, user_id: str) -> User:
user = await db.get(User, user_id)
if not user:
raise UserNotFoundException(identifier=user_id)
return user
# routes/user/__init__.py
@router.get("/users/{user_id}", response_model=UserResponse)
async def get_user(
user_id: str,
db: AsyncSession = Depends(get_db)
):
return await UserService.get_user_profile(db, user_id)Configuration
- Set
ENVIRONMENT=productionin.env - Set
DEBUG=false - Generate strong
SECRET_KEY(32+ characters):python -c "import secrets; print(secrets.token_urlsafe(32))" - Use PostgreSQL with async driver:
DATABASE_URL=postgresql+psycopg://user:password@localhost:5432/dbname - Configure specific CORS origins (no wildcards)
- Set up SSL/TLS certificates
Deployment
# Using uvicorn (recommended for FastAPI)
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
# Or with gunicorn + uvicorn workers
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000Infrastructure
- Configure load balancer health checks:
GET /health - Set up log aggregation (JSON logs to stdout)
- Configure monitoring/alerting
- Run database migrations before deployment
- Use environment variables (never commit secrets)
Docker (optional)
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]Key Improvements Over Typical Templates:
- ✅ Full async stack - No blocking calls in async routes
- ✅ Exception-driven - No silent failures, proper error handling
- ✅ Structured logging - JSON for production, request tracing
- ✅ Validated configuration - Pydantic with validators, fail-fast
- ✅ Middleware stack - Request ID, logging, exception handling, compression
- ✅ Security layers - Multiple middleware, validated secrets, secure defaults
- ✅ Database best practices - Connection pooling, async sessions, proper cleanup
- ✅ Complete auth flow - Registration, login, refresh tokens, JWT
- ✅ Production configs - Multi-worker, graceful shutdown, health checks
- ✅ Comprehensive docs - Architecture guide, refactoring summary
See REFACTORING_SUMMARY.md for detailed improvements made.
- README.md - This file (quick start and overview)
- ARCHITECTURE.md - Detailed architecture decisions and patterns
- REFACTORING_SUMMARY.md - What was changed and why
- API Docs - Auto-generated at
/docs(Swagger UI)
This is a template - fork it and make it your own! Customize:
- Branding (APP_NAME, email templates)
- Authentication requirements
- Additional middleware (rate limiting, etc.)
- Custom exceptions for your domain
- Business logic in services/
MIT License - Free to use for commercial and personal projects.
Created by Jeremy - For questions or improvements, open an issue or PR!