Skip to content
tezvyn:

How do you disable FastAPI docs but keep the OpenAPI schema?

Source: fastapi.tiangolo.comHardHow cards are made

How do you disable FastAPI docs but keep the OpenAPI schema?

Tests FastAPI constructor routing: docs_url, redoc_url, and openapi_url. Answer: pass docs_url=None and redoc_url=None while keeping openapi_url="/openapi.json", gated by env var. Red flag: middleware or manual route deletion instead of native configuration.

What's really being asked

This question evaluates whether you know that FastAPI exposes its automatic documentation routes through constructor parameters rather than through decorators or middleware. It tests if you understand the distinction between the OpenAPI schema endpoint and the Swagger UI or ReDoc interactive interfaces, and whether you can configure them independently at application startup.

The full answer

First, the candidate should state that FastAPI's constructor accepts docs_url, redoc_url, and openapi_url arguments. Second, they should explain that setting docs_url to None and redoc_url to None removes those interactive UI routes entirely, while leaving openapi_url as its default value of /openapi.json keeps the raw schema available for internal consumers. Third, a senior candidate should mention making this conditional on an environment variable or configuration setting so that docs remain available in development but are disabled in production. Fourth, they should note that the OpenAPI schema is still generated internally even when the UIs are disabled, so internal tools can continue to fetch and use it.

The mistakes people make

A major red flag is proposing to add middleware to block requests to /docs and /redoc. This is unnecessarily complex and still exposes the routes in the routing table. Another weak pattern is manually deleting routes from app.routes after startup, which is fragile and implementation-dependent. Some candidates suggest keeping the docs but protecting them with authentication, which does not satisfy the requirement to disable them. Finally, conflating the UI endpoints with the JSON endpoint and thinking that disabling docs also disables the schema reveals a fundamental gap in understanding FastAPI's architecture.

What usually comes next

The interviewer may ask how you would secure /openapi.json so that only internal tools can access it. They might also ask what happens to the schema generation when docs are disabled, or how you would serve a custom documentation UI only to internal users without exposing the default Swagger or ReDoc pages. Another common pivot is asking how to mount separate FastAPI sub-applications with different docs configurations for public versus internal traffic.

A concrete example

You can instantiate the application conditionally based on an environment variable. For instance, in production you would write app = FastAPI(docs_url=None, redoc_url=None, openapi_url="/openapi.json"), while in development you might allow the defaults. A cleaner pattern is to read a settings object first, then pass docs_url=None if settings.ENV is production else "/docs", and similarly for redoc_url. This keeps environment-specific behavior explicit and testable without scattering conditionals throughout the codebase.

Interview question

You need to disable FastAPI's interactive documentation routes in production while keeping the raw OpenAPI schema available for internal consumers. What is the proper way to achieve this?

  • a.Require authentication on /docs and /redoc but leave the routes registered
  • b.Add middleware that rejects requests to /docs and /redoc with a 404 status
  • c.Pass docs_url=None and redoc_url=None to the FastAPI constructor while leaving openapi_url at its defaultCorrect
  • d.Manually delete the /docs and /redoc routes from app.routes after startup
Why?

The correct approach uses FastAPI's native constructor parameters to prevent the UI routes from being registered entirely while preserving the raw schema endpoint. Requiring authentication is wrong because it keeps the routes exposed rather than disabling them, and middleware still leaves the routes in the routing table.

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