How to Exclude Endpoint from Docs in FastAPI
include_in_schema=False in the route decorator. This prevents the endpoint from appearing in the OpenAPI schema and the Swagger UI documentation.Syntax
Use the include_in_schema parameter in your route decorator to control whether the endpoint appears in the docs.
include_in_schema=True(default): Endpoint is shown in docs.include_in_schema=False: Endpoint is hidden from docs.
from fastapi import FastAPI app = FastAPI() @app.get("/visible", include_in_schema=True) async def visible_endpoint(): return {"message": "This endpoint is visible in docs"} @app.get("/hidden", include_in_schema=False) async def hidden_endpoint(): return {"message": "This endpoint is hidden from docs"}
Example
This example shows two endpoints: one visible in the docs and one excluded from the docs using include_in_schema=False. When you run this FastAPI app and open the Swagger UI at /docs, only the visible endpoint appears.
from fastapi import FastAPI app = FastAPI() @app.get("/hello", include_in_schema=True) async def hello(): return {"message": "Hello, visible endpoint!"} @app.get("/secret", include_in_schema=False) async def secret(): return {"message": "This endpoint is hidden from docs."}
Common Pitfalls
One common mistake is forgetting to set include_in_schema=False on the route decorator, which means the endpoint will still appear in the docs. Another is trying to exclude endpoints by manipulating the docs manually, which is unnecessary and error-prone.
Also, include_in_schema=False only hides the endpoint from docs; it does not disable or protect the endpoint itself.
from fastapi import FastAPI app = FastAPI() # Wrong: endpoint still appears in docs because include_in_schema is not set @app.get("/visible") async def visible(): return {"message": "Visible in docs"} # Right: endpoint hidden from docs @app.get("/hidden", include_in_schema=False) async def hidden(): return {"message": "Hidden from docs"}
Quick Reference
Summary tips for excluding endpoints from FastAPI docs:
- Use
include_in_schema=Falsein route decorators. - This works for all HTTP methods:
@app.get(),@app.post(), etc. - Hidden endpoints still work normally; they just don't show in docs.
- Use this to hide internal or private APIs from public docs.