DDD + Hexagonal Architecture FastAPI Boilerplate
A production-ready boilerplate for building FastAPI applications with Domain-Driven Design and Hexagonal Architecture patterns.
- Domain-Driven Design: Clean separation of domain logic from infrastructure
- Hexagonal Architecture: Port-Adapter pattern for flexible dependency management
- FastAPI: Modern, fast web framework with automatic OpenAPI documentation
- MongoDB: Async database operations with Motor
- AWS SQS: FIFO queue integration for async task processing
- Unit of Work: Transaction support with MongoDB replica sets
- Three Entrypoints: API, Worker, CLI for different use cases
# Clone the repository
git clone <your-repo-url> your-project
cd your-project
# Create virtual environment
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
# Install dependencies
pip install -r src/requirements.txt# Copy example environment file
cp .env.example .env
# Edit .env with your settings
vi .env# API Server (development)
./void run api
# SQS Worker
./void run worker
# CLI Job
./void run job <JOB_NAME>src/
├── domain/ # Pure Python domain logic
│ ├── entities/ # Domain entities
│ ├── ports/ # Abstract interfaces
│ └── value_objects/ # Enums and value objects
├── service_layer/ # Application services
│ ├── application/ # Use case implementations
│ └── exceptions.py # Business exceptions
├── adapters/ # Infrastructure implementations
│ ├── aws/ # SQS client/producer/consumer
│ ├── http/ # HTTP client (httpx)
│ ├── mongodb/ # MongoDB adapters
│ ├── repositories/ # Repository implementations
│ └── uow/ # Unit of Work
├── entrypoints/ # Application entry points
│ ├── api/ # FastAPI application
│ ├── worker/ # SQS Worker
│ └── cli/ # CLI Jobs
└── config.py # Configuration
# Start API server with hot reload
./void run api
# Start SQS consumer worker
./void run worker
# Run a specific CLI job
./void run job process_item --item-id 507f1f77bcf86cd799439011
# List available jobs
./void run job list| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Root endpoint |
| GET | /health |
Health check |
| POST | /api/v1/items |
Create item |
| GET | /api/v1/items/{id} |
Get item by ID |
| Variable | Description | Default |
|---|---|---|
ENVIRONMENT |
Environment name | development |
DEBUG |
Debug mode | true |
MONGODB_URI |
MongoDB connection URI | mongodb://localhost:27017 |
MONGODB_NAME |
Database name | void |
AWS_ACCESS_KEY_ID |
AWS access key | - |
AWS_SECRET_ACCESS_KEY |
AWS secret key | - |
AWS_REGION |
AWS region | ap-northeast-2 |
SQS_QUEUE_URL |
SQS FIFO queue URL | - |
This project follows Domain-Driven Design (DDD) and Hexagonal Architecture principles:
-
Domain Layer (
domain/)- Pure Python with no external dependencies
- Contains entities, value objects, and port interfaces
-
Service Layer (
service_layer/)- Application services implementing use cases
- Orchestrates domain logic and infrastructure
-
Adapters Layer (
adapters/)- Infrastructure implementations
- MongoDB repositories, AWS clients, HTTP clients
-
Entrypoints Layer (
entrypoints/)- Application entry points
- API routes, Worker tasks, CLI jobs
- Repository Pattern: Abstract data access through interfaces
- Unit of Work: Transactional consistency (use only for multi-write operations)
- Dependency Injection: FastAPI's Depends for loose coupling
- Task Registry: Decorator-based worker task registration
- Job Registry: Decorator-based CLI job registration
- Exception Pattern: Class-level
status_code/error_typefor automatic HTTP response mapping
- Create entity in
domain/entities/(withcreate()factory method) - Define repository port in
domain/ports/ - Add value objects in
domain/value_objects/(if needed) - Implement MongoDB adapter in
adapters/mongodb/collections/ - Implement repository in
adapters/repositories/mongodb/ - Register repository in
adapters/uow/mongo_unit_of_work.py
- Create schemas in
entrypoints/api/schemas/ - Create route handler in
entrypoints/api/routes/ - Register router in
entrypoints/api/routes/__init__.py
- Create task handler with
@taskdecorator inentrypoints/worker/tasks/ - Add module to
TASK_MODULESinentrypoints/worker/tasks/__init__.py
- Create job handler with
@jobdecorator inentrypoints/cli/jobs/ - Add module to
JOB_MODULESinentrypoints/cli/jobs/__init__.py
- Define exception in
service_layer/exceptions.pyinheriting fromServiceError - Set
status_codeanderror_typeclass attributes - Exception is automatically handled by API (no additional registration needed)
- Python 3.9+
- MongoDB (Replica Set for transactions)
- AWS Account (for SQS)
MIT License