FastAPI modern Python web framework. Covers routing, Pydantic models, dependency injection, and async support. Use when building Python APIs. USE WHEN: user mentions "fastapi", "pydantic", "async python api", "python rest api", asks about "dependency injection python", "python openapi", "python swagger", "async endpoints", "python api validation", "fastapi middleware" DO NOT USE FOR: Django apps - use `django` instead, Flask apps - use `flask` instead, synchronous Python APIs without type hints, GraphQL-only APIs

GitHub
安装命令
npx skhub add claude-dev-suite/fastapi
Markdown
SKILL.md

FastAPI Core Knowledge

Full Reference: See advanced.md for WebSocket integration including connection management, authentication, room management, Pydantic message protocols, heartbeat, Redis pub/sub scaling, and background tasks.

Deep Knowledge: Use mcp__documentation__fetch_docs with technology: fastapi for comprehensive documentation.

Basic Setup

from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel, EmailStr

app = FastAPI(title="My API")

class UserCreate(BaseModel):
    name: str
    email: EmailStr

class User(UserCreate):
    id: int

    class Config:
        from_attributes = True

Route Patterns

@app.get("/users", response_model=list[User])
async def get_users(skip: int = 0, limit: int = 100):
    return await db.users.find_many(skip=skip, limit=limit)

@app.get("/users/{user_id}", response_model=User)
async def get_user(user_id: int):
    user = await db.users.find(user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    return user

@app.post("/users", response_model=User, status_code=201)
async def create_user(user: UserCreate):
    return await db.users.create(user.model_dump())

Dependency Injection

async def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = await verify_token(token)
    if not user:
        raise HTTPException(status_code=401, detail="Invalid token")
    return user

@app.get("/me", response_model=User)
async def get_me(user: User = Depends(get_current_user)):
    return user

Key Features

  • Auto OpenAPI/Swagger docs at /docs
  • Pydantic validation
  • Async support
  • Type hints everywhere

When NOT to Use This Skill

  • Django projects - Django has its own ORM, admin, templates
  • Flask microservices - Flask is lighter without type validation overhead
  • Synchronous WSGI apps - FastAPI is async-first
  • Legacy Python 2.x - FastAPI requires Python 3.7+
  • Non-REST APIs - Use dedicated GraphQL or gRPC frameworks

Anti-Patterns

Anti-PatternWhy It's BadSolution
def instead of async defBlocks event loopUse async def for I/O operations
Missing response_modelNo output validationAlways specify response_model
Sync database callsBlocks workersUse async drivers (asyncpg, motor)
Global state without locksRace conditionsUse asyncio.Lock or Depends()
Raising exceptions without HTTPExceptionGeneric 500 errorsUse HTTPException with status codes
No input validationSecurity vulnerabilitiesUse Pydantic models with validators

Quick Troubleshooting

ProblemDiagnosisFix
"RuntimeError: no running event loop"Calling async from sync codeUse await or asyncio.run()
Validation errors not clearMissing field descriptionsAdd Field(description=...)
Slow response timesSync database callsSwitch to async SQLAlchemy
CORS errors in browserMissing middlewareAdd CORSMiddleware
Dependency not injectedWrong import or syntaxCheck Depends() syntax

Production Readiness

Security Configuration

from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter

app = FastAPI(
    title="My API",
    docs_url="/docs" if os.getenv("ENV") != "production" else None,
)

limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter

app.add_middleware(
    CORSMiddleware,
    allow_origins=os.getenv("ALLOWED_ORIGINS", "").split(","),
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH"],
    allow_headers=["*"],
)

Health Checks

@app.get("/health")
async def health():
    return {"status": "healthy"}

@app.get("/ready")
async def readiness(db: Session = Depends(get_db)):
    try:
        db.execute("SELECT 1")
        return {"status": "ready", "database": "connected"}
    except Exception:
        return JSONResponse(status_code=503, content={"status": "not ready"})

Graceful Shutdown

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    await database.connect()
    yield
    await database.disconnect()

app = FastAPI(lifespan=lifespan)

Monitoring Metrics

MetricAlert Threshold
Request latency p99> 500ms
Error rate (5xx)> 1%
Memory usage> 80%
Worker utilization> 90%

Checklist

  • CORS properly configured
  • Rate limiting enabled
  • Security headers middleware
  • Pydantic validation on all inputs
  • Health/readiness/liveness endpoints
  • Structured logging (JSON format)
  • Global exception handler
  • Secrets via environment variables
  • Docs disabled in production
  • Gunicorn with multiple workers
  • Graceful shutdown handling

Reference Documentation

发现
标签

此技能尚未发布标签。

版本
最新版本元数据

版本

v2026.09.24

发布时间

Sep 24, 2026

分类

未分类

许可证

MIT

源路径

skills/backend-frameworks/fastapi

默认分支

main

最新提交

9496306

Tree SHA

fe4e2f1