Skip to content
tezvyn:

Customizing FastAPI's Swagger UI Behavior

Source: fastapi.tiangolo.comHardHow cards are made

Customizing FastAPI's Swagger UI Behavior

Treat FastAPI's Swagger UI as a configurable frontend, not a static page. You can customize its behavior by passing a dictionary of settings on app startup. This is useful for changing themes or pre-filling auth fields. The footgun: keys must be camelCase.

Why it exists

FastAPI's auto-generated docs are functional, but default settings don't fit every team. You might need to match company branding, improve performance on large schemas, or streamline the developer workflow by changing default UI behaviors like which sections are open or closed.

The mental model

Think of your FastAPI app as a bridge to the Swagger UI JavaScript library running in the browser. The swagger_ui_parameters argument is a direct passthrough, letting your Python backend inject a configuration object into that frontend library at initialization.

How it works

When you create your app with app = FastAPI(...), you can include the swagger_ui_parameters argument. It accepts a Python dictionary. FastAPI takes this dictionary, converts it to a JSON object, and embeds it in the HTML page that serves the docs. When a browser loads /docs, the Swagger UI script reads this JSON and applies the settings. For example, {"docExpansion": "none"} will make Swagger UI collapse all operations by default.

When to use it

Use this to tailor the documentation experience. Three common use cases: first, setting docExpansion to list or none to manage large APIs; second, pre-configuring authorization details to make testing easier; third, disabling syntax highlighting ("syntaxHighlight": False) to speed up rendering for very large JSON examples.

When not to use it

Do not use this for core API logic or security enforcement. These settings only affect the documentation's display in the browser; a user can always bypass the UI. Also, avoid adding so many customizations that the docs become confusing to developers familiar with standard Swagger.

One canonical example

To disable syntax highlighting and collapse all endpoint sections by default, initialize your app like this: from fastapi import FastAPI; app = FastAPI(swagger_ui_parameters={"syntaxHighlight": False, "docExpansion": "none"}). Notice the keys are camelCase strings, which is how the JavaScript library expects them, not Python's snake_case. If you used doc_expansion, it would be silently ignored.

Interview question

When customizing FastAPI's Swagger UI, why must configuration keys like "docExpansion" be in camelCase?

  • a.Using camelCase improves the performance of the generated documentation page.
  • b.It's a standard Python convention for dictionary keys used in web frameworks.
  • c.These keys are directly consumed by the underlying JavaScript Swagger UI library.Correct
  • d.FastAPI automatically converts snake_case keys to camelCase for compatibility.
Why?

The card states that "keys are camelCase strings, which is how the JavaScript library expects them, not Python's snake_case." This indicates the dictionary is a direct passthrough to the frontend JavaScript. Distractor A is incorrect because the card explicitly mentions that snake_case keys would be "silently ignored," meaning no automatic conversion occurs.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles