fastapi-architecture

Guides structuring and refactoring FastAPI applications with routers, services, and dependency injection.

1|Updated Apr 15, 2026
One-click install
npx skills add https://github.com/pnewsam/skills --skill fastapi-architecture-pnewsam
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: fastapi-architecture
Source: https://github.com/pnewsam/skills/tree/main/archive/python-evicted/fastapi-architecture
Command: npx skills add https://github.com/pnewsam/skills --skill fastapi-architecture-pnewsam

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? FastAPI applications become hard to maintain as they grow: route handlers accumulate business logic, database access, and validation in one place, making testing and refactoring risky. This Skill provides architecture guidance to keep handlers thin, boundaries explicit, and business logic testable. ## Core Features & Use Cases - Layered Boundaries: Defines clear responsibilities for main.py, APIRouter modules, Pydantic schemas, services, repositories, and dependency injection with Annotated types. - Refactor Guidance: Gives concrete rules for extracting services from fat handlers, centralizing settings with pydantic-settings, managing async database boundaries, and converting domain errors to HTTP responses at the API boundary. - Testing Patterns: Shows how to test services without HTTP and override dependencies in tests instead of monkeypatching globals. - Use Case: When a router module mixes several domains and handlers contain business rules, use this Skill to plan a refactor that extracts an InvoiceService while preserving paths, status codes, and response shapes. ## Quick Start Ask the agent to review and refactor a FastAPI router module so handlers stay thin and business logic moves into a testable service layer.

Frequently Asked Questions about fastapi-architecture

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

FAQPage Schema
How do I structure a FastAPI project for multiple domains?▼

Organize by domain or feature rather than technical type: keep routers under api/routes, and give each domain its own schemas.py, service.py, and repository.py. Follow the existing project convention first and avoid reorganizing the whole app when a local extraction solves the problem.

How to refactor fat FastAPI route handlers into services?▼

Move business rules out of route handlers into a service class before splitting routers, then inject the service with Annotated and Depends. The handler should only describe the API boundary while the service owns the workflow, keeping paths and response shapes unchanged.

Should I use FastAPI dependency injection for database sessions?▼

Yes, declare request-scoped concerns like sessions, auth, and settings with Annotated[Type, Depends(...)] and reuse them via type aliases such as SessionDep. This makes dependencies explicit and lets tests override them instead of monkeypatching globals.

When is a service layer unnecessary in FastAPI?▼

A small app can have just routers and schemas without a full service/repository split until business logic justifies it. Avoid premature layering; introduce services when handlers start mixing business rules, coordination, or transactions.

How do I handle domain errors in FastAPI services?▼

Raise typed domain exceptions from service code and convert them to HTTP responses at the API boundary with centrally registered exception handlers. Avoid raising HTTPException from deep service code so business logic stays independent of HTTP.