FastAPI framework with Pydantic v2 patterns, PII sanitisation, and practical workflows
Create a new FastAPI project with this layout:
<project>/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI app, lifespan, router includes
│ ├── routers/ # APIRouter modules
│ │ └── __init__.py
│ ├── models/ # DB / ORM models
│ │ └── __init__.py
│ ├── schemas/ # Pydantic request/response schemas
│ │ └── __init__.py
│ ├── dependencies/ # Shared Depends() callables
│ │ └── __init__.py
│ └── core/
│ ├── __init__.py
│ └── config.py # pydantic-settings BaseSettings
├── tests/
│ ├── __init__.py
│ └── test_health.py
└── pyproject.tomlAsk the user for a project name (default: myapp).
Create directories — all folders listed above, each with an __init__.py.
Generate app/core/config.py:
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
app_name: str = "<project-name>"
debug: bool = False
settings = Settings()app/main.py:from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.core.config import settings
@asynccontextmanager
async def lifespan(app: FastAPI):
# startup
yield
# shutdown
app = FastAPI(title=settings.app_name, lifespan=lifespan)
@app.get("/health")
def health():
return {"status": "ok"}tests/test_health.py:from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
r = client.get("/health")
assert r.status_code == 200
assert r.json() == {"status": "ok"}Generate pyproject.toml with dependencies:
fastapi[standard]pydantic-settingspytest, httpx in dev groupVerify by running:
fastapi dev app/main.py &
sleep 2
curl -s http://127.0.0.1:8000/health
kill %1tessl i maria/fastapi@0.1.0