Skip to main content
You can conditionally enable or disable OpenAPI documentation based on environment, configuration, or other runtime conditions. This is commonly used to hide API docs in production environments.

Understanding OpenAPI Control

FastAPI provides several parameters to control documentation:
  • openapi_url: URL where OpenAPI schema is served (default: /openapi.json)
  • docs_url: URL for Swagger UI docs (default: /docs)
  • redoc_url: URL for ReDoc docs (default: /redoc)
Setting openapi_url=None disables OpenAPI schema generation and automatically disables both /docs and /redoc.

Security Considerations

Hiding documentation does NOT secure your API. Path operations remain accessible even when docs are disabled.

Why Hiding Docs Isn’t Security

Disabling documentation:
  • ❌ Doesn’t protect endpoints
  • ❌ Doesn’t fix security vulnerabilities
  • ❌ Doesn’t prevent API access
  • ❌ Is simply security through obscurity

Real Security Measures

Instead, implement proper security:
  • ✅ Use Pydantic models for request/response validation
  • ✅ Implement authentication and authorization
  • ✅ Use OAuth2 scopes for granular permissions
  • ✅ Store password hashes, never plaintext
  • ✅ Use proven cryptographic tools (JWT, bcrypt, etc.)
  • ✅ Apply rate limiting and input sanitization
  • ✅ Follow security best practices
Hiding docs may make debugging harder and doesn’t improve security. Only disable docs if you have a specific organizational requirement.

Disabling OpenAPI with Settings

Use environment variables and Pydantic settings to control OpenAPI availability.

Using Pydantic Settings

Disable OpenAPI by setting an environment variable:
With OpenAPI disabled:
  • /openapi.json returns 404
  • /docs returns 404
  • /redoc returns 404
  • Your endpoints still work normally
Setting OPENAPI_URL to an empty string is equivalent to openapi_url=None.

Environment-Based Configuration

Different configurations for different environments:

Environment Files

Development (.env.development)
Production (.env.production)

Selectively Disabling Documentation UIs

Disable specific docs UIs while keeping others:

Disable Swagger UI, Keep ReDoc

Disable ReDoc, Keep Swagger UI

Custom Documentation URLs

Advanced Environment-Based Control

Multiple Environment Support

Feature Flags

Protected Documentation Endpoints

If you want docs available but protected, use dependencies:
This approach keeps docs available but requires authentication to view them.

Testing with Conditional OpenAPI

Test different configurations:

Complete Production Example

Best Practices

Configuration

  • Use environment variables: Make OpenAPI configurable without code changes
  • Environment-based defaults: Set sensible defaults per environment
  • Document behavior: Clearly document which environments have docs enabled

Security

  • Don’t rely on hidden docs: Implement proper authentication and authorization
  • Consider protected docs: Use authentication on docs instead of disabling them
  • Keep schema accessible: Consider keeping /openapi.json but hiding UIs

Operations

  • Test all configurations: Verify behavior with docs enabled and disabled
  • Log configuration: Log OpenAPI availability on startup
  • Monitor access: Track docs access in production environments

Summary

Conditional OpenAPI gives you control over documentation visibility, but remember:
  • It’s not a security feature
  • Endpoints remain accessible
  • Use proper authentication/authorization for real security
  • Consider protected docs instead of disabled docs