Discover how a tiny addition to your schema can make your API docs shine effortlessly!
Why Example data in schema in FastAPI? - Purpose & Use Cases
Imagine you are building an API and want to show users what kind of data your endpoints expect or return. You try to write this example data manually in your documentation or comments.
Manually writing example data is easy to get wrong, often outdated, and requires extra work every time your data model changes. This leads to confusing docs and frustrated users.
FastAPI lets you add example data directly inside your schema definitions. This keeps examples accurate, up-to-date, and automatically shown in your API docs.
"""User data: { 'id': 1, 'name': 'Alice' }"""
class User(BaseModel): id: int name: str class Config: schema_extra = { "example": {"id": 1, "name": "Alice"} }
This makes your API self-explanatory and easier to use, saving time and avoiding mistakes.
When a developer visits your API docs, they instantly see example user data, so they know exactly what to send or expect back.
Manual example data is error-prone and hard to maintain.
FastAPI schemas can include example data directly.
API docs become clearer and more reliable automatically.