Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

FastAPI interactive API documentation

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

  1. Click the route you want to test to expand its details
  2. Click "Try it out" button
  3. Fill in the parameter values
  4. Click "Execute" button to send the request
  5. 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:


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 description and the function's docstring,description takes 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