crudman-fastapi
Minimal, adapter-driven CRUD layer for FastAPI + SQLAlchemy.
Docs · GitHub · NestJS version
Status: 0.2.2 (alpha). Everything below is implemented and covered by end-to-end tests on SQLite and PostgreSQL, except the Redis cache and S3 storage adapters, which are built but not yet tested against live services.
Creating CRUD is so simple. This is a complete Companies CRUD:
from crudman import Crudman, CrudmanOptions, crud_router
from app.models import Company
Crudman.init(app, CrudmanOptions(session_dependency=get_session))
app.include_router(crud_router(Company))
That gives you (the path comes from Company.__tablename__):
GET /companies(with filters, sorting, pagination, keyword)GET /companies/{id}(includes relations by default)POST /companiesPATCH /companies/{id}DELETE /companies/{id}
Plus OpenAPI docs at /docs, validation generated from your model, and CSV / Excel export
(x-content-type: csv or excel).
Or define routes with decorators, like crudman-nestjs, and switch auth on per route:
from fastapi import Request
from crudman import Controller, Crud, CrudDelete, CrudList, Get, Private, Public, Roles
@Controller("/api/companies")
@Private() # signed-in users only...
@Crud(Company) # list, details, create, update, delete
class CompaniesController:
@Public() # ...except the list
@CrudList()
async def list(self): ...
@Roles("admin") # admins only
@CrudDelete()
async def remove(self): ...
@Get("/{id}/summary") # your own route
async def summary(self, id: int, request: Request):
return (await self.crud.details("companies", request)).body
app.include_router(CompaniesController.router)
crudman-fastapi is the Python port of crudman-nestjs. The HTTP contract (query parameters, response envelope, headers) is identical, so the same front-end works against either backend.
Features
- 🔌 Launch-ready CRUD endpoints in one line
- 🐙 Adapter-first design (SQLAlchemy 2.0 async today; ORM, validator, cache and storage are pluggable)
- 🔎 Powerful querying: filters, sorting, pagination, keyword search
- 🧭 Relations and attributes control (include/exclude, sensible defaults)
- 🧪 Validation generated from your model with Pydantic v2, overridable per request
- 🎬 Hooks at every step, with a typed per-request context
- 🔧 Minimal config: global options, per-section and per-action settings
- ✏️ OpenAPI-native: typed request bodies and response envelopes
- 📦 File uploads: multipart/base64 with local/S3 storage and shorthand presets
- 🧊 Per-endpoint caching with invalidation on writes
- 📤 CSV/Excel export, bulk import and bulk delete
At-a-glance capabilities
| Capability | What you get |
|---|---|
| Auto CRUD endpoints | List, details, create, update, delete from crud_router(Model) |
| Zero naming config | Section name and URL default to the table name |
| PATCH-first updates | Partial updates by default (configurable to PUT) |
| Relations by default | All relations eager-loaded; refine with include/exclude |
| Attributes selection | Separate read and write include/exclude |
| Powerful querying | Filters, sorting, pagination, keyword search with safe whitelists |
| Validation built-in | Pydantic models generated from SQLAlchemy columns |
| Uniqueness checks | fields_for_uniqueness_validation with OR/AND semantics |
| Hooks | Before/after action, query, fetch and validation |
| Access control | required_roles, ownership, identity accessor |
| Caching | TTL cache per endpoint, Redis optional |
| Save (upsert) | One endpoint that creates or updates |
| Programmatic calls | call_action across sections, inside the same transaction |
| Import / export | CSV and Excel export; bulk import and delete |
Installation
pip install crudman-fastapi
# optional extras
pip install "crudman-fastapi[uploads,images,excel,s3,redis]"
Requires Python 3.11+, FastAPI 0.136.3+, SQLAlchemy 2.0+, Pydantic 2.9+.
Quick start
1) Model
# app/models.py
from datetime import datetime
from sqlalchemy import String, func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase): ...
class Company(Base):
__tablename__ = "companies"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(150))
email: Mapped[str | None] = mapped_column(String(255))
is_active: Mapped[bool] = mapped_column(default=True)
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
2) Register crudman once
# app/main.py
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from crudman import Crudman, CrudmanOptions, crud_router
from app.models import Company
engine = create_async_engine("sqlite+aiosqlite:///./app.db")
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async def get_session():
async with SessionLocal() as session:
yield session
app = FastAPI()
Crudman.init(app, CrudmanOptions(session_dependency=get_session))
3) Mount the CRUD
app.include_router(crud_router(Company)) # /companies
app.include_router(crud_router(Company), prefix="/api") # /api/companies
crud_router
crud_router(
model, # required: SQLAlchemy model (or a list of models)
section=None, # optional: defaults to the model's table name
config=None, # optional: Section(...) or dict with settings
*,
path=None, # default "/{section}"; "" for no segment
actions=None, # default ["list", "details", "create", "update", "delete"]
tags=None, # default [section]
)
crud_router(Company) # section "companies" → /companies
crud_router(Company, "orgs") # section "orgs" → /orgs
crud_router(Company, config=companies) # with settings
crud_router(model=Company, section="orgs") # keyword form
crud_router(Company, path="/organisations") # custom URL, section stays "companies"
crud_router(UserProfile, path_style="kebab") # table user_profiles → /user-profiles
crud_router(model=Company, "orgs")is a PythonSyntaxError(positional after keyword). Usecrud_router(Company, "orgs")orcrud_router(model=Company, section="orgs").
Several models in one router (each entry is Model, (Model, "name"), (Model, config) or
(Model, "name", config)):
app.include_router(
crud_router([Tag, Category, (Company, companies), (State, "regions", {"relations": ["country"]})]),
prefix="/api",
)
# → /api/tags, /api/categories, /api/companies, /api/regions
Extra endpoints are opt-in: actions=["*", "save", "bulk_import", "bulk_delete"].
Controllers (decorators)
Routes can be defined on a class with decorators instead of crud_router(...). Both styles use the same engine and
section settings.
| Decorator | Meaning |
|---|---|
@Controller(prefix, tags=None, dependencies=None) |
URL prefix for the class |
@Crud(Model, section=None, config=None, path=None, actions=None) |
Register a section and generate its routes (actions=[]: only decorated methods) |
@CrudList(path=None, section=None), @CrudDetails, @CrudCreate, @CrudUpdate, @CrudDelete, @CrudSave, @CrudBulkImport, @CrudBulkDelete |
Place or override a generated route. An empty body lets crudman handle it; a result: CrudResult parameter receives the result to change |
@Get(path), @Post, @Put, @Patch, @Delete |
Custom routes with normal FastAPI parameters; self.crud is the service |
@Controller("/api/companies")
@Crud(Company, config=companies)
class CompaniesController:
def __init__(self, mailer: Mailer = Depends(get_mailer)): # constructor injection
self.mailer = mailer
@CrudDetails("/{id}/full") # new path for a generated route
async def details(self, result: CrudResult) -> CrudResult:
result.body["data"]["display"] = result.body["data"]["name"].upper()
return result
Crudman.init(app, CrudmanOptions(session_dependency=get_session, controllers=[CompaniesController]))
Authentication
Routes are public by default. Tell crudman how to find the user, then mark routes with @Private():
async def current_user(creds = Depends(HTTPBearer(auto_error=False))):
user = decode_jwt(creds.credentials) if creds else None
if user is None:
raise HTTPException(401)
return {"id": user.id, "role": user.role} # becomes ctx.identity
Crudman.init(app, CrudmanOptions(session_dependency=get_session, auth=AuthOptions(dependency=current_user)))
| Decorator | Meaning |
|---|---|
@Private() |
signed-in user required (401) |
@Roles("admin") |
signed in and one of the roles (403); implies @Private() |
@Public() |
no auth; opens one route inside a @Private controller |
They work on controller classes, controller methods and plain FastAPI routes. Without decorators:
crud_router(Company, auth="private", public=["list"], roles={"delete": ["admin"]}), or section config auth.
AuthOptions(dependency=..., default="private") protects everything except @Public() routes. Private routes show
a lock and the Authorize button in Swagger.
Sections
A section is a named CRUD resource: a model plus settings. Settings resolve in three layers, and the later layer wins:
CrudmanOptions.defaults → section top-level keys → section.<action> block
Write a section as a typed object (IDE autocompletion) or a plain dict. Both are validated at startup.
# app/crud/companies.py
from crudman import Section, ListAction, DetailsAction, WriteAction, Keyword, Pagination
companies = Section(
title="Company",
attributes={"read": {"exclude": ["secret"]}}, # every action
required_roles=["admin"],
list=ListAction(
filters_whitelist=["name", "is_active", "created_at"],
sorting_whitelist=["name", "created_at"],
order_by=[("created_at", "DESC")],
keyword=Keyword(searchable_fields=["name", "contacts.email"], min_length=2),
pagination=Pagination(max_per_page=100),
enable_cache={"ttl": 60},
),
details=DetailsAction(relations=["contacts"]),
create=WriteAction(fields_for_uniqueness_validation=["name"]),
update=WriteAction(fields_for_uniqueness_validation=["name"]),
)
Section keys (any of these can also go inside an action block):
| Key | Meaning |
|---|---|
relations / get_relations(ctx) |
"*" (default) · list · {"include": [...]} · {"exclude": [...]} |
attributes |
{"read": ..., "write": ...}, each "*" / list / include / exclude |
filters_whitelist, sorting_whitelist |
default: all readable root columns |
order_by |
default sort when the request sends none |
conditions |
fixed where added to every query |
keyword, keyword_param_name |
keyword search |
query_param_names, pagination |
renames and pagination toggles |
record_selection_field |
default "id"; also names the path param |
fields_for_uniqueness_validation, condition_type_for_uniqueness_validation |
uniqueness checks ("or" / "and") |
required_roles, ownership |
access control |
| hooks | see Hooks |
context |
per-request context builder |
additional_response / get_additional_response |
extra keys merged into meta |
response_handler, custom_implementation |
output shaping / full override |
enable_cache |
True or {"ttl": int, "key": fn} |
uploadable, upload, upload_defaults |
file uploads |
session, orm, validator |
per-section overrides |
List APIs and pagination (quick reference)
- Pagination:
page(default 1),perPage(default 30, max 100; aliasper_page).paginate=false|0|noorperPage=0return everything only when the section setspagination={"is_disable_allowed": True} - Sorting:
sort.<field>=asc|desc(repeatable). Default:created_at DESC, then primary key - Filters:
- Equals:
field=value - Like:
field.like=value(case-insensitive) - Ranges:
field.min,field.max,field.gt,field.lt(numbers or ISO dates) - Between:
field.between=start,end
- Equals:
- Keyword:
keyword=acme
Field names can be sent in camelCase or snake_case: sort.createdAt and sort.created_at both reach the
created_at column.
GET /api/companies?page=2&perPage=20
GET /api/companies?sort.created_at=desc&sort.name=asc
GET /api/companies?name.like=air&created_at.between=2024-01-01,2024-12-31
GET /api/companies?keyword=acme&is_active=true
Rename any parameter per action:
list=ListAction(query_param_names={"page": "p", "per_page": "pp", "sort_prefix": "order.", "keyword": "q"})
Response format
{
"data": [ { "id": 1, "name": "Acme Air" } ],
"errors": [],
"success": true,
"pagination": {
"page": 2, "perPage": 25, "totalItemsCount": 137, "totalPagesCount": 6,
"isHavingNextPage": true, "isHavingPreviousPage": true
},
"filters": [ { "field": "name", "op": "like", "value": "air", "param": "name.like" } ],
"sorting": [ { "field": "created_at", "order": "DESC" } ]
}
Details, create, update and save return { "data": {...}, "errors": [], "success": true }. Delete returns
{ "data": { "message": "Successfully deleted" }, ... }. Validation errors return HTTP 400:
{ "data": null, "success": false, "errors": [ { "type": "unique", "field": "email", "message": "email must be unique" } ] }
A database constraint violation (duplicate value in a unique column, missing related record) returns HTTP
409 with {"type": "conflict"} and rolls back the request.
Override the format globally with CrudmanOptions(response_formatter=fn) or per action with
response_handler=fn.
Hooks
All hooks are optional and may be async. The subject comes first and ctx last.
async def only_my_company(ctx): # on_before_action: return False to stop
if not ctx.identity.get("company_id"):
return False
async def active_only(stmt, ctx): # on_before_query: receives a SQLAlchemy Select
return stmt.where(Company.is_active.is_(True))
async def add_flag(data, ctx): # on_after_fetch
return data
def final_rules(rules, ctx): # get_final_validation_rules
return {**rules, "name": {"type": "string", "min": 3, "max": 100}}
async def after_validate(errors, ctx): ... # on_after_validate: return False to stop
async def stamp(result, ctx): # on_after_action: return value replaces the response
return {**result, "servedAt": datetime.utcnow().isoformat()}
Execution order:
- list / details:
on_before_action→ relations → cache lookup →on_before_query→ fetch →on_after_fetch→ format →on_after_action→ cache store - create / update / save:
on_before_action→ uploads → rules →get_final_validation_rules→on_before_validate→ validate → uniqueness →on_after_validate→ write (transaction) → format →on_after_action→ cache invalidation - delete:
on_before_action→ delete → format →on_after_action→ cache invalidation
Context (ctx)
Every hook receives ctx: service, session, repository, section, action, model, request,
identity, params, query, body, files, plus anything you add:
Section(context={"services": {"mailer": Depends(get_mailer)}, "repositories": {"audit": AuditLog}})
async def build_ctx(ctx):
return {"tenant_id": ctx.request.headers.get("x-tenant-id")}
Section(context=build_ctx)
Custom routes
from crudman import crud, CrudmanService, get_crud
crud.register(Company) # section without auto routes
@router.get("/companies/list")
@crud.list("companies")
async def list_companies(): ...
@router.post("/countries/{country_id}/states")
async def create_state(country_id: int, body: dict, svc: CrudmanService = Depends(get_crud)):
r = await svc.call_action("states", "save", body={**body, "country_id": country_id})
return r.to_response()
File uploads
crud_router(Profile, config={"uploadable": {"avatar": "image-avatar"}, "upload_defaults": {"storage": "local"}})
Accepts multipart (avatar=@me.png) or JSON base64 ({"avatar": "data:image/png;base64,..."}).
Shorthand suffixes: image[] (array), image:base64, binary:blob. Storage modes: filename,
filename_in_field, base64, blob. Presets: image, image-jpg|png|gif|webp|avif, image-avatar,
video, audio, pdf, doc, spreadsheet, csv, text, xml, html, json, archive, binary,
any.
Caching
CrudmanOptions(cache=CacheOptions(enabled=True, ttl=60, invalidate_lists_on_write=True))
ListAction(enable_cache={"ttl": 30})
Writes invalidate only the written section's cache entries. For a shared cache, pass
CacheOptions(enabled=True, backend=RedisCache(url="redis://...")) (needs the redis extra; not yet tested
against a live server).
Import / Export
x-content-type: csv(orAccept: text/csv, or?format=csv) on list/details returns CSV; pagination goes intoX-Pagination-Total,X-Pagination-Page,X-Pagination-PerPageheaders.x-content-type: excelreturns.xlsx(needs the[excel]extra).POST /{section}/bulk/import(JSON or CSV) andPOST /{section}/bulk/delete, opt-in viaactions=.
Security
0.1.1 fixes issues found in a security review of 0.1.0; upgrade if you use 0.1.0. Highlights:
- Hidden fields stay hidden: they cannot be filtered, sorted or searched, and related records follow their own section's read attributes.
- Uploads: storage keys cannot leave the storage folder, file columns change only through uploads, image content is verified, and executable/HTML extensions are rejected.
- Cache keys include the caller's identity; exports escape spreadsheet formulas.
- Limits:
perPagemax 100,paginate=falseoff by default,max_body_mb,max_file_mb,max_bulk_rows.
By default clients can write every column except the primary key and timestamps, so restrict sensitive columns:
attributes={"write": {"exclude": ["role", "is_admin"]}}. See the
security docs and CHANGELOG.
Coming from crudman-nestjs
| crudman-nestjs | crudman-fastapi |
|---|---|
@Controller('api/companies') |
@Controller("/api/companies") |
@UseCrud({ sections: { companies: { model: Company } } }) + CrudControllerBase('companies') |
@Crud(Company) on the class, or crud_router(Company) |
@CrudList('companies') on a controller method |
@CrudList() |
guards (@UseGuards(...)) |
@Private(), @Roles(...), @Public() |
CrudmanModule.forRoot({...}) |
Crudman.init(app, CrudmanOptions(...)) |
@CrudList('companies') on a function |
@crud.list("companies") |
filtersWhitelist, onBeforeAction, … |
filters_whitelist, on_before_action, … |
onBeforeQuery(findOptions, ...) |
on_before_query(select_stmt, ctx) |
| fastest-validator rules | Pydantic, or the same rule dicts in get_final_validation_rules |
UseCrud({ models: [Tag] }) (pluralised class name) |
crud_router([Tag]) (table name) |
Example app
A runnable FastAPI + SQLAlchemy + SQLite app lives in
examples/fastapi_sqlalchemy_sqlite:
cd examples/fastapi_sqlalchemy_sqlite
pip install -r requirements.txt
python seed.py
uvicorn app.main:app --reload # http://localhost:8000/docs
Roadmap
| Phase | Scope | Status |
|---|---|---|
| 1–6 | Query parsing, SQLAlchemy adapter, validation, routing + OpenAPI, hooks/context, access control | ✅ |
| 7 | Cache (memory; Redis adapter untested against a live server) | ✅ |
| 8 | Uploads (local disk; S3 adapter untested against S3) | ✅ |
| 9 | CSV/Excel export, bulk import/delete | ✅ |
| 10 | Example app, docs | ✅ |
| next | PyPI release; sync sessions; model specs without an ORM class | ⏳ |
Development
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest # unit + end-to-end tests (SQLite)
CRUDMAN_TEST_DATABASE_URL=postgresql+asyncpg://localhost/crudman_test pytest # same suite on PostgreSQL
ruff check src tests
mypy src
cd website && npm install && npm start # docs at http://localhost:3000
License
MIT
Metadata
Release files for crudman-fastapi 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| crudman_fastapi-0.2.2.tar.gz | 98.2 kB | With provenance | Details for crudman_fastapi-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| crudman_fastapi-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for crudman_fastapi-0.2.2-py3-none-any.whl |
Total release size: 169.9 kB
Release files / crudman_fastapi-0.2.2.tar.gz
| Download URL | crudman_fastapi-0.2.2.tar.gz |
|---|---|
| Size | 98.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2971c81a9c102543afd8ab735901209b78a177831c288bacfdb750f9e8f63496
|
|
BLAKE2b-256 checksum How to use checksums |
5cdb5c70b33cfb88f01717f751b444290751f8a129c87b0fcb3f0351d310b07a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logRelease files / crudman_fastapi-0.2.2-py3-none-any.whl
| Download URL | crudman_fastapi-0.2.2-py3-none-any.whl |
|---|---|
| Size | 71.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8ec7ce0df3d81ec3f57b0191671a881f40ca02abadde9a2b337c179155d3dac6
|
|
BLAKE2b-256 checksum How to use checksums |
86c37047c2db0c17fe9ec948cfa9f590dc4d123ca9f8fffca6ec71c16b0df9e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logSecurity for crudman-fastapi 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for crudman_fastapi-0.2.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.