“FastAPI is one of the fastest-growing Python frameworks, loved for its simplicity, performance, and flexibility. But because FastAPI is unopinionated, there is no single “official” FastAPI project structures.
This is beneficialβit gives you freedom.
But itβs also challengingβhow do you know which structure is right?
So, it’s important for backend developers to understand project structure, which is the architecture of the application. We will cover DDD and Clean Architecture, and how we can create a project design based on Clean Architecture. For starters like me, it is very confusing, as many tutorials don’t teach which project structure is recommended and when to use it.
In this guide, we’ll learn:
- Example folder layouts for each”Letβs dive in.
- The most commonly used FastAPI project structures
- How horizontal (layered) architecture works
- How vertical (feature-based) architecture works
- When to choose one over the other
Table of Contents
Mine FastAPI project structures
Im learning FastAPI, I’m following Clean Architecture principles as much as possible and below is mine project structure.
Mine project structure follows a horizontal (layered) organization, which is a common approach for implementing the principles of Clean Architecture and i know its need lots of improvement.
app/
βββ config.py
βββ routes/
βββ repository/
βββ schemas/
βββ services/
βββ db/
βββ models/
βββ database.py
βββ db.db
βββ init_db.py
π Improvements suggestion from chatgpt
As searching through chatgpt, got following suggestion of improvement of the project structure.
1. Separate core config from app logic: Common pattern for FastAPI, Flask, Django
app/core/config.py
2. Organize routes using βversionsβ: Easier for API evolution.
app/api/v1/users.py
app/api/v1/auth.py
app/api/router.py
3. db/structure refinement
Rename database.py β session.py. Keep Base in a dedicated base.py to avoid import cycles.
app/db/session.py # engine, SessionLocal, get_db()
app/db/base.py # Base and import models
Place db.db inside /data/ or root, not inside source code.
project/data/db.db
- init_db.py only for dev
Creating tables using create_all() is okay only for development. In production, use Alembic migrations.
β Why Project Structure Matters
Choosing the right structure affects:
- Scalability
- Maintainability
- Onboarding new developers
- Testing
- Code clarity
- Separation of concerns
A good structure ensures that as your application grows, it does not become a tangled mess.
1. π§± Horizontal / Layered Architecture (Most Common)
This structure organizes your project by technical layers, not by domain or feature, here is full details of horizontal architecture.
Example:
project/
β
βββ .env
βββ main.py
βββ alembic/ # (Optional but recommended for DB migrations)
βββ app/
β βββ core/ # Core configuration and initialization
β β βββ config.py # Pydantic Settings, app config
β β βββ security.py # Auth utilities (JWT/OAuth2)
β β βββ logging.py # Centralized logging config
β β
β βββ api/
β β βββ deps.py # Common dependencies for routes
β β βββ v1/
β β β βββ __init__.py
β β β βββ users.py # One file per domain route
β β β βββ auth.py
β β β βββ items.py
β β βββ router.py # APIRouter aggregator
β β
β βββ db/
β β βββ base.py # Base = declarative_base(), import all models here
β β βββ session.py # engine, SessionLocal, get_db dependency
β β βββ init_db.py # create_all (only for dev, not for prod)
β β βββ models/
β β βββ user.py
β β βββ item.py
β β βββ __init__.py
β β
β βββ repositories/
β β βββ user_repo.py
β β βββ item_repo.py
β β βββ __init__.py
β β
β βββ services/
β β βββ user_service.py
β β βββ auth_service.py
β β βββ __init__.py
β β
β βββ schemas/
β β βββ user.py
β β βββ item.py
β β βββ __init__.py
β β
β βββ utils/
β β βββ helpers.py
β β βββ email_utils.py
β β
β βββ __init__.py
β
βββ requirements.txt / pyproject.toml
This is the architecture used by many production FastAPI applications.
β Why teams use it
- Clear separation of concerns
- Easy to test individual layers
- Works well for small and medium apps
- Familiar to developers from Django/Flask/Java backgrounds
β Pros
β Clean, predictable structure
β Easy to read and navigate
β Modules are decoupled
β Good for CRUD-heavy apps
β Ideal for monolithic services
β Cons
β As your app grows, files become scattered
β Business logic can spread across multiple folders
β Adding new features often means editing many folders
β When to choose Horizontal / Layered
Choose this architecture if:
- Your app is small to medium-sized
- You want maximum clarity and organization
- You have few developers on the team
- You value technical separation (e.g., controllers/services/repositories)
- Youβre building an internal API, CRM, admin tool, or SaaS backend
This is the most common structure for FastAPI.
2. π¦ Vertical / Feature-Based Architecture (Very Common for Large Apps)
Instead of organizing by layers, you organize by feature/domain.
Example:
app/
βββ users/
β βββ models.py
β βββ schemas.py
β βββ routes.py
β βββ repository.py
β βββ service.py
β βββ __init__.py
β
βββ auth/
β βββ routes.py
β βββ service.py
β βββ schemas.py
β βββ models.py
β
βββ core/
βββ config.py
βββ database.py
βββ security.py
Each feature contains everything it needs. This approach is widely used in microservices, domain-driven design, and large enterprise systems.
β Why teams use it
- All files for a feature are kept together
- Easy for multiple developers to work independently
- Fewer cross-module imports
- Features are modular and isolated
β Pros
β Perfect for large-scale applications
β Each feature is self-contained
β Easier refactoring
β Easier to convert features into microservices later
β Very clean boundaries
β Cons
β Harder to maintain shared logic if not planned well
β Beginners may find it unfamiliar
β Can duplicate code across features
β Requires discipline to avoid cross-dependencies
β When to choose Vertical / Feature-Based
Choose this architecture if:
- Your application will grow large
- You have many developers working in parallel
- You want isolated modules
- Youβre following Domain Driven Design (DDD)
- You may split the app into microservices later
This is the best architecture for enterprise-level FastAPI apps.
3. Minimal / Flat Structure (Good for learning)
For very small or prototype apps:
app/
βββ main.py
βββ routes.py
βββ models.py
βββ schemas.py
βββ database.py
Great for:
- Tutorials
- Demos
- MVPs
Not recommended for production.
4. Hybrid Architecture (Popular in Real Teams)
Some teams combine both:
- Shared layers for common logic
- Feature folders for large modules
Example:
app/
βββ core/
βββ db/
βββ common/
β βββ utils.py
β βββ dependencies.py
β
βββ features/
β βββ users/
β βββ items/
β βββ auth/
This is often the most practical structure for very large projects.
π§ Which Architecture Should You Choose?
Hereβs a quick cheat sheet:
| App Size | Team Size | Best Choice |
|---|---|---|
| Tiny / prototype | 1 | Minimal |
| Small | 1β3 | Layered (horizontal) |
| Medium | 3β10 | Layered or Hybrid |
| Large | 10+ | Vertical (feature-based) or Hybrid |
| Microservices | Any | Vertical |
Related Artciles