fastapi-clean-architecture

Generate FastAPI project structures with Clean Architecture layers.

1|Updated Aug 4, 2025
One-click install
npx skills add https://github.com/rafaelkamimura/claude-config --skill fastapi-clean-architecture
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-clean-architecture
Source: https://github.com/rafaelkamimura/claude-config/tree/main/skills/fastapi-clean-architecture
Command: npx skills add https://github.com/rafaelkamimura/claude-config --skill fastapi-clean-architecture

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires fastapi, sqlalchemy, pydantic, dependency-injector, uvicorn.

What problem does it solve?

This Skill provides a structured approach to building FastAPI applications using Clean Architecture, solving problems of maintainability, testability, and scalability by enforcing clear separation of concerns and robust dependency management. It helps developers create enterprise-grade backend services that are easy to evolve and debug.

Core Features & Use Cases

  • Three-Layer Architecture: Organizes code into API, Domain, and Infrastructure layers, ensuring clear responsibilities and preventing tight coupling.
  • Dependency Injection Container: Utilizes dependency-injector to manage and inject dependencies, making components easily swappable and testable.
  • Repository & Service Patterns: Implements abstract interfaces for data access and business logic in the domain, with concrete implementations in the infrastructure, promoting a clean separation from persistence details.
  • Comprehensive Testing Strategy: Guides on unit, service, and integration testing, ensuring high code quality and reliability across all layers.
  • Use Case: Design and implement a new microservice for user management, ensuring it adheres to enterprise-grade architectural standards, is fully testable, and can easily integrate with different databases or external services without major refactoring.

Quick Start

Generate a new FastAPI project structure following Clean Architecture, including src/api, src/domain, src/infra directories, and a basic user module with repository and service interfaces.

Frequently Asked Questions about fastapi-clean-architecture

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

FAQPage Schema
How do I structure a FastAPI application with Clean Architecture principles?

Clean Architecture for FastAPI separates code into three layers—API, Domain, and Infrastructure—each with distinct responsibilities. This enforces clear boundaries, prevents tight coupling, and makes your backend maintainable and testable as it scales.

Can I use dependency injection with FastAPI to make components testable and swappable?

Yes. The dependency-injector library integrates with FastAPI to manage and inject dependencies across your application, letting you swap implementations without changing calling code and mock dependencies easily during testing.

How do I implement repository and service patterns in FastAPI for data access and business logic?

Define repository and service interfaces in your Domain layer, then provide concrete implementations in the Infrastructure layer that handle database queries and business operations. This isolates persistence details and keeps your domain logic database-agnostic.

What's the best way to design a FastAPI microservice that works with multiple data sources?

Use Clean Architecture with repository abstractions to interface with different sources—PostgreSQL, Redis, external APIs—through the Infrastructure layer. Abstract implementations behind domain interfaces so switching data sources requires no domain or API layer changes.

Do I need to set up testing strategies differently with Clean Architecture in FastAPI?

Clean Architecture simplifies testing by isolating layers. Mock repository implementations in unit tests, test services independently, and run integration tests against real or in-memory databases. The separation makes each layer testable in isolation.

Can Clean Architecture help with refactoring an existing FastAPI codebase?

Yes. Incrementally extract domain logic, repositories, and services into separate layers while keeping your API endpoints as thin adapters. This reduces technical debt and makes future changes safer and faster.