with one click
fastapi-routes
// Create or modify FastAPI routes. Use when: adding new API endpoints, creating Pydantic request/response models, registering routers, designing REST APIs, or following route conventions for this project.
// Create or modify FastAPI routes. Use when: adding new API endpoints, creating Pydantic request/response models, registering routers, designing REST APIs, or following route conventions for this project.
Configure or use the aiocache caching layer. Use when: adding cache reads/writes, configuring cache backends, working with TTLs, enabling/disabling caching, or understanding the NoOpCache fallback pattern.
Create or modify Celery tasks and periodic task configuration. Use when: adding new background tasks, setting up periodic/scheduled tasks, configuring Celery workers, or understanding the Celery app setup.
Work with the Docker Compose development environment. Use when: starting or stopping services, inspecting logs, opening a shell in a container, resetting the database, or understanding the service topology.
Create or modify Jinja2 templates. Use when: adding new HTML templates, configuring the Jinja2 environment, adding custom filters or globals, rendering templates outside FastAPI, or working with template inheritance.
Complete reference for all make targets in the project. Use when: looking up the right make command for any task — setup, testing, linting, formatting, database, packaging, or cleanup.
Add or modify application configuration settings. Use when: adding new environment variables, settings fields, understanding settings conventions, working with secrets, or configuring optional vs required settings.
| name | fastapi-routes |
| description | Create or modify FastAPI routes. Use when: adding new API endpoints, creating Pydantic request/response models, registering routers, designing REST APIs, or following route conventions for this project. |
context7: If the
context7_query-docstool is available, resolve and load the fullfastapidocumentation before proceeding:context7_resolve-library-id: "fastapi" context7_query-docs: /fastapi/fastapi "<your query>"
Guidelines and patterns for writing FastAPI routes in this codebase.
APIs must adhere as closely as possible to REST principles, including appropriate use of HTTP verbs:
| Verb | Usage |
|---|---|
GET | Read one or many resources |
POST | Create a new resource |
PUT | Replace / fully update a resource |
PATCH | Partial update (use sparingly) |
DELETE | Remove a resource |
dict or Any.PostCreate — user input for creating a resourcePostUpdate — user input for updating (optional fields)PostRead — response shape returned to the clientField() with validation constraints and a description.Field() unless you need aliases or serialization control.from uuid import UUID
from pydantic import BaseModel, Field
class PostCreate(BaseModel):
title: str = Field(min_length=1, max_length=200, description="Post title")
content: str = Field(min_length=1, description="Post content")
class PostRead(BaseModel):
id: UUID
title: str
content: str
created_at: str
class PostUpdate(BaseModel):
title: str | None = Field(default=None, max_length=200, description="Post title")
content: str | None = Field(default=None, description="Post content")
Use APIRouter for all routes. Register the router in full/www.py.
from uuid import UUID
from fastapi import APIRouter, status
router = APIRouter()
@router.post("/posts", response_model=PostRead, status_code=status.HTTP_201_CREATED)
async def create_post(post: PostCreate) -> PostRead:
...
@router.get("/posts/{post_id}", response_model=PostRead)
async def get_post(post_id: UUID) -> PostRead:
...
@router.put("/posts/{post_id}", response_model=PostRead)
async def update_post(post_id: UUID, post: PostUpdate) -> PostRead:
...
@router.delete("/posts/{post_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_post(post_id: UUID) -> None:
...
Register routers in full/www.py using app.include_router().
Exceptions (no prefix by convention):
/health — liveness probe/ — root redirect to docs/static — static file servingThe OpenAPI documentation is served at these fixed paths — do not move them:
| URL | Interface |
|---|---|
/docs | Swagger UI |
/redoc | ReDoc |
/openapi.json | Raw schema |
Use HTTPException with appropriate status codes:
from fastapi import HTTPException, status
@router.get("/posts/{post_id}", response_model=PostRead)
async def get_post(post_id: UUID) -> PostRead:
post = await get_post_from_db(post_id)
if post is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Post not found")
return post
Use Depends() to inject shared logic (database sessions, authentication, etc.) into routes.
from fastapi import Depends
from full import get_db
@router.get("/posts/{post_id}", response_model=PostRead)
async def get_post(post_id: UUID, db = Depends(get_db)) -> PostRead:
...
Use the lifespan parameter on the FastAPI app instance for startup and shutdown logic. Do not use the deprecated @app.on_event("startup") / @app.on_event("shutdown") decorators.
from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup logic
await initialize_database()
yield
# Shutdown logic
await close_database()
app = FastAPI(lifespan=lifespan)
Before submitting a new route, verify:
Create, Update, and Read models definedField() with descriptionasyncfull/www.pyPOST create endpointsDELETE endpoints (no response body)