A production-ready FastAPI template following Domain-Driven Design (DDD) principles.
- Python 3.14+
- FastAPI for the web framework
- Pydantic v2 for data validation and settings
- SQLModel/SQLAlchemy for ORM
- uv for fast dependency management
- DDD Architecture for scalable and maintainable code
The project follows a Domain-Driven Design (DDD) layout:
app/
├── adapters/ # External interfaces (API routes, middlewares, dependencies)
│ ├── api/ # FastAPI routers
│ ├── dependencies/ # DI providers
│ └── middlewares/ # Custom middlewares
├── application/ # Application logic (orchestration)
│ ├── dto/ # Data Transfer Objects (Pydantic models)
│ └── use_cases/ # Use case implementations
├── domain/ # Core business logic
│ ├── models/ # Domain entities and enums
│ ├── repositories/ # Repository interfaces
│ └── services/ # Domain services
└── core/ # Framework-level configuration
├── db/ # Database config and base models
├── init_app.py # FastAPI initialization logic
├── pydantic_base.py # Shared Pydantic BaseModel
└── settings.py # LRU-cached settings management
-
Setup the project:
make setup
-
Run the application:
uv run python app/main.py
The API will be available at
http://127.0.0.1:8000.
Common tasks are managed via Makefile for convenience and taskipy for granular control:
make setup- Install dependencies and setup virtual environmentmake format- Run code formatting and linting (Ruff + Mypy)make lint- Run linting checks only
uv run task ruff- Format and lint code with Ruffuv run task mypy-lint- Run static type checkinguv run task tests- Run pytest suite with coverageuv run task format-and-lint- Run both Ruff and Mypy
Build the production image:
docker build -f docker/Dockerfile -t fastapi-app .