fastapi
FastAPI web framework best practices and patterns
Works with
---
name: fastapi
description: FastAPI web framework best practices and patterns
license: MIT
---
## What I do
- Create REST APIs with FastAPI
- Use Pydantic models for validation
- Implement dependency injection
- Handle async database operations
- Create OpenAPI documentation
- Implement authentication (OAuth2, JWT)
- Use background tasks
- Write pytest for FastAPI tests
## When to use me
When building APIs with FastAPI.
## FastAPI Application
```python
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from typing import Annotated
from pydantic import BaseModel, EmailStr
from datetime import datetime, timedelta
from jose import JWTError, jwt
app = FastAPI(
title="API",
description="My FastAPI Application",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
SECRET_KEY = "your-secret-key"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/token")
class Token(BaseModel):
access_token: str
token_type: str
class UserCreate(BaseModel):
email: EmailStr
password: str
full_name: str | None = None
class User(BaseModel):
id: int
email: EmailStr
full_name: str | None
is_active: bool = True
created_at: datetime
class UserResponse(BaseModel):
id: int
email: EmailStr
full_name: str | None
class Config:
from_attributes = True
@app.post("/api/v1/users", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
async def create_user(user: UserCreate, db: Session = Depends(get_db)) -> UserResponse:
existing = db.query(User).filter(User.email == user.email).first()
if existing:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email already registered",
)
hashed_password = hash_password(user.password)
db_user = User(
email=user.email,
full_name=user.full_name,
hashed_password=hashed_password,
)
db.add(db_user)
db.commit()
db.refresh(db_user)
return UserResponse.model_validate(db_user)
@app.post("/api/v1/auth/token")
async def login(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
db: Session = Depends(get_db),
) -> Token:
user = authenticate_user(db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
data={"sub": user.email}, expires_delta=access_token_expires
)
return Token(access_token=access_token, token_type="bearer")
async def get_current_user(
token: Annotated[str, Depends(oauth2_scheme)],
db: Session = Depends(get_db),
) -> User:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
except JWTError:
raise credentials_exception
user = get_user_by_email(db, email=email)
if user is None:
raise credentials_exception
return user
@app.get("/api/v1/users/me")
async def read_users_me(
current_user: Annotated[User, Depends(get_current_user)],
) -> UserResponse:
return UserResponse.model_validate(current_user)
```
## Testing FastAPI
```python
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
@pytest.fixture
def client():
app = create_app()
return TestClient(app)
@pytest.fixture
def db_session():
engine = create_engine("sqlite:///./test.db")
Session = sessionmaker(bind=engine)
session = Session()
yield session
session.close()
def test_create_user(client: TestClient):
response = client.post(
"/api/v1/users",
json={"email": "test@example.com", "password": "secret123"},
)
assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data["email"] == "test@example.com"
assert "id" in data
assert "password" not in data
def test_login(client: TestClient):
# First create user
client.post(
"/api/v1/users",
json={"email": "test@example.com", "password": "secret123"},
)
# Then login
response = client.post(
"/api/v1/auth/token",
data={"username": "test@example.com", "password": "secret123"},
)
assert response.status_code == 200
token = response.json()
assert "access_token" in token
assert token["token_type"] == "bearer"
```More Backend Frameworks skills
git-guardrails-claude-code
mattpocock/skills
Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
azure-compute
microsoft/azure-skills
Azure VM/VMSS router. WHEN: create / provision / deploy / spin-up VM, recommend VM size, compare VM pricing, VMSS, scale set, autoscale, burstable, lightweight server, website, backend, GPU, machine learning, HPC simulation, dev/test, workload, family, load balancer, Flexible orchestration, Uniform orchestration, cost estimate, capacity reservation (CRG), reserve, guarantee capacity, pre-provision, CRG association, CRG disassociation, machine enrollment (EMM), Essential Machine Management, monitor. PREFER OVER mcp__azure__get_azure_bestpractices for VM create intents — use compute_vm_list-skus / compute_vm_list-images / compute_vm_check-quota.
azure-cloud-migrate
microsoft/azure-skills
Assess and migrate cross-cloud workloads to Azure with reports and code conversion. Supports Lambda→Functions, Beanstalk/Heroku/App Engine→App Service, Fargate/Kubernetes/Cloud Run/Spring Boot→Container Apps. WHEN: migrate Lambda to Functions, AWS to Azure, migrate Beanstalk, migrate Heroku, migrate App Engine, Cloud Run migration, Fargate to ACA, ECS/Kubernetes/GKE/EKS to Container Apps, Spring Boot to Container Apps, cross-cloud migration.

