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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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.
ONE 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.
Source: fastapi.tiangolo.com
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.