A comprehensive, production-ready blueprint for implementing Clean Architecture in FastAPI projects. This template provides a modular, scalable foundation for building modern web applications following SOLID principles and separation of concerns.
- Clone or copy this blueprint to your new project
- Update project metadata in
pyproject.tomlandconfig/settings.py - Install dependencies:
pip install -r requirements.txt - Run the application:
uvicorn src.main:app --reload - Run tests:
pytest
- Architecture Overview - High-level design and Clean Architecture principles
- Project Structure - Detailed folder organization and purpose
- Development Guide - How to extend and add features
- Testing Strategy - Unit and integration testing approach
┌─────────────────────────────────────────┐
│ Presentation Layer (API) │
│ - FastAPI routes, dependencies │
│ - Request/response schemas │
└──────────────┬──────────────────────────┘
│
┌──────────────▼──────────────────────────┐
│ Application Layer │
│ - Use cases, application services │
│ - Business orchestration │
└──────────────┬──────────────────────────┘
│
┌──────────────▼──────────────────────────┐
│ Domain Layer (Core Business Logic) │
│ - Entities, value objects │
│ - Domain rules & invariants │
└──────────────┬──────────────────────────┘
│
┌──────────────▼──────────────────────────┐
│ Infrastructure Layer │
│ - Database, repositories │
│ - External services, adapters │
└─────────────────────────────────────────┘
- ✅ Clean Architecture - Well-separated concerns with testable, maintainable code
- ✅ Dependency Injection - Using FastAPI's
Dependsand manual injection - ✅ Repository Pattern - Abstract data access with multiple implementation options
- ✅ Environment Configuration - Support for
.envfiles and environment-specific settings - ✅ Error Handling - Custom exceptions and centralized error responses
- ✅ Validation - Pydantic schemas for request/response validation
- ✅ Logging - Structured logging with Python's logging module
- ✅ Testing - Comprehensive unit and integration test examples
- ✅ Database Agnostic - Easily switch between SQLAlchemy, MongoDB, etc.
FastAPIBluePrint/
├── src/
│ ├── domain/ # Core business logic (entities, interfaces)
│ ├── application/ # Use cases and application services
│ ├── infrastructure/ # Database, external services, repositories
│ ├── presentation/ # FastAPI routes, schemas, dependencies
│ ├── config/ # Configuration management
│ └── main.py # Application entry point
├── tests/
│ ├── unit/ # Unit tests for use cases
│ ├── integration/ # Integration tests for adapters
│ └── conftest.py # Pytest fixtures and configuration
├── docs/ # Documentation files
├── config/ # Runtime configuration
├── pyproject.toml # Project metadata and dependencies
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── pytest.ini # Pytest configuration
- Framework: FastAPI 0.104+
- Python: 3.10+
- Database: SQLAlchemy ORM (adapter pattern allows alternatives)
- Validation: Pydantic v2
- Testing: pytest, pytest-asyncio
- Logging: Python's built-in logging
- Configuration: python-dotenv, Pydantic Settings
This blueprint is suitable for:
- CRUD Applications - User management, content management systems
- Domain-driven Design Projects - Complex business logic with rich domain models
- Microservices - Isolated, independently deployable services
- REST APIs - Clean separation between business logic and API layer
- Event-driven Systems - Extensible architecture for event handling
- Keep all use cases in a single file per feature
- Simplify repository implementations
- Minimal configuration complexity
- One use case per file
- Multiple repository implementations
- Enhanced error handling and logging
- Feature-based organization within layers
- Multiple database backends
- Event-driven architecture, messaging queues
- Comprehensive testing and CI/CD
See Development Guide for detailed scaling strategies.
# Run all tests
pytest
# Run with coverage
pytest --cov=src
# Run specific test file
pytest tests/unit/test_example.py
# Run tests matching pattern
pytest -k "test_user"- Start with Architecture Overview
- Review Project Structure
- Examine skeleton code in
src/directory - Read Development Guide for adding features
- Study test examples in
tests/directory - Refer to Testing Strategy
To add a new feature:
- Create entities in
domain/entities/ - Define repository interfaces in
domain/repositories/ - Implement use cases in
application/use_cases/ - Add repository implementations in
infrastructure/repositories/ - Create FastAPI routers in
presentation/routers/ - Add tests in
tests/unit/andtests/integration/
See Development Guide for detailed examples.
This blueprint is provided as-is for educational and commercial use.
Last Updated: December 2025
Version: 1.0.0
Python: 3.10+