fastapi-clean
v0.0.1A minimal, modular, and clean architecture base recipe for FastAPI applications using Pydantic v2 and decoupled routing.
xnex install fastapi-clean GitHub repo FastAPI Clean Base Recipe
A production-ready, modular, and lightweight foundation for modern FastAPI applications. Designed for developers who need a clean architectural pattern with separated routing, central environment configuration, and strict module boundaries without bloat.
๐๏ธ Architecture Overview
This recipe scaffolds a predictable, decoupled layout following Python/FastAPI best practices:
app/
โโโ api/
โ โโโ endpoints/
โ โ โโโ health.py # Route controller definitions
โ โโโ router.py # Central API Router aggregator
โโโ core/
โ โโโ config.py # Pydantic Settings with .env parsing
โโโ main.py # Application entrypoint & Lifecycle
๐ Installation & Automated Setup
You don't need to manually install dependencies or set up file structures. The Nex CLI handles everything out of the box using the recipe manifest:
xnex install fastapi-clean
What Happens Automatically Behind the Scenes:
- Scaffolding: Downloads and places the modular
app/architecture into your current working directory. - Dependencies: Automatically executes
pip installusing the recipe'srequirements.txt. - Dev Server: Launches the live development server instantly.
โ๏ธ Configuration & Execution
Environment Variables
Optionally create a .env file in your root directory to override default settings:
PROJECT_NAME="FastAPI Clean App"
API_STR="/api"
Running the Application
If you need to start the development server manually in the future:
fastapi dev app/main.py
Access the interactive Swagger API documentation at: http://127.0.0.1:8000/docs
๐งฉ Deep-Dive Code Examples
1. Settings Management (app/core/config.py)
Uses pydantic-settings to parse environment variables securely while ignoring unexpected system input.
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
PROJECT_NAME: str = "FastAPI Clean Architecture"
API_STR: str = "/api"
# Ignores unknown extra environment variables (e.g., NODE_ENV)
model_config = SettingsConfigDict(
env_file=".env",
extra="ignore"
)
settings = Settings()
2. Endpoint Logic (app/api/endpoints/health.py)
Define domain-specific route handlers in separate files inside endpoints/.
from fastapi import APIRouter
router = APIRouter()
@router.get("/health", tags=["System"])
async def health_check():
return {
"status": "ok",
"message": "Service is up and running"
}
3. Router Aggregator (app/api/router.py)
Combines isolated endpoint modules into a unified router module.
from fastapi import APIRouter
from api.endpoints import health
api_router = APIRouter()
api_router.include_router(health.router)
4. Application Entry (app/main.py)
Attaches the aggregated router and configures global application settings.
from fastapi import FastAPI
from core.config import settings
from api.router import api_router
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_STR}/openapi.json"
)
# Register top-level API routes under configured prefix
app.include_router(api_router, prefix=settings.API_STR)
๐ ๏ธ Adding New Endpoints
To extend this base structure:
- Add a new route handler in
app/api/endpoints/(e.g.,users.py). - Define an
APIRouter()instance insideusers.py. - Register it inside
app/api/router.py:
from api.endpoints import users
api_router.include_router(users.router, prefix="/users", tags=["Users"])
Versions
Release links are tracked by Nex; source code lives in the developer's GitHub repo.
setup.files
Files that will be integrated into your project
- app
- api
- endpoints
- health.py โ app/api/endpoints
-
- router.py โ app/api
-
- core
- config.py โ app/core
-
- main.py โ app
-
- recipes
- fastapi-clean
- README.md โ recipes/fastapi-clean
- requirements.txt โ recipes/fastapi-clean
-
-
setup.scripts
Commands executed during recipe integration
These commands run automatically during integration โ after your explicit approval via the Nex CLI.
- 1 Auto-run script
pip install -r .\recipes\fastapi-clean\requirements.txt - 2 Auto-run script
fastapi dev app/main.py
nexconfig
Full recipe configuration
{
"name": "fastapi-clean",
"version": "0.0.1",
"description": "A minimal, modular, and clean architecture base recipe for FastAPI applications using Pydantic v2 and decoupled routing.",
"keywords": [
"fastapi",
"clean-architecture",
"python",
"pydantic",
"uvicorn",
"backend",
"boilerplate",
"recipe"
],
"files": [
"app",
"recipes/fastapi-clean"
],
"setup": {
"scripts": [
"pip install -r .\\recipes\\fastapi-clean\\requirements.txt",
"fastapi dev app/main.py"
],
"files": {
"app": {
"api": {
"endpoints": {
"health.py": "app/api/endpoints"
},
"router.py": "app/api"
},
"core": {
"config.py": "app/core"
},
"main.py": "app"
},
"recipes/fastapi-clean": {
"README.md": "recipes/fastapi-clean",
"requirements.txt": "recipes/fastapi-clean"
}
}
}
}
You might also be interested in these
fastapi-exceptions
Global exception handling module and demo error endpoints for FastAPI recipes.
nexcord-core
Nexcord basic project recipe
astro-cards
A collection of beautifully designed, standalone UI kit card components for Astro
nestjs-basic-auth
NESTJS BASIC AUTH RECIPE
nestjs
NESTJS EMPTY TEMPLATE