Sweden
Loading...
India
Loading...

FastAPI Project Structure and Components

This document defines the recommended structure for a FastAPI backend.
It combines best practices from industry resources and the FastAPI official guide to ensure modularity, maintainability, and scalability.


fastapi_backend/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ dependencies.py
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py
β”‚   β”‚   β”œβ”€β”€ security.py
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ session.py
β”‚   β”‚   β”œβ”€β”€ base_class.py
β”‚   β”‚   └── init_db.py
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.py
β”‚   β”‚   └── item.py
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”œβ”€β”€ user.py
β”‚   β”‚   └── item.py
β”‚   β”œβ”€β”€ crud/
β”‚   β”‚   β”œβ”€β”€ user.py
β”‚   β”‚   └── item.py
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ deps.py
β”‚   β”‚   β”œβ”€β”€ api_v1/
β”‚   β”‚   β”‚   β”œβ”€β”€ endpoints/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ users.py
β”‚   β”‚   β”‚   β”‚   └── items.py
β”‚   β”‚   β”‚   └── api.py
β”‚   └── utils/
β”‚       β”œβ”€β”€ email.py
β”‚       └── logging.py
β”œβ”€β”€ tests/
β”œβ”€β”€ .env
└── requirements.txt

2. Layered Architecture Overview

Layer Purpose Example Components
API (Presentation) Handles routing, request validation, responses /api/endpoints/*.py
Service / CRUD Encapsulates business logic and DB operations /crud/*.py
Data (Persistence) Defines ORM models and schemas /models/, /schemas/
Core / Config App config, startup events, security /core/*.py
Infrastructure Utilities, logging, third-party services /utils/*.py

3. Configuration and Dependency Management

Example: core/config.py

from pydantic import BaseSettings

class Settings(BaseSettings):
    PROJECT_NAME: str = "FastAPI Backend"
    DATABASE_URL: str
    JWT_SECRET_KEY: str
    API_V1_STR: str = "/api/v1"

    class Config:
        env_file = ".env"

settings = Settings()

Example: app/dependencies.py

from .db.session import SessionLocal

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

4. Router Organization

Each domain (users, items, etc.) has its own router file.

# app/api/api_v1/endpoints/users.py
from fastapi import APIRouter, Depends
from app import crud, schemas
from app.dependencies import get_db

router = APIRouter()

@router.get("/", response_model=list[schemas.User])
def list_users(db=Depends(get_db)):
    return crud.user.get_all(db)

Routers are then included in api.py:

# app/api/api_v1/api.py
from fastapi import APIRouter
from .endpoints import users, items

api_router = APIRouter()
api_router.include_router(users.router, prefix="/users", tags=["Users"])
api_router.include_router(items.router, prefix="/items", tags=["Items"])

5. Database Layer

# app/db/session.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.core.config import settings

engine = create_engine(settings.DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

6. Real-Time Communication (WebSockets)

FastAPI natively supports WebSockets using Starlette under the hood.
A typical real-time setup includes a ConnectionManager class to handle active connections and broadcasts.

from fastapi import WebSocket

class ConnectionManager:
    def __init__(self):
        self.active_connections = []

    async def connect(self, websocket: WebSocket):
        await websocket.accept()
        self.active_connections.append(websocket)

    async def broadcast(self, message: str):
        for connection in self.active_connections:
            await connection.send_text(message)

This can be extended to handle live collaboration, notifications, and status updates in your system.


7. Summary

A modular structure separates core logic from routing and data management.
This approach simplifies scaling, testing, and onboarding new developers β€” aligning with production-level best practices from the FastAPI ecosystem.