Interactive API documentation in FastAPI is a web - based interface that provides detailed information about the API endpoints. It includes details such as the HTTP methods supported by each endpoint, the input parameters, the expected output, and the status codes. The most significant advantage is that users can interact with the API directly from the documentation page. They can send requests to the endpoints, provide input data, and view the responses, all without using external tools like Postman.
FastAPI uses two popular tools for generating interactive API documentation:
First, make sure you have FastAPI and Uvicorn (a server for running FastAPI applications) installed. You can install them using pip
:
pip install fastapi uvicorn
Here is a basic example of a FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
To run the application, use the following command in the terminal:
uvicorn main:app --reload
Here, main
is the name of the Python file (if your file is named main.py
), and app
is the FastAPI application instance.
http://127.0.0.1:8000/docs
. On this page, you will see a list of all the API endpoints. You can expand each endpoint to view the details, and click the “Try it out” button to send requests.http://127.0.0.1:8000/redoc
. It provides a more organized and static view of the API.You can add descriptions to your endpoints using docstrings. These descriptions will be displayed in the interactive API documentation.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
"""
This is the root endpoint.
It returns a simple greeting message.
"""
return {"Hello": "World"}
FastAPI uses Pydantic models to define input and output schemas. These schemas are automatically displayed in the API documentation.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
def create_item(item: Item):
"""
Create a new item.
"""
return item
You can group related endpoints using tags. Tags make the API documentation more organized and easier to navigate.
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/", tags=["items"])
def read_items():
return [{"name": "Item 1"}, {"name": "Item 2"}]
@app.get("/users/", tags=["users"])
def read_users():
return [{"name": "User 1"}, {"name": "User 2"}]
If your API has authentication requirements, you can configure the API documentation to handle authentication. For example, if you are using OAuth2, you can set up the Swagger UI to accept tokens.
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/protected/")
def read_protected(token: str = Depends(oauth2_scheme)):
return {"token": token}
FastAPI’s interactive API documentation is a powerful tool that simplifies the process of developing, testing, and understanding APIs. By leveraging the underlying technologies like Swagger UI and ReDoc, developers can quickly create and share detailed API documentation. Following the common practices and best practices outlined in this guide will help you create more organized, secure, and user - friendly API documentation.