The default path of API documentation is /docs, so it will be available at
http://localhost:5000/docs when running on local with the default port. You can
change the path via the docs_path parameter when creating the APIFlask instance:
In this way, you can serve multiple API docs at the same time, or add auth protect
to the docs view. If you want to use the built-in configuration variable for API docs or
just want to write less code, you can import the API docs template directly from APIFlask:
The following configuration variables can be used to configure API docs:
DOCS_FAVICON
REDOC_USE_GOOGLE_FONT
REDOC_CONFIG
SWAGGER_UI_LAYOUT
SWAGGER_UI_CONFIG
SWAGGER_UI_OAUTH_CONFIG
ELEMENTS_LAYOUT
ELEMENTS_CONFIG
RAPIDOC_THEME
RAPIDOC_CONFIG
RAPIPDF_CONFIG
See Configuration for the
introduction and examples of these configuration variables.
Use different CDN server for API documentation resources¶
Each resource (JavaScript/CSS files) URL has a configuration variable. You can pass
the URL from your preferred CDN server to the corresponding configuration variables:
Like what you need to do in the last section, to use local resources, you can pass
the URL of local static files to the corresponding configuration variables:
REDOC_STANDALONE_JS
SWAGGER_UI_CSS
SWAGGER_UI_BUNDLE_JS
SWAGGER_UI_STANDALONE_PRESET_JS
RAPIDOC_JS
ELEMENTS_JS
ELEMENTS_CSS
RAPIPDF_JS
For local resources, you can pass a relative URL. For example, if you want to host
the Redoc standalone JavaScript file from a local file, follow the following steps:
Put the file in your static folder, name it as redoc.standalone.js or whatever
you want.
Figure out the relative URL to your js file. If the file sits in the root of the
static folder, then the URL will be /static/redoc.standalone.js. If you put it
into a subfolder called js, then the URL will be /static/js/redoc.standalone.js.
If the generated PDF file is not meeting your requirements, you can also serve a static HTML file as the API documentation.
Take Swagger UI as an example, you will need to:
Create a static HTML file that loads Swagger UI JavaScript and CSS files.
Generate a local OpenAPI spec file with the flask spec command. Set the following configuration:
app.config['SYNC_LOCAL_SPEC']=True# keep the spec file up-to-date with codeapp.config['LOCAL_SPEC_PATH']='openapi.json'# the path to the spec fileapp.config['LOCAL_SPEC_JSON_INDENT']=0# set to 0 to remove indentation
Create a shell script to read the OpenAPI spec file and pass it to Swagger UI HTML file. For example:
#!/bin/shspec_value=$(python3-c"import json; print(open('openapi.json').read())")# Replace var spec = {...} line in index.html
sed-i"s|var spec = {.*}|var spec = $spec_value|g"index.html