fastapi-patterns

Guides writing and reviewing FastAPI routers, schemas, dependencies, services, and async handlers.

Updated Aug 3, 2026
One-click install
npx skills add https://github.com/m-de-graaff/skills --skill fastapi-patterns-m-de-graaff
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: fastapi-patterns
Source: https://github.com/m-de-graaff/skills/tree/main/skills/fastapi-patterns
Command: npx skills add https://github.com/m-de-graaff/skills --skill fastapi-patterns-m-de-graaff

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? FastAPI codebases often accumulate business logic inside route handlers, leak ORM fields through missing response models, block the event loop with sync calls in async handlers, and rely on outdated libraries like passlib and python-jose. This Skill provides reviewed patterns for structuring FastAPI applications so handlers stay thin, security pitfalls are avoided, and tests can override dependencies cleanly. ## Core Features & Use Cases - Project structure and app factory: Standard layout with routers, schemas, services, and models, plus a create_app() factory and lifespan management so tests can build fresh apps with overridden dependencies. - Security-correct auth patterns: Separate 401/403 dependencies, explicit JWT algorithms lists, SecretStr settings, bcrypt password hashing with awareness of the 72-byte truncation limit, and database-enforced uniqueness via IntegrityError. - Async discipline and testing: Rules for avoiding blocking calls in async handlers, pagination with mandatory ORDER BY and capped limits, and pytest fixtures using httpx ASGITransport with dependency_overrides. - Use Case: When a route handler starts hashing passwords and committing ORM objects directly, apply this Skill to extract a service layer, add input/output Pydantic schemas, and wire authentication through composable Annotated dependency aliases. ## Quick Start Review my FastAPI users router and refactor it to use a service layer, separate input and output schemas, and proper JWT authentication dependencies.

Frequently Asked Questions about fastapi-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a FastAPI project with routers and services?▼

Use a layout with separate directories for routers, schemas, models, and services, plus a create_app() factory that wires middleware and routers. Route handlers should only translate HTTP and delegate business logic and transactions to a service layer.

How do I implement JWT authentication in FastAPI?▼

Use OAuth2PasswordBearer with a get_current_user dependency that decodes the token via PyJWT with an explicit algorithms list. Keep authentication (401) and authorization (403) in separate dependencies, and store the secret key as a SecretStr in pydantic-settings.

Should I use passlib or bcrypt for FastAPI password hashing?▼

Use bcrypt and PyJWT directly. passlib has had no release since 2020 and breaks against bcrypt 4.1 and later, while python-jose is unmaintained. Note that bcrypt silently truncates passwords at 72 bytes, so bound input length or pre-hash with SHA-256.

Why does my async FastAPI endpoint block other requests?▼

A synchronous blocking call inside an async def handler blocks the entire event loop, stalling every in-flight request. Use an AsyncSession for database work, define the handler as plain def so FastAPI runs it in a threadpool, or wrap blocking calls in run_in_threadpool.

Does FastAPI support the HTTP QUERY method?▼

FastAPI does not support QUERY; there is no @router.query() and OpenAPI 3.1 schema generation breaks on it. You can route it via api_route with include_in_schema=False, but you should ship a POST endpoint as the real route since browsers and gateways often reject QUERY.

How do I test FastAPI endpoints without running a server?▼

Use httpx AsyncClient with ASGITransport to run the app in-process, and override get_db via app.dependency_overrides so tests control the database transaction. Layer fixtures from db_session through client to auth_client so authenticated tests request a single fixture.