FastAPI interactive API documentation
FastAPI automatically generates interactive API documentation from type annotations in the code, providing Swagger UI and ReDoc interfaces by default. Developers can test APIs directly in the docs without extra tools.
Accessing the API documentation
After running the FastAPI app, visit the following addresses to view the docs:
| Address | Document type | Feature |
|---|---|---|
| http://127.0.0.1:8000/docs | Swagger UI | Interactive testing—click "Try it out" to send requests |
| http://127.0.0.1:8000/redoc | ReDoc | Good reading experience, suitable for browsing and looking up API definitions |
| http://127.0.0.1:8000/openapi.json | OpenAPI JSON | Raw OpenAPI specification JSON, consumable by tools |
Swagger UI
Swagger UI provides an intuitive interface for testing APIs directly in the browser:

Steps to test an API
- Click the route you want to test to expand its details
- Click "Try it out" button
- Fill in the parameter values
- Click "Execute" button to send the request
- View the response result

ReDoc
ReDoc focuses on readability, making it suitable for browsing API definitions:

OpenAPI Specification
FastAPI uses OpenAPI the standard to convert APIs into a "schema" (Schema). Visit http://127.0.0.1:8000/openapi.json to view the raw OpenAPI JSON:
{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/items/{item_id}": {
"get": {
"responses": {
"200": {
"description": "Successful Response"
}
}
}
}
}
}
Uses of the OpenAPI specification:
- Powers the Swagger UI and ReDoc documentation systems
- Automatically generates client code in various languages
- Integrates with API testing tools (such as Postman)
- Compatible with a large number of third-party tools and platforms
Customizing API documentation info
When creating a FastAPI instance, you can customize the documentation metadata:
Example
from
fastapi
import
FastAPI
app
=
FastAPI
(
title
=
"My API"
,
API title
description
=
"This is a sample API demonstrating documentation customization"
,
API description
version
=
"1.0.0"
,
API version
terms_of_service
=
"http://example.com/terms/"
,
Terms of service URL
contact
=
{
Contact information
"name"
:
"Developer"
,
"url"
:
"http://example.com/contact/"
,
"email"
:
"dev@example.com"
,
}
,
license_info
=
{
License information
"name"
:
"MIT"
,
"url"
:
"https://opensource.org/licenses/MIT"
,
}
,
)
Adding documentation info to routes
You can add detailed documentation info for each route in the decorator and function:
Example
from
typing
import
Annotated
from
fastapi
import
FastAPI
,
Path
,
Query
app
=
FastAPI
(
)
@
app.
get
(
"/items/{item_id}"
,
summary
=
"Get product info"
,
Short summary
description
=
"Get detailed product information by product ID"
,
Detailed description
response_description
=
"Product info object"
,
Response description
tags
=
[
"Product management"
]
,
Grouping tag
)
async
def
read_item
(
item_id: Annotated
[
int
,
Path
(
ge
=
1
,
description
=
"product_id"
)
]
,
q: Annotated
[
str
|
None
,
Query
(
description
=
"Search keyword"
)
]
=
None
,
)
:
"""
Get product info:
- item_id: the product's unique identifier
- q: an optional search keyword
"""
return
{
"item_id"
: item_id
,
"q"
: q
}
Documentation parameter descriptions:
| Parameters | Position | Description |
|---|---|---|
summary |
Decorator | A short summary of the route, shown in the route list |
description |
Decorator | A detailed description of the route, supporting Markdown |
response_description |
Decorator | The description of the response |
tags |
Decorator | Route grouping, displayed by tag in the docs |
| docstring | Function body | The function's docstring, displayed as the description |
If both are set
descriptionand the function's docstring,descriptiontakes precedence. Docstrings support Markdown formatting, suitable for longer explanations.
Grouping with tags
tags parameter can group related routes and display them more clearly in the docs:
Example
from
fastapi
import
FastAPI
app
=
FastAPI
(
)
@
app.
get
(
"/users/"
,
tags
=
[
"User management"
]
)
async
def
read_users
(
)
:
return
[
{
"username"
:
"Rick"
}
,
{
"username"
:
"Morty"
}
]
@
app.
get
(
"/items/"
,
tags
=
[
"Product management"
]
)
async
def
read_items
(
)
:
return
[
{
"name"
:
"Foo"
}
,
{
"name"
:
"Bar"
}
]
In Swagger UI, routes are displayed grouped by tag.
Disabling documentation
In a production environment, you may want to disable the automatic documentation:
Example
from
fastapi
import
FastAPI
Disabling documentation
app
=
FastAPI
(
docs_url
=
None
,
redoc_url
=
None
)
Summary
- FastAPI automatically generates two interactive docs: Swagger UI and ReDoc
- Doc content is generated automatically from type annotations in the code, and stays in sync as the code changes
- Use
title、description、tagsand other parameters to customize documentation info - The function's docstring also appears in the docs
- In production, you can use
docs_url=NoneDisabling documentation