Back to Skills

fastapi-patterns

FastAPI best practices covering project structure, Pydantic v2 schemas, dependency injection, async handlers, authentication, authorization, transactional service layers, and testing with httpx and pytest.

227,210stars34,736forksUpdated 7/8/2026

Security Assessment

Medium Risk(50/100)

Detected risks:

Secret Exposure([SKILL.md] token =, [SKILL.md] password=)
Sensitive File Access([SKILL.md] .env)
Security Score50/100

About fastapi-patterns

fastapi-patterns is a reference for modern, production-grade FastAPI development, laying out a layered project structure and the conventions that go with it. The suggested layout separates an application factory and lifespan in main.py, settings via pydantic-settings, shared dependencies, an async SQLAlchemy database module, and dedicated routers, models, schemas, and services packages, alongside a tests directory. The application factory attaches CORS middleware and includes routers, and its lifespan creates tables on startup for dev or demo convenience while recommending Alembic migrations for strict production, disposing the engine on shutdown.

Data shapes use Pydantic v2: EmailStr and Field constraints, a model validator that runs after parsing to confirm matching passwords, and model configuration with from_attributes for ORM serialization. Dependency injection is central, with an async get_db session that rolls back on exceptions, OAuth2PasswordBearer paired with a get_current_user dependency that decodes a JWT via python-jose, a get_current_active_user layer, and Annotated aliases (DbDep, CurrentUserDep, ActiveUserDep) that keep endpoint signatures clean.

Router design is demonstrated through user endpoints: creation returning 201, a /me endpoint, paginated listing with Query bounds, an update guarded by an authorization check, and a /token login using OAuth2PasswordRequestForm. Business logic lives in a service layer that raises domain errors such as DuplicateUserError, which routers translate into HTTP responses. The stated scope also includes authorization, transactional service methods, and testing with httpx and pytest, making it a broad checklist for structuring a FastAPI service well.

FAQ

Which FastAPI topics does this cover?

Project structure, Pydantic v2 schemas, dependency injection, async handlers, authentication and authorization, transactional service layers, and testing with httpx and pytest.

What version of Pydantic does it use?

Pydantic v2. Schemas use EmailStr, Field constraints, a model validator that runs after parsing, and model_config with from_attributes for ORM serialization.

How is authentication implemented?

With OAuth2PasswordBearer and JWTs decoded via python-jose in a get_current_user dependency, layered with get_current_active_user, and a /token login endpoint using OAuth2PasswordRequestForm.

How does it structure database access?

An async SQLAlchemy session provided by a get_db dependency that rolls back on exceptions, with business logic in a service layer such as UserService that raises DuplicateUserError.

How are database tables created?

A lifespan handler creates tables on startup for dev and demo convenience, but the skill recommends managing schemas with Alembic migrations for strict production.

Install fastapi-patterns

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill