fastapi
FastAPI web framework patterns and best practices. Use when building REST APIs, adding endpoints, or configuring FastAPI application for the Todo backend.
When & Why to Use This Skill
This FastAPI skill provides comprehensive patterns and best practices for building high-performance REST APIs with Python. It streamlines backend development by offering standardized templates for routing, dependency injection, Pydantic data validation, and error handling, ensuring your web applications are scalable, secure, and automatically documented.
Use Cases
- Rapidly bootstrapping a new RESTful backend with a production-ready structure and automatic OpenAPI (Swagger) documentation.
- Implementing secure and scalable dependency injection patterns for database session management and JWT-based user authentication.
- Defining robust request and response schemas using Pydantic models to ensure strict data validation and type safety.
- Configuring CORS middleware and global exception handlers to create resilient and cross-platform compatible web services.
- Optimizing API performance through efficient routing patterns and asynchronous request handling.
| name | fastapi |
|---|---|
| description | FastAPI web framework patterns and best practices. Use when building REST APIs, adding endpoints, or configuring FastAPI application for the Todo backend. |
| allowed-tools | Read, Edit, Write, Glob, Grep, Bash |
FastAPI Skill
Quick Reference
FastAPI is a modern Python web framework for building APIs with automatic documentation.
Basic App Structure
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title="Todo API",
description="REST API for Todo application",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc"
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
async def health_check():
return {"status": "healthy"}
Routing Patterns
from fastapi import APIRouter, HTTPException, Depends, Query
router = APIRouter(prefix="/api/{user_id}/tasks", tags=["tasks"])
@router.get("/")
async def list_tasks(
skip: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=1000),
user_id: str = Depends(get_current_user)
):
"""List tasks with pagination."""
pass
@router.post("/")
async def create_task(
task: TaskCreate,
user_id: str = Depends(get_current_user)
):
"""Create a new task."""
pass
@router.get("/{task_id}")
async def get_task(
task_id: int,
user_id: str = Depends(get_current_user)
):
"""Get a specific task."""
pass
Dependency Injection
from typing import Annotated
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def get_current_user(
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
) -> str:
token = credentials.credentials
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload.get("sub")
# Use in routes
@router.get("/")
async def list_tasks(
user_id: str = Depends(get_current_user),
db: Session = Depends(get_db)
):
pass
Error Handling
from fastapi import HTTPException, status
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail}
)
# Raise errors in routes
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
Request Models (Pydantic)
from pydantic import BaseModel, Field
class TaskCreate(BaseModel):
title: str = Field(..., min_length=1, max_length=200)
description: str | None = Field(None, max_length=1000)
class TaskUpdate(BaseModel):
title: str | None = Field(None, min_length=1, max_length=200)
description: str | None = None
completed: bool | None = None
Response Models
from pydantic import BaseModel
from datetime import datetime
class TaskResponse(BaseModel):
id: int
user_id: str
title: str
description: str | None
completed: bool
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True
@router.get("/", response_model=list[TaskResponse])
async def list_tasks():
pass
Running the Server
uvicorn main:app --reload --port 8000
# Or with hot reload
uvicorn main:app --reload
For Detailed Reference
See REFERENCE.md for:
- Advanced routing patterns
- Middleware configuration
- Testing FastAPI apps
- Performance optimization
- Deployment options