Safeguard
Security Guides•

Should you disable FastAPI /docs and /openapi.json in production?

FastAPI serves /docs, /redoc and /openapi.json by default. When to turn them off, how with docs_url=None and openapi_url=None, and what that does not fix.

Safeguard Security Team
3 min read

For an API that is not meant to be public documentation, yes: turn them off or put them behind authentication. FastAPI serves Swagger UI at /docs, ReDoc at /redoc and the schema at /openapi.json by default, and you can disable each with docs_url=None, redoc_url=None and openapi_url=None.

What is exposed by default

FastAPI's metadata docs describe two documentation interfaces: Swagger UI served at /docs and ReDoc served at /redoc. The OpenAPI schema is served at /openapi.json. All three are on unless you change them.

The schema lists every path, method, parameter and model. For a public API that is the point. For an internal or partner API it is a free inventory for anyone who finds the host: endpoint names, expected fields and which routes look administrative.

How to turn them off

from fastapi import FastAPI

app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)

FastAPI's docs note that setting openapi_url to None disables the schema completely and also disables the documentation interfaces that use it. So that one setting is enough if you want everything gone. Setting docs_url and redoc_url alone leaves the raw schema available.

You can also move them rather than remove them, for example docs_url at a less guessable path, but that is obscurity, not protection.

Make it environment aware

A common approach is to keep docs in development and staging and disable them in production:

import os
from fastapi import FastAPI

prod = os.environ.get("APP_ENV") == "production"
app = FastAPI(
    docs_url=None if prod else "/docs",
    redoc_url=None if prod else "/redoc",
    openapi_url=None if prod else "/openapi.json",
)

If your clients generate SDKs from the schema, publish the schema file through your build pipeline instead of serving it live.

What disabling docs does not do

Hiding documentation does not secure an endpoint. Every route still exists and still answers. Attackers can find paths through guessing, client code or traffic. So treat this as one layer:

ControlPurpose
Disable or gate docsReduce free reconnaissance
Authentication on every routeStop unauthorized calls
Authorization checks per objectStop one user reading another's data
Response modelsStop internal fields leaking
Rate limits and input limitsReduce abuse

If you want docs in production but only for staff, put the docs routes behind your gateway or an authentication dependency rather than leaving them open.

How to check

  1. Request /docs, /redoc and /openapi.json on each production hostname.
  2. Check every environment that is reachable from the internet, including preview and staging.
  3. Compare the schema against what you intend to expose. Unexpected routes are a finding by themselves.

Try it yourself

Click through it at your own pace, or open it full screen. Look at how an API scan enumerates endpoints and flags what should not be reachable.

Quick check

Does openapi_url=None remove Swagger UI too?

Yes. FastAPI's docs say it disables the schema completely and the documentation interfaces that depend on it.

Is hiding /docs enough to protect an API?

No. It only removes a convenience for finding routes. Authentication and authorization on the routes themselves are what protect data.

Safeguard's API security scanning and asset discovery can help find API surfaces you did not know were exposed. Try a free scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.