fastapi
When to use this skill
Use this skill whenever the user wants to:
- Build REST or async APIs with FastAPI and Pydantic models
- Implement dependency injection, authentication, or middleware
- Configure routing, OpenAPI documentation, and deployment
- Integrate with databases using async patterns
How to use this skill
Workflow
- Create app — instantiate
FastAPI()and define route handlers - Define models — use Pydantic for request/response validation
- Add dependencies — implement DI for auth, DB sessions, etc.
- Test and deploy — run with uvicorn, verify via
/docs
Quick Start Example
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel
from datetime import datetime
app = FastAPI(title="My API", version="1.0.0")
# Request/Response models
class ItemCreate(BaseModel):
name: str
price: float
description: str | None = None
class ItemResponse(ItemCreate):
id: int
created_at: datetime
# Dependency example
async def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Route handlers
@app.post("/items/", response_model=ItemResponse, status_code=201)
async def create_item(item: ItemCreate, db=Depends(get_db)):
db_item = Item(**item.model_dump())
db.add(db_item)
db.commit()
return db_item
@app.get("/items/{item_id}", response_model=ItemResponse)
async def get_item(item_id: int, db=Depends(get_db)):
item = db.query(Item).get(item_id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
# Run the server
uvicorn main:app --reload
# Interactive docs available at http://localhost:8000/docs
Authentication Example
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/users/me")
async def read_current_user(token: str = Depends(oauth2_scheme)):
user = verify_token(token)
if not user:
raise HTTPException(status_code=401, detail="Invalid token")
return user
Best Practices
- Define explicit request and response schemas with Pydantic; use consistent status codes
- Use async functions and database connection pools for high concurrency
- Configure CORS middleware and security headers for production
- Leverage automatic OpenAPI docs (
/docs,/redoc) for API exploration - Use
BackgroundTasksfor non-blocking operations like email sending
Reference
- Official documentation: https://fastapi.tiangolo.com/
Keywords
fastapi, async API, Pydantic, OpenAPI, dependency injection, Python web, REST API, uvicorn
More from partme-ai/full-stack-skills
vite
Guidance for Vite using the official Guide, Config Reference, and Plugins pages. Use when the user needs Vite setup, configuration, or plugin selection details.
68element-plus-vue3
Provides comprehensive guidance for Element Plus Vue 3 component library including installation, components, themes, internationalization, and API reference. Use when the user asks about Element Plus for Vue 3, needs to build Vue 3 applications with Element Plus, or customize component styles.
63vue3
Guidance for Vue 3 using the official guide and API reference. Use when the user needs Vue 3 concepts, patterns, or API details to build components, apps, and tooling.
54electron
Build cross-platform desktop applications with Electron, covering main/renderer process architecture, IPC communication, BrowserWindow management, menus, tray icons, packaging, and security best practices. Use when the user asks about Electron, needs to create desktop applications, implement Electron features, or build cross-platform desktop apps.
51uniapp-project
Provides per-component and per-API examples with cross-platform compatibility details for uni-app, covering built-in components, uni-ui components, and APIs (network, storage, device, UI, navigation, media). Use when the user needs official uni-app components or APIs, wants per-component examples with doc links, or needs platform compatibility checks.
40ascii-cli-logo-banner
Entry point for ASCII CLI banners that routes to the Python built-in font skill or figlet.js/FIGfont skill. Use when the user wants a startup banner, ASCII logo, terminal welcome screen, or CLI branding for a service.
38