fastapi-layered-architecture

Enforce Router, Service, and Repository layers in FastAPI projects.

Updated Jan 24, 2026
One-click install
npx skills add https://github.com/amaozhao/MySkills --skill fastapi-layered-architecture
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-layered-architecture
Source: https://github.com/amaozhao/MySkills/tree/main/skills/fastapi-layered-architecture
Command: npx skills add https://github.com/amaozhao/MySkills --skill fastapi-layered-architecture

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill addresses the common issue of tangled FastAPI code by enforcing a strict separation of Router, Service, and Repository layers.

Core Features & Use Cases

  • Router: Parse requests and validate with Pydantic
  • Service: Implement business logic and coordinate commits
  • Repository: Build SQL queries and return model instances
  • Use Case: Starting a new production FastAPI project with clean layering

Quick Start

Create the directory structure as shown: app/api/endpoints (Router), app/services (Service), app/repositories (Repository), app/models (DB models), app/schemas (Pydantic models). Then start the app with uvicorn main:app --reload.

Frequently Asked Questions about fastapi-layered-architecture

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

FAQPage Schema
How do I separate business logic from SQL queries in FastAPI?

To separate business logic from SQL queries in FastAPI, enforce a layered architecture where the Service layer handles business logic and coordinates commits, while the Repository layer builds SQL queries and returns model instances. This strict separation prevents tangled code and ensures endpoints do not mix concerns.

What is the best way to structure a new production FastAPI project?

The best way to structure a production FastAPI project is using a strict Layered Architecture with separate directories for api/endpoints, services, repositories, models, and schemas. This enforces clear boundaries between Router, Service, and Repository layers for maintainability.

Why does my FastAPI endpoint mix request parsing with database operations?

FastAPI endpoints mix request parsing with database operations when a strict layered architecture is not enforced. By delegating request parsing and Pydantic validation to the Router, business logic to the Service, and SQL queries to the Repository, you establish clear boundaries and eliminate tangled code.

Do I need external tools to enforce layered architecture in FastAPI?

You do not need external tools to enforce layered architecture in FastAPI. This approach requires no external tooling beyond standard Python FastAPI practices, relying entirely on structural separation into Router, Service, and Repository layers to guide teams toward maintainability.

Can I refactor an existing FastAPI app to use Router Service Repository layers?

You can refactor an existing FastAPI app to use Router, Service, and Repository layers. This Skill applies to refactors where endpoints mix concerns, guiding teams toward clear boundaries by specifying responsibilities for parsing requests, implementing business logic, and building SQL queries.