Introduction
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python. It's known for its simplicity, ease of use, and automatic interactive API documentation generation. One of its powerful features is the ability to customize the documentation by adding titles and descriptions to your endpoints. In this article, we'll explore how to do that.
Changing the API Title and Description
When you create a FastAPI application, you can set the title and description for your API using the FastAPI class. These properties will be displayed in the automatically generated API documentation (Swagger UI or ReDoc). We simply provide the title and description parameters when initializing the FastAPI instance.
from fastapi import FastAPI
app = FastAPI(
title="My API",
description="My First API with FastAPI"
)
In the above example
- title: Sets the title of your API.
- description: Provides a brief description of your API.
Output Image
Adding Descriptions to Endpoints
You can add descriptions to individual endpoints using docstrings. These descriptions will appear in the API documentation for each route.
from fastapi import FastAPI, APIRouter
app = FastAPI(
title="My API",
description="My First API with FastAPI"
)
router_item = APIRouter()
router_user = APIRouter()
@router_user.get("/users/",summary="Get some users", tags=["Users"])
async def get_users():
"""
Returns a list of all users.
"""
return [{"name": "Harry"}, {"name": "Ron"}]
@router_user.get("/users1/", summary="Get other users", tags=["Users"])
async def get_other_users():
"""
Returns a list of other users.
"""
return [{"name": "Priya"}, {"name": "Riya"}]
@router_item.get("/items/",summary="Get some items", tags=["Items"])
async def get_items():
"""
Returns a list of all items.
"""
return [{"name": "wand"}, {"name": "flying broom"}]
@router_item.get("/items1/",summary="Get other items", tags=["Items"])
async def get_other_items():
"""
Returns a list of other items.
"""
return [{"name": "Camera"}, {"name": "Phone"}]
app.include_router(router_item, prefix="/items")
app.include_router(router_user, prefix="/users")
In the above example
- summary: Provides a short summary of the endpoint (displayed in the documentation).
- The docstring below the endpoint function provides a detailed description of what the endpoint does.
Output Image
In the above modification, we've added a summary ("Get all users.") and a description ("Returns a list of all users.") as docstrings right below the endpoint decorator. We've maintained the summary field (like- "Get all users.") for each endpoint, which provides a concise description of what each endpoint does (like -"Returns a list of all users."). This is a recommended way to document your endpoints as it not only serves as a documentation for developers but also gets picked up by FastAPI to generate interactive API documentation. And did the same with all the endpoints as well.
Conclusion
Enhancing FastAPI documentation by adding titles and descriptions to both the main app and each endpoint is a simple yet effective way to improve the readability and usability of your API. With just a few extra lines of code, you can make your API more understandable and user-friendly. So why not give it a try in your next FastAPI project?