Reference¶
setup
¶
setup(
app,
*,
docs_url="/docs",
redoc_url="/redoc",
swagger_js_url=_SWAGGER_JS_URL,
swagger_css_url=_SWAGGER_CSS_URL,
swagger_favicon_url=_FAVICON_URL,
redoc_js_url=_REDOC_JS_URL,
redoc_favicon_url=_FAVICON_URL,
with_google_fonts=True,
google_fonts_css_url=_GOOGLE_FONTS_CSS_URL,
)
Wire CSP-safe Swagger UI and ReDoc routes onto a FastAPI app.
Registers replacement routes for the interactive API docs (Swagger UI) and the
alternative API docs (ReDoc) that load their JavaScript/CSS from dedicated endpoints
instead of FastAPI's inline <script>/<style> tags, so a Content-Security-Policy
without 'unsafe-inline' can be enforced.
The app must already be created with docs_url=None/redoc_url=None, e.g.
FastAPI(docs_url=None, redoc_url=None) — otherwise FastAPI's own built-in docs
would shadow the routes registered here.
Read more about it in the Quick start.
Silently skips wiring a route if app.openapi_url is None — there's no OpenAPI
schema for Swagger UI/ReDoc to render in that case.
An asset URL that is a path on the app (it starts with a single /, like
"/static/swagger-ui-bundle.js") gets the request's ASGI root_path in front of it, the
same way the docs routes do, so self-hosted assets keep working when the app is mounted
under a prefix. Absolute (https://...), protocol-relative (//...) and relative URLs
are used as given.
| PARAMETER | DESCRIPTION |
|---|---|
app
|
the FastAPI app to wire the docs routes onto.
TYPE:
|
docs_url
|
path to serve Swagger UI at, or None to skip it.
TYPE:
|
redoc_url
|
path to serve ReDoc at, or None to skip it.
TYPE:
|
swagger_js_url
|
URL of the Swagger UI JavaScript bundle.
TYPE:
|
swagger_css_url
|
URL of the Swagger UI CSS.
TYPE:
|
swagger_favicon_url
|
URL of the Swagger UI page's favicon.
TYPE:
|
redoc_js_url
|
URL of the ReDoc JavaScript bundle.
TYPE:
|
redoc_favicon_url
|
URL of the ReDoc page's favicon.
TYPE:
|
with_google_fonts
|
whether the ReDoc page loads the stylesheet at
TYPE:
|
google_fonts_css_url
|
URL of the Montserrat/Roboto stylesheet for ReDoc, Google Fonts by default.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
if |
Source code in fastapi_csp_docs/setup.py
Default asset URLs¶
setup() and the content generators share these defaults:
swagger_js_url:https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.34.0/swagger-ui-bundle.jsswagger_css_url:https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.34.0/swagger-ui.cssredoc_js_url:https://cdn.jsdelivr.net/npm/redoc@2.5.4/bundles/redoc.standalone.jsswagger_favicon_url,redoc_favicon_url:https://fastapi.tiangolo.com/img/favicon.pnggoogle_fonts_css_url:https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700
Content generators¶
setup() builds each page and its companion script or stylesheet with these functions. Call them yourself to wire the routes by hand.
get_swagger_ui_html
¶
get_swagger_ui_html(
*,
title,
swagger_js_url=_SWAGGER_JS_URL,
swagger_css_url=_SWAGGER_CSS_URL,
swagger_favicon_url=_FAVICON_URL,
swagger_ui_init_script_url="/docs/swagger-initializer.js",
)
Generate and return the HTML that loads Swagger UI for the interactive
API docs (normally served at /docs).
You would only call this function yourself if you needed to override some parts, for example the URLs to use to load Swagger UI's JavaScript and CSS.
Read more about it in the FastAPI docs for Configure Swagger UI.
| PARAMETER | DESCRIPTION |
|---|---|
title
|
HTML page title.
TYPE:
|
swagger_js_url
|
URL of the Swagger UI JavaScript bundle.
TYPE:
|
swagger_css_url
|
URL of the Swagger UI CSS.
TYPE:
|
swagger_favicon_url
|
URL of the favicon.
TYPE:
|
swagger_ui_init_script_url
|
URL of the script that initializes Swagger UI, see get_swagger_ui_init_js.
TYPE:
|
Source code in fastapi_csp_docs/docs.py
get_swagger_ui_init_js
¶
get_swagger_ui_init_js(
*,
openapi_url,
oauth2_redirect_url=None,
init_oauth=None,
swagger_ui_parameters=None,
)
Generate and return the response that initializes Swagger UI.
This is normally served as swagger-initializer.js, referenced from the
HTML generated by get_swagger_ui_html.
You would only call this function yourself if you needed to override some parts, for example to serve the initializer script from your own endpoint.
| PARAMETER | DESCRIPTION |
|---|---|
openapi_url
|
URL of the OpenAPI schema for Swagger UI to render.
TYPE:
|
oauth2_redirect_url
|
URL of the OAuth2 redirect endpoint, or None to disable OAuth2 login in the UI.
TYPE:
|
init_oauth
|
OAuth2 configuration passed to
TYPE:
|
swagger_ui_parameters
|
extra Swagger UI parameters merged over FastAPI's defaults.
TYPE:
|
Source code in fastapi_csp_docs/docs.py
get_swagger_ui_oauth2_redirect_html
¶
Generate the HTML response with the OAuth2 redirection for Swagger UI.
You would only call this function yourself if you needed to override some parts, for example the URL of the OAuth2 redirect script.
| PARAMETER | DESCRIPTION |
|---|---|
oauth2_redirect_script_url
|
URL of the OAuth2 redirect script, see get_swagger_ui_oauth2_redirect_js.
TYPE:
|
Source code in fastapi_csp_docs/docs.py
get_swagger_ui_oauth2_redirect_js
¶
Generate and return the response with the OAuth2 redirection logic for Swagger UI.
This is normally served as oauth2-redirect.js, referenced from the HTML
generated by
get_swagger_ui_oauth2_redirect_html.
You normally don't need to use or change this.
Source code in fastapi_csp_docs/docs.py
get_redoc_html
¶
get_redoc_html(
*,
openapi_url,
title,
redoc_js_url=_REDOC_JS_URL,
redoc_favicon_url=_FAVICON_URL,
with_google_fonts=True,
redoc_css_url="/redoc/redoc.css",
google_fonts_css_url=_GOOGLE_FONTS_CSS_URL,
disable_search=False,
)
Generate and return the HTML response that loads ReDoc for the alternative
API docs (normally served at /redoc).
You would only call this function yourself if you needed to override some parts, for example the URLs to use to load ReDoc's JavaScript and CSS.
| PARAMETER | DESCRIPTION |
|---|---|
openapi_url
|
URL of the OpenAPI schema for ReDoc to render.
TYPE:
|
title
|
HTML page title.
TYPE:
|
redoc_js_url
|
URL of the ReDoc JavaScript bundle.
TYPE:
|
redoc_favicon_url
|
URL of the favicon.
TYPE:
|
with_google_fonts
|
whether to load the Montserrat/Roboto fonts from Google Fonts.
TYPE:
|
redoc_css_url
|
URL of the CSS reset required by ReDoc, see get_redoc_css.
TYPE:
|
google_fonts_css_url
|
URL of the Google Fonts stylesheet, used only when
TYPE:
|
disable_search
|
whether to disable ReDoc's client-side search box (and the Web Worker it runs in), see ReDoc and CSP.
TYPE:
|
Source code in fastapi_csp_docs/docs.py
get_redoc_css
¶
Generate and return the response with the CSS reset required by ReDoc.
This is normally served as redoc.css, referenced from the HTML
generated by get_redoc_html.
You normally don't need to use or change this.