| name | fastapi-templates |
| description | Production-grade FastAPI project creation and setup guide. Includes async patterns, DI, and error handling. Triggers: "FastAPI", "Python API project", "FastAPI project creation", "FastAPI template", "FastAPI setup" Anti-triggers: "Django", "Flask", "Node.js API", "Express", "modifying existing FastAPI only"
|
| user-invocable | false |
FastAPI Project Templates
Production-ready FastAPI project structures with async patterns, dependency injection, middleware, and best practices for building high-performance APIs.
When to Use This Skill
- Starting new FastAPI projects from scratch
- Implementing async REST APIs with Python
- Building high-performance web services and microservices
- Creating async applications with PostgreSQL, MongoDB
- Setting up API projects with proper structure and testing
Core Concepts
1. Project Structure
Recommended Layout:
app/
├── api/ # API routes
│ ├── v1/
│ │ ├── endpoints/
│ │ │ ├── users.py
│ │ │ ├── auth.py
│ │ │ └── items.py
│ │ └── router.py
│ └── dependencies.py # Shared dependencies
├── core/ # Core configuration
│ ├── config.py
│ ├── security.py
│ └── database.py
├── models/ # Database models
│ ├── user.py
│ └── item.py
├── schemas/ # Pydantic schemas
│ ├── user.py
│ └── item.py
├── services/ # Business logic
│ ├── user_service.py
│ └── auth_service.py
├── repositories/ # Data access
│ ├── user_repository.py
│ └── item_repository.py
└── main.py # Application entry
2. Dependency Injection
FastAPI's built-in DI system using Depends:
- Database session management
- Authentication/authorization
- Shared business logic
- Configuration injection
3. Async Patterns
Proper async/await usage:
- Async route handlers
- Async database operations
- Async background tasks
- Async middleware
Implementation Patterns
Pattern 1: Complete FastAPI Application
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events."""
await database.connect()
yield
await database.disconnect()
app = FastAPI(
title="API Template",
version="1.0.0",
lifespan=lifespan
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
from app.api.v1.router import api_router
app.include_router(api_router, prefix="/api/v1")
Resources
- references/fastapi-architecture.md: Detailed architecture guide
- references/async-best-practices.md: Async/await patterns
- references/testing-strategies.md: Comprehensive testing guide
Best Practices
- Async All The Way: Use async for database, external APIs
- Dependency Injection: Leverage FastAPI's DI system
- Repository Pattern: Separate data access from business logic
- Service Layer: Keep business logic out of routes
- Pydantic Schemas: Strong typing for request/response
- Error Handling: Consistent error responses
- Testing: Test all layers independently
Common Pitfalls
- Blocking Code in Async: Using synchronous database drivers
- No Service Layer: Business logic in route handlers
- Missing Type Hints: Loses FastAPI's benefits
- Ignoring Sessions: Not properly managing database sessions
- No Testing: Skipping integration tests
- Tight Coupling: Direct database access in routes