fastapi-backend-architecture

Enforce layered FastAPI architecture with DTO contracts and transaction lifecycle rules.

1|Updated Feb 21, 2026
One-click install
npx skills add https://github.com/marcius-llmus/skills --skill fastapi-backend-architecture-marcius-llmus
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-backend-architecture
Source: https://github.com/marcius-llmus/skills/tree/main/fastapi-backend-architecture
Command: npx skills add https://github.com/marcius-llmus/skills --skill fastapi-backend-architecture-marcius-llmus

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires sqlalchemy, fastapi, and includes scripts (resource) and references (resource) components.

What problem does it solve?

Projects often suffer from blurred layer responsibilities, transaction leakage, tight coupling between modules, and inconsistent testing boundaries, which lead to brittle backends and hard-to-maintain code.

Core Features & Use Cases

  • Layered Contracts: Enforces route -> service -> repository -> db separation so HTTP handlers remain transport-only and services hold business logic.
  • DTO-Based Service API: Mandates Pydantic DTOs for service inputs and outputs to stabilize contracts and avoid leaking ORM models.
  • Centralized Transaction Management: Confines commit/rollback to the dependency/session manager (get_db) and prescribes flush/refresh semantics in repositories.
  • Module Isolation & Testing Matrix: Prescribes service-to-service cross-module calls, facade/orchestrator patterns for multi-module workflows, and a strict testing mocking matrix (router mocks service, service mocks repo, repo uses real DB).
  • HTMX Guidance (Optional): Provides decorator-based patterns and response semantics when HTMX-driven views are relevant.
  • Use Case: Add a new update endpoint that preserves DTO boundaries, uses provider wiring for service/repository, keeps services transaction-unaware, and includes layered tests as specified.

Quick Start

Read STRUCTURE.md, consult the required references for your change, and ask for route, service, repository, DTOs, dependency providers, and tests that follow the skill's non-negotiable rules.

Frequently Asked Questions about fastapi-backend-architecture

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

FAQPage Schema
How do I separate FastAPI routes, services, and repositories to prevent transaction leakage?

To prevent transaction leakage in a FastAPI backend, enforce a strict layered architecture where HTTP routes remain transport-only, services hold business logic, and repositories interact with the database using flush-and-refresh update semantics while keeping transaction commit and rollback confined to a centralized session manager.

What is the best way to structure FastAPI services to avoid leaking ORM models to the client?

The best way to avoid leaking ORM models in FastAPI services is to mandate Pydantic DTOs for all service inputs and outputs, stabilizing service contracts and ensuring database models remain isolated within the repository layer.

How do I add a new FastAPI endpoint that preserves strict module isolation and DTO boundaries?

To add a FastAPI endpoint while preserving module isolation, use dependency provider wiring for services and repositories, define DTO-based service contracts, keep services transaction-unaware, and include tests following a strict mocking matrix where routers mock services and services mock repositories.

Can I use HTMX with FastAPI while maintaining a strict layered backend architecture?

Yes, you can use HTMX with FastAPI in a layered architecture by applying decorator-based patterns and specific response semantics for HTMX-driven view endpoints, ensuring they remain integrated within the isolated module structure.

Why does my FastAPI backend architecture have blurred layer responsibilities and inconsistent testing boundaries?

FastAPI backends develop blurred layer responsibilities and inconsistent testing boundaries when lacking strict route, service, and repository separation, centralized transaction management, and a defined mocking matrix where router tests mock services and service tests mock repositories.

Do I need SQLAlchemy to manage transactions in a layered FastAPI backend?

Yes, SQLAlchemy is required to manage transactions in this layered FastAPI architecture, specifically by confining the transaction lifecycle—commit and rollback—within a dependency or session manager and prescribing flush-and-refresh semantics inside repositories.