FastAPI REST API Design
Use this skill when designing, implementing, or reviewing FastAPI REST endpoints.
Purpose and scope
This skill enforces practical REST design and FastAPI implementation quality: resource naming, HTTP semantics, schema quality,
dependency boundaries, security checks, and concise review output.
Core design rules
Resource and path conventions
- Use nouns in paths, not action verbs.
- Use plural collection names such as
/projects, /users.
- Use stable item identifiers such as
/projects/{project_id}.
- Keep names lowercase and consistent.
- Keep nesting shallow (typically max 2 levels), for example
/projects/{project_id}/models.
- Do not expose internal storage structure in public routes.
HTTP methods and status semantics
- Use
GET read, POST create, PUT full replace, PATCH partial update, DELETE remove.
- Return
201 on create, 200 on standard success, and 204 when no response body is needed.
- Choose precise error codes (
400, 401, 403, 404, 409, 422, 500) and avoid vague fallbacks.
- Keep semantics consistent across related endpoints.
Request/response contracts
- Use JSON payloads for standard API requests/responses.
- Use Pydantic models for request and response contracts.
- Enforce explicit field constraints (enums, lengths, ranges, formats).
- Prefer explicit response models for stable contracts.
- Keep error response bodies consistent across endpoints.
FastAPI architecture patterns
- Organize routes by resource/domain with
APIRouter.
- Inject dependencies with
Depends(...); avoid hidden globals.
- Keep handlers thin and move business logic into services/use-cases.
- Raise domain exceptions in service layer and map to HTTP errors at API boundary.
- Add explicit
responses={...} metadata when custom errors must appear in OpenAPI docs.
Security and operability
- Enforce HTTPS in deployed environments.
- Enforce authentication and authorization per route/use case.
- Apply least-privilege checks for each resource operation.
- Avoid exposing sensitive internals in error details.
- Add filtering, sorting, and pagination for large collection endpoints.
- Introduce versioning (for example
/v1/...) before shipping breaking API changes.
Review workflow
- Classify each endpoint as collection, item, or nested resource.
- Verify verb-to-action mapping.
- Verify status code and error semantics.
- Verify Pydantic schema quality and response model clarity.
- Verify DI and layer boundaries.
- Verify authn/authz and least-privilege behavior.
- Apply checklist and report material issues first.
Output style
When asked to design or review APIs, respond concisely with:
- Endpoint proposal (route + method list).
- Contract notes (request/response + validation).
- Security checks (authn/authz + sensitive data handling).
- Checklist verdict (pass/fail highlights).
- Top fixes in priority order.
Additional resources
- Checklist: REST_API_CHECKLIST.md
1---2name: fastapi-rest-api-design3description: Designs and reviews REST APIs for FastAPI services using consistent resource naming, HTTP semantics, validation, security, and error handling patterns. Use for backend API tasks, endpoint design/refactors, or API review requests in FastAPI/Python projects.4---56# FastAPI REST API Design78Use this skill when designing, implementing, or reviewing FastAPI REST endpoints.910## Purpose and scope1112This skill enforces practical REST design and FastAPI implementation quality: resource naming, HTTP semantics, schema quality,13dependency boundaries, security checks, and concise review output.1415## Core design rules1617### Resource and path conventions1819- Use nouns in paths, not action verbs.20- Use plural collection names such as `/projects`, `/users`.21- Use stable item identifiers such as `/projects/{project_id}`.22- Keep names lowercase and consistent.23- Keep nesting shallow (typically max 2 levels), for example `/projects/{project_id}/models`.24- Do not expose internal storage structure in public routes.2526### HTTP methods and status semantics2728- Use `GET` read, `POST` create, `PUT` full replace, `PATCH` partial update, `DELETE` remove.29- Return `201` on create, `200` on standard success, and `204` when no response body is needed.30- Choose precise error codes (`400`, `401`, `403`, `404`, `409`, `422`, `500`) and avoid vague fallbacks.31- Keep semantics consistent across related endpoints.3233### Request/response contracts3435- Use JSON payloads for standard API requests/responses.36- Use Pydantic models for request and response contracts.37- Enforce explicit field constraints (enums, lengths, ranges, formats).38- Prefer explicit response models for stable contracts.39- Keep error response bodies consistent across endpoints.4041### FastAPI architecture patterns4243- Organize routes by resource/domain with `APIRouter`.44- Inject dependencies with `Depends(...)`; avoid hidden globals.45- Keep handlers thin and move business logic into services/use-cases.46- Raise domain exceptions in service layer and map to HTTP errors at API boundary.47- Add explicit `responses={...}` metadata when custom errors must appear in OpenAPI docs.4849### Security and operability5051- Enforce HTTPS in deployed environments.52- Enforce authentication and authorization per route/use case.53- Apply least-privilege checks for each resource operation.54- Avoid exposing sensitive internals in error details.55- Add filtering, sorting, and pagination for large collection endpoints.56- Introduce versioning (for example `/v1/...`) before shipping breaking API changes.5758## Review workflow59601. Classify each endpoint as collection, item, or nested resource.612. Verify verb-to-action mapping.623. Verify status code and error semantics.634. Verify Pydantic schema quality and response model clarity.645. Verify DI and layer boundaries.656. Verify authn/authz and least-privilege behavior.667. Apply checklist and report material issues first.6768## Output style6970When asked to design or review APIs, respond concisely with:71721. Endpoint proposal (route + method list).732. Contract notes (request/response + validation).743. Security checks (authn/authz + sensitive data handling).754. Checklist verdict (pass/fail highlights).765. Top fixes in priority order.7778## Additional resources7980- Checklist: [REST_API_CHECKLIST.md](REST_API_CHECKLIST.md)