
FastAPI
Learn the fundamentals of request examples in FastAPI.
Request examples in Pydantic models provide sample data in Swagger UI. They help API consumers understand what data to send and improve API documentation automatically.
In this section, you'll understand:
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()
class Item(BaseModel):
name: str = Field(..., example="Widget")
price: float = Field(..., example=9.99, gt=0)
description: str = Field(None, example="A useful widget")
@app.post("/items/")
async def create_item(item: Item):
return itemfrom pydantic import BaseModel, ConfigDict
class Item(BaseModel):
model_config = ConfigDict(
json_schema_extra={
"example": {
"name": "Widget",
"price": 9.99,
"description": "A useful widget"
}
}
)
name: str
price: float
description: str = NoneExamples are critical for:
Ready to explore more? Check out the advanced section for production patterns and edge cases.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward