Configure Swagger UI

Warning

The current page still doesn’t have a translation for this language.

But you can help translating it: Contributing.

You can configure some extra Swagger UI parameters.

To configure them, pass the swagger_ui_parameters argument when creating the FastAPI() app object or to the get_swagger_ui_html() function.

swagger_ui_parameters receives a dictionary with the configurations passed to Swagger UI directly.

FastAPI converts the configurations to JSON to make them compatible with JavaScript, as that’s what Swagger UI needs.

Disable Syntax Highlighting

For example, you could disable syntax highlighting in Swagger UI.

Without changing the settings, syntax highlighting is enabled by default:

Configure Swagger UI - 图1

But you can disable it by setting syntaxHighlight to False:

  1. from fastapi import FastAPI
  2. app = FastAPI(swagger_ui_parameters={"syntaxHighlight": False})
  3. @app.get("/users/{username}")
  4. async def read_user(username: str):
  5. return {"message": f"Hello {username}"}

…and then Swagger UI won’t show the syntax highlighting anymore:

Configure Swagger UI - 图2

Change the Theme

The same way you could set the syntax highlighting theme with the key "syntaxHighlight.theme" (notice that it has a dot in the middle):

  1. from fastapi import FastAPI
  2. app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"})
  3. @app.get("/users/{username}")
  4. async def read_user(username: str):
  5. return {"message": f"Hello {username}"}

That configuration would change the syntax highlighting color theme:

Configure Swagger UI - 图3

Change Default Swagger UI Parameters

FastAPI includes some default configuration parameters appropriate for most of the use cases.

It includes these default configurations:

  1. swagger_ui_default_parameters: Annotated[
  2. Dict[str, Any],
  3. Doc(
  4. """
  5. Default configurations for Swagger UI.

You can override any of them by setting a different value in the argument swagger_ui_parameters.

For example, to disable deepLinking you could pass these settings to swagger_ui_parameters:

  1. from fastapi import FastAPI
  2. app = FastAPI(swagger_ui_parameters={"deepLinking": False})
  3. @app.get("/users/{username}")
  4. async def read_user(username: str):
  5. return {"message": f"Hello {username}"}

Other Swagger UI Parameters

To see all the other possible configurations you can use, read the official docs for Swagger UI parameters.

JavaScript-only settings

Swagger UI also allows other configurations to be JavaScript-only objects (for example, JavaScript functions).

FastAPI also includes these JavaScript-only presets settings:

  1. presets: [
  2. SwaggerUIBundle.presets.apis,
  3. SwaggerUIBundle.SwaggerUIStandalonePreset
  4. ]

These are JavaScript objects, not strings, so you can’t pass them from Python code directly.

If you need to use JavaScript-only configurations like those, you can use one of the methods above. Override all the Swagger UI path operation and manually write any JavaScript you need.