A high-performance, minimalist architecture for asynchronous, NoSQL-native Python web applications and microservices.
FastAPI NoSQL Template provides a clean, modular foundation for building scalable backends with MongoDB and Redis. Designed with performance and code quality in mind, it delivers zero-ORM overhead, strict type safety, asymmetric JWT security, and native support for both REST and GraphQL APIs.
- Fully Asynchronous: End-to-end non-blocking I/O across FastAPI, MongoDB, and Redis.
- Pydantic v2 Integration: High-speed schema validation and serialization.
- Dual API Support: Parallel REST (
v1,v2) and GraphQL (via Strawberry) endpoint structures. - Strict Code Quality: Fully compliant with PEP 8 standards, clean module structures, and 2-space indentation.
- Asymmetric JWT Authentication: RS256 token signing powered by private/public RSA keypairs.
- Role-Based Access Control (RBAC): Fine-grained, scope-based permissions for Admin, Seller, and Customer roles.
- Distributed Rate Limiting: Dynamic, Redis-backed rate limiting managed via SlowAPI.
- MongoDB Atlas & Local Support: Smart URI construction supporting standalone MongoDB instances and Atlas (SRV) clusters.
- Admin Management API: Specialized endpoints for managing user profiles, role migrations, product catalogs, and system metrics.
- Caching Layer: Optimized Redis caching for user sessions and frequent entity lookups.
- Prometheus Monitoring: Pre-configured
/metricsendpoint and structured JSON application logging. - Complete Test Coverage: Unit and integration test suite with full AsyncMock support for MongoDB and Redis operations.
- Production Orchestration: Docker Compose configuration paired with an Nginx reverse proxy gateway.
| Layer | Technology | Description |
|---|---|---|
| Framework | FastAPI | Asynchronous Web Framework |
| Language | Python 3.11+ | Type Annotations & PEP 8 Standards |
| Database | MongoDB | Async NoSQL Data Persistence |
| Caching & Rate Limiting | Redis & SlowAPI | In-memory Data Store |
| GraphQL | Strawberry | Python-ic GraphQL Implementation |
| Security | Authlib & PyJWT | OAuth2, RS256 JWT, and Password Hashing |
| Observability | Prometheus | Application Metrics Instrumentation |
| Gateway | Nginx & Docker | Reverse Proxy & Containerization |
- Docker and Docker Compose
- Python 3.11+ (if running locally)
-
Clone the Repository
git clone https://github.com/th0truth/fastapi-nosql-template.git cd fastapi-nosql-template -
Configure Environment Variables
cp .env.example .env
-
Generate RSA Keypair for JWT Signing
openssl genrsa -out private_key.pem 2048 openssl rsa -in private_key.pem -pubout -out public_key.pem
Format the key contents into single-line strings with
\nline breaks and assign them toPRIVATE_KEY_PEMandPUBLIC_KEY_PEMin your.envfile. -
Launch Application Stack
docker compose up --build
| Script Command | Purpose |
|---|---|
bash scripts/build.sh |
Build production-ready Docker images |
bash scripts/run.sh |
Start the full application container stack |
bash scripts/clean.sh |
Stop containers and prune unused networks and volumes |
Access the interactive GraphQL IDE at /graphql to execute combined queries:
query GetSystemOverview {
user(username: "admin") {
username
email
role
}
products(category: "electronics") {
title
brand
price
}
}The codebase strictly adheres to PEP 8 style guidelines:
- Default 2-space indentation width.
- Enforced line length restrictions (
$\le 88$ characters). - Explicit vertical spacing before control flow statements (
if,while,for,return). - Standardized import order (Standard Library
$\rightarrow$ Third-Party$\rightarrow$ Local Application).
If your team or project standard requires 4-space indentation, you can instantly reformat the entire codebase using ruff:
# Reformat entire codebase to 4-space indentation
uv run ruff format --config 'indent-width=4' .To switch back to 2-space indentation at any time:
# Reformat entire codebase to 2-space indentation
uv run ruff format --config 'indent-width=2' .This project is licensed under the MIT License. See the LICENSE file for complete details.
