A production-ready FastAPI boilerplate with modern Python development practices, async SQLAlchemy 2.0, Alembic migrations, JWT authentication, and comprehensive tooling.
- FastAPI: Modern, fast (high-performance) web framework
- SQLAlchemy 2.0: Async/sync dual-engine database support
- Alembic: Database migrations with auto-generation
- JWT Authentication: Secure token-based auth with cookie support
- UUID7: Modern UUID generation for database IDs
- Pydantic: Data validation and settings management
- Email System: FastAPI-Mail with Jinja2 templates
- Development Tools: Black, isort, flake8, mypy, pytest
- Docker Support: Multi-stage builds with development/production modes
- UV Support: Modern Python package management
- Comprehensive Logging: Structured logging setup
- Python 3.11+
- PostgreSQL (or SQLite for development)
- Git
git clone <your-repo-url>
cd fastapi-templateCopy and configure your environment variables:
copy .env.sample .envEdit .env with your actual configuration values.
# Install UV if you haven't
pip install uv
# Install dependencies
uv sync
# Run development server
uv run fastapi dev main.py# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Run development server
fastapi dev main.py# Development with hot reload
docker-compose up
# Or build and run manually
docker build -t fastapi-template .
docker run -p 8000:8000 fastapi-template# Run migrations
alembic upgrade head
# Create new migration (after model changes)
alembic revision --autogenerate -m "description"fastapi-template/
βββ api/ # Main API package
β βββ core/ # Core functionality
β β βββ dependencies/ # Dependency injection
β β βββ responses.py # Response constants
β βββ db/ # Database configuration
β β βββ database.py # SQLAlchemy setup
β β βββ enum.py # Database enums
β βββ templates/ # Email templates
β βββ utils/ # Utilities
β β βββ cookies.py # OAuth2 with cookies
β β βββ response.py # Response helpers
β β βββ security.py # JWT utilities
β β βββ settings.py # Configuration
β βββ v1/ # API version 1
β βββ models/ # SQLAlchemy models
β βββ routes/ # API endpoints
β βββ schemas/ # Pydantic schemas
β βββ services/ # Business logic
βββ alembic/ # Database migrations
βββ tests/ # Test suite
βββ main.py # FastAPI application
βββ requirements.txt # Python dependencies
βββ pyproject.toml # Project configuration
βββ docker-compose.yml # Docker development setup
βββ Dockerfile # Container configuration
βββ .env.sample # Environment template
# Development server with hot reload
fastapi dev main.py
# Production server
fastapi run main.py
# Custom host/port
fastapi dev main.py --host 0.0.0.0 --port 8080# Format code
black .
isort .
# Lint code
flake8 .
mypy .
# Run tests
pytest
pytest -v --cov=api # With coverage# Create migration
alembic revision --autogenerate -m "add user table"
# Apply migrations
alembic upgrade head
# Rollback migration
alembic downgrade -1
# Check current version
alembic currentOnce running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
The template includes JWT authentication with cookie support:
# Token in Authorization header
Authorization: Bearer <token>
# Or in cookie
Cookie: access_token=Bearer <token>Built-in email system with Jinja2 templates:
await send_mail(
recipient="user@example.com",
template_name="welcome.html",
subject="Welcome!",
context={"name": "John"}
)docker-compose up# Build production image
docker build --target production -t fastapi-template:prod .
# Run with environment variables
docker run -d \
-p 8000:8000 \
-e DB_HOST=your-db-host \
-e JWT_SECRET_KEY=your-secret \
fastapi-template:prodKey environment variables:
| Variable | Description | Default |
|---|---|---|
APP_NAME |
Application name | "FastAPI Template" |
ENVIRONMENT |
Environment (development/production) | "development" |
DB_HOST |
Database host | "localhost" |
DB_TYPE |
Database type (postgresql/sqlite) | "postgresql" |
JWT_SECRET_KEY |
JWT secret key | Required |
FRONTEND_URL |
Frontend URL for CORS | "" |
See .env.sample for complete configuration options.
# Run all tests
pytest
# Run with coverage
pytest --cov=api --cov-report=html
# Run specific test file
pytest tests/test_users.py
# Run with verbose output
pytest -v# api/v1/models/your_model.py
from api.v1.models.base_mdel import BaseTableModel
from sqlalchemy import Column, String
class YourModel(BaseTableModel):
__tablename__ = "your_table"
name = Column(String, nullable=False)# Import in api/v1/models/__init__.py
from .your_model import YourModel
# Generate migration
alembic revision --autogenerate -m "add your_model"
alembic upgrade head# api/v1/routes/your_routes.py
from fastapi import APIRouter
from api.utils.response import success_response
router = APIRouter()
@router.get("/")
async def list_items():
return success_response(
status_code=200,
message="Items retrieved successfully",
data={"items": []}
)# api/v1/routes/__init__.py
from .your_routes import router as your_router
api_router.include_router(your_router, prefix="/items", tags=["Items"])- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- π FastAPI Documentation
- ποΈ SQLAlchemy Documentation
- π Alembic Documentation
Happy coding! π