A modern template for building scalable and maintainable web applications using FastAPI following Clean Architecture principles.
- Clean Architecture design
- SOLID principles implementation with focus on Dependency Inversion
- FastAPI framework for high-performance API development
- Session-based authentication with secure password handling
- SQLAlchemy with async support for database operations
- Dishka for dependency injection
- Alembic for database migrations
- Poetry for dependency management
- Docker support for containerization
- Pre-commit hooks with Ruff for code quality
src/
├── application/ # Application business rules
│ ├── interactors/ # Use cases implementation
│ ├── interfaces/ # Abstract interfaces (ports)
│ └── validators.py # Validation rules
├── domain/ # Enterprise business rules
│ ├── entities/ # Business entities
│ └── exceptions.py # Domain exceptions
├── infrastructure/ # External frameworks and tools
│ ├── adapters/ # Implementation of interfaces (adapters)
│ └── database/ # Database related code (SQLAlchemy)
├── main/ # Application configuration
│ ├── ioc/ # Dependency injection setup
│ └── config.py # Configuration management
└── presentation/ # Controllers and exception handlers
└── controllers/ # API endpoints
- Python 3.13+
- Poetry
- Docker and Docker Compose (optional)
- Clone the repository:
git clone https://github.com/yourusername/fastapi-clean-architecture.git
cd fastapi-clean-architecture- Install dependencies:
poetry install- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration- Run migrations:
poetry run alembic upgrade headpoetry run uvicorn src.main.app:create_application --factorydocker-compose up -dThe template includes session-based authentication with the following features:
- User registration with email and password
- Password validation and secure hashing with bcrypt
- User login with session creation
- Session management (creation, validation, deactivation)
- Integration with request handling
Once the application is running, you can access:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Configuration is managed through environment variables and pydantic settings. Key configuration options:
APPLICATION_TITLE: Application nameAPPLICATION_DEBUG: Debug mode flagSESSION_*: Session settingsPOSTGRES_*: Database connection settingsREDIS_*: Redis connection settings
This template strictly adheres to the Clean Architecture principles:
- Independence of frameworks: Business logic is independent of the delivery mechanism (FastAPI)
- Testability: Business rules can be tested without external elements
- Independence of UI: The API can change without changing the business rules
- Independence of database: You can swap SQLAlchemy for another ORM
- Independence of external agencies: Business rules don't know about the outside world
The dependency flow follows the Dependency Inversion Principle:
- Domain layer has no dependencies
- Application layer depends only on the Domain layer
- Infrastructure and Presentation layers depend on the Application layer interfaces