A modular, production-grade FastAPI template designed for scalable backend services, strong security defaults, and consistent architectural patterns shared across the QuickAPI ecosystem (Express, NestJS, FastAPI).
- Strict Pydantic validation for configuration, requests, and responses
- ASGI middleware suite: header sanitization, security headers, body size limiting, rate limiting, request context, structured logging
- Prometheus metrics with protected
/metricsendpoint - Unified error model replacing default FastAPI 422 responses
- Structured logging using
structlogwith colored, contextual logs - OpenAPI documentation with corrected schemas and custom error responses
- Graceful shutdown via FastAPI lifespan context
- Modular folder structure optimized for large-scale APIs
- CORS + CSP with strict production defaults
- Developer‑friendly architecture inspired by Express & NestJS templates
app/
├── config/ # Environment configuration
├── controllers/ # High-level request orchestration
├── database/
│ ├── entities/ # ORM models
│ └── repositories/ # Database abstraction layer
├── docs/ # OpenAPI utilities & schema customization
├── handlers/ # Process-level handlers (signals, shutdown helpers)
├── middleware/ # All ASGI middleware (security, logging, rate limiting)
├── models/ # Pydantic schemas (ErrorModel, domain models, etc.)
├── routes/ # Router modules, metrics, system endpoints
├── store/ # Request-scoped state (contextvars-backed)
└── main.py # Application factory + middleware wiringAPP_NAME=QuickAPI
APP_VERSION=1.0.0
ENV=development
LOG_LEVEL=DEBUG
HOST=0.0.0.0
PORT=5000
DATABASE_URL=sqlite:///./dev.db
METRICS_API_KEY=dev-metricsAll values are validated on startup. If validation fails, the application prints a clear diagnostic report and exits safely.
pip install -r requirements.txt
uvicorn app.main:create_app --factory --reload --port 5000http://localhost:5000/docs
http://localhost:5000/redochttp://localhost:5000/metrics- colorized structured logs
- contextual
request_id - mute noisy framework logs
- environment-controlled log level
Prometheus middleware emits:
- request counts
- request latency histogram
- status code distribution
Example metric:
http_requests_total{method="GET",path="/api/v1/items",status="200"} 42Prevents:
- header injection
- smuggling vectors
- duplicate headers
- invalid characters
Trims non-whitelisted headers while allowing standard browser headers (connection, keep-alive, etc.).
Rejects large requests (413 Payload Too Large) with custom error model.
Burst + sustained limits with lightweight in‑memory store.
Secure-by-default configurations mirroring Express/Nest templates.
All errors follow the same JSON envelope:
{
"status": 400,
"message": "Missing required field: email",
"timestamp": 1764310185
}FastAPI’s 422 validation responses are fully overridden and documented in OpenAPI.
- Fail-fast validation at every layer
- Strict input sanitation
- Deterministic behavior across environments
- Predictable, platform-level architecture
- Production-first mindset (observability, errors, shutdown, metrics)
MIT License — Free for personal and commercial use.
QuickAPI-FastAPI — part of the QuickAPI ecosystem by John Desjardins.