Dynamic CRUD & Auth Generator for SQLModel β single-file plug-and-play.
FastAPI Auto Routes is a single-file dynamic router generator (auto_routes.py) that eliminates repetitive CRUD boilerplate.
Simply download or import the file, configure your SQLModel engine, and youβre ready to generate full-featured CRUD endpoints with:
- β Authentication via Bearer tokens
- β‘ Smart caching (with TTL)
- π Concurrency control
- π§© Bulk operations
- πͺͺ Auto-generated
/loginand/logoutroutes
Built on FastAPI + SQLModel + diskcache, ready to plug into your project.
# Clone the repo
git clone https://github.com/yourusername/fastapi-auto-routes.git
cd fastapi-auto-routes
# Install dependencies
pip install -r requirements.txtor using Poetry:
poetry add fastapi sqlmodel diskcacheSimply copy or import auto_routes.py into your project.
from fastapi import FastAPI
from sqlmodel import SQLModel, Field
from app.db.config import engine # Configure your SQLModel engine
from app.utils.auto_routes import crud_router
class User(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
email: str
password: str
# Create tables
SQLModel.metadata.create_all(engine)
app = FastAPI()
# π Auth Router (Login / Logout)
app.include_router(
crud_router(User, login=True, login_fields=["email", "password"]),
prefix="/auth",
tags=["Auth"]
)
# βοΈ CRUD Router (Requires Token)
app.include_router(
crud_router(User, auth=True, ttl=120, max_concurrent=8),
prefix="/users",
tags=["Users"]
)| Route | Method | Description | Auth Required |
|---|---|---|---|
/auth/login |
POST |
Generate session token | No |
/auth/logout |
POST |
Invalidate active session | β |
/users/ |
GET |
Paginated list of users | β |
/users/{id} |
GET |
Get user by ID | β |
/users/ |
POST |
Create user | β |
/users/{id} |
PATCH |
Update user | β |
/users/{id} |
DELETE |
Delete user | β |
| Parameter | Type | Default | Description |
|---|---|---|---|
model |
Type[SQLModel] |
β | Your SQLModel class |
ttl |
int | None |
None |
Cache expiration time (seconds) |
max_concurrent |
int | None |
cpu_count() |
Max concurrent operations |
login |
bool |
False |
Enables /login and /logout |
login_fields |
List[str] |
None |
Fields used for login validation |
login_ttl |
int |
3600 |
Token lifetime in seconds |
auth |
bool |
False |
Requires Bearer token for all routes |
-
Single-file CRUD & Auth Generation
crud_router()dynamically builds all routes (GET,POST,PATCH,DELETE) for the given model from one file. -
Authentication Layer
-
/login: validates credentials and creates a token stored insessions_cache. -
/logout: invalidates the token. -
Protected routes require the header:
Authorization: Bearer <token>
-
-
Caching & Concurrency
- Uses
diskcachefor persistent caching with optional TTL. - Uses
asyncio.Semaphorefor safe concurrency limits per model.
- Uses
app/
βββ db/
β βββ config.py # Database engine setup
βββ utils/
β βββ auto_routes.py # Single-file router generator
βββ main.py # FastAPI entrypoint
- Python 3.11+
- FastAPI
- SQLModel
- DiskCache
- Uvicorn (for local testing)
Automation without compromise.
Instead of repeating CRUD definitions across every model, this single file dynamically builds routers that are secure, scalable, and production-ready. Your backend becomes data-driven, not boilerplate-driven.
MIT License Β© 2025 Luiz Gabriel MagalhΓ£es Trindade Free for personal and commercial use.
- π§ Project Author: Luiz Gabriel Trindade
- πΌ GitHub: @Luiz-Trindade
- π§ Contact: Email