FastAPI
Minimal app
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.get("/items/{item_id}")
async def read(item_id: int):
if item_id < 1:
raise HTTPException(404, "not found")
return {"id": item_id}
@app.post("/items")
async def create(item: Item):
return item
Run
uvicorn main:app --reload # development
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4 # production
- Auto docs:
/docs (Swagger), /redoc.
Patterns
- Pydantic models give validation + serialization; limit output with
response_model=.
- Dependency injection:
def dep(): ... + Depends(dep) — share auth, DB session.
- Async: use
async def for I/O; wrap blocking work with run_in_threadpool.
- Router: split large apps with
APIRouter, app.include_router(...).
- Middleware / CORS:
app.add_middleware(CORSMiddleware, ...).
Production
- Gunicorn + uvicorn workers; nginx reverse proxy in front.
- Read config from the environment (pydantic-settings).
Use this skill with your AI agent:
Open on Skill Issue to copy, download, or add to your library.
Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.