Agent skill
fastapi-development
Build async APIs with FastAPI, including endpoints, dependency injection, validation, and testing. Use when creating REST APIs, web backends, or microservices.
Install this agent skill to your Project
npx add-skill https://github.com/aiskillstore/marketplace/tree/main/skills/bossjones/fastapi-development
SKILL.md
FastAPI Development
Quick start
Create a basic FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
Run with:
uv run uvicorn main:app --reload
Common patterns
Pydantic models for validation
from pydantic import BaseModel
from typing import Optional
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
@app.post("/items/")
async def create_item(item: Item):
return item
Dependency injection
from typing import Annotated
from fastapi import Depends
async def common_parameters(
q: str | None = None,
skip: int = 0,
limit: int = 100
):
return {"q": q, "skip": skip, "limit": limit}
CommonsDep = Annotated[dict, Depends(common_parameters)]
@app.get("/items/")
async def read_items(commons: CommonsDep):
return commons
Database dependencies with cleanup
async def get_db():
db = connect_to_database()
try:
yield db
finally:
db.close()
@app.get("/query/")
async def query_data(db: Annotated[dict, Depends(get_db)]):
return {"data": "query results"}
Error handling
from fastapi import HTTPException
@app.get("/items/{item_id}")
async def read_item(item_id: int):
if item_id < 1:
raise HTTPException(status_code=404, detail="Item not found")
return {"item_id": item_id}
Path and query validation
from typing import Annotated
from fastapi import Path, Query
@app.get("/items/{item_id}")
async def read_item(
item_id: Annotated[int, Path(gt=0, le=1000)],
q: Annotated[str, Query(max_length=50)] = None
):
return {"item_id": item_id, "q": q}
Response models
from pydantic import BaseModel
class ItemPublic(BaseModel):
id: int
name: str
price: float
@app.get("/items/{item_id}", response_model=ItemPublic)
async def read_item(item_id: int):
return ItemPublic(id=item_id, name="Laptop", price=999.99)
Testing with TestClient
from fastapi.testclient import TestClient
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
def test_read_item():
response = client.get("/items/42?q=test")
assert response.status_code == 200
assert response.json() == {"item_id": 42, "q": "test"}
Requirements
uv add fastapi uvicorn
uv add "fastapi[all]" # Includes all optional dependencies
Key concepts
- Async/await: Use
async deffor I/O operations - Automatic validation: Request/response validation with Pydantic
- Dependency injection: Share logic across endpoints with
Depends - Type hints: Full editor support and validation
- Interactive docs: Auto-generated Swagger/OpenAPI at
/docs - Background tasks: Run tasks after response using
BackgroundTasks
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
perigon-backend
Perigon ASP.NET Core + EF Core + Aspire conventions
perigon-agent
Pointers for Copilot/agents to apply Perigon conventions
perigon-angular
Angular 21+ standalone/Material/signal conventions for Perigon WebApp
fastapi-mastery
Comprehensive FastAPI development skill covering REST API creation, routing, request/response handling, validation, authentication, database integration, middleware, and deployment. Use when working with FastAPI projects, building APIs, implementing CRUD operations, setting up authentication/authorization, integrating databases (SQL/NoSQL), adding middleware, handling WebSockets, or deploying FastAPI applications. Triggered by requests involving .py files with FastAPI code, API endpoint creation, Pydantic models, or FastAPI-specific features.
context7-efficient
Token-efficient library documentation fetcher using Context7 MCP with 86.8% token savings through intelligent shell pipeline filtering. Fetches code examples, API references, and best practices for JavaScript, Python, Go, Rust, and other libraries. Use when users ask about library documentation, need code examples, want API usage patterns, are learning a new framework, need syntax reference, or troubleshooting with library-specific information. Triggers include questions like "Show me React hooks", "How do I use Prisma", "What's the Next.js routing syntax", or any request for library/framework documentation.
browser-use
Browser automation using Playwright MCP. Navigate websites, fill forms, click elements, take screenshots, and extract data. Use when tasks require web browsing, form submission, web scraping, UI testing, or any browser interaction.
Didn't find tool you were looking for?