Backend Router.mdc
Patterns for FastAPI router files
Loading actions...
Skill content
Main instructions and any bundled files for this skill.
Backend Router Patterns
-
Pydantic models in file — define request/response
BaseModelclasses at the top of the router file, not inmodels.py.models.pyis for SQLModel table definitions only. -
Every endpoint has
response_model=— return the Pydantic response object, never a SQLModel row directly. -
DB dependency —
db: Annotated[Session, Depends(get_session)]. -
Admin protection —
_: Annotated[None, Depends(require_admin)]as a parameter (underscore name — not used in body). -
404 pattern:
obj = db.get(Model, id) if not obj: raise HTTPException(status_code=404, detail="X not found") -
409 for duplicates —
HTTPException(status_code=409, detail="...")when a unique constraint would be violated. -
Background tasks — accept
BackgroundTasks, callbackground_tasks.add_task(fn, arg1, arg2). The task function must open its ownSession(engine)and import engine locally to avoid circular imports. -
New router file — must be imported in
main.pyand registered withapp.include_router(x.router, prefix="/api"). -
Path conventions — resource paths:
/modules/{module_id}/resourcefor nested,/resource/{id}for top-level. Use plural nouns. -
Status codes —
201for POST create,202for accepted/async,204for DELETE.
Related Skills
Frontend Typescript Linting.mdc
TypeScript and ESLint rules that MUST be followed when creating, modifying, or reviewing any file under apps/frontend/, including .ts, .tsx, .js, and .jsx files. Also apply when discussing frontend li...
2. Apply Deepthink Protocol (reason about dependencies
risks