0
0
FastapiHow-ToBeginner · 3 min read

How to Exclude Endpoint from Docs in FastAPI

In FastAPI, you can exclude an endpoint from the automatic API docs by setting 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.
python
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.

python
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."}
Output
Run the app and visit http://127.0.0.1:8000/docs to see only the /hello endpoint documented.
⚠️

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.

python
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=False in 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.

Key Takeaways

Set include_in_schema=False in route decorators to hide endpoints from FastAPI docs.
Hidden endpoints remain accessible; this only affects documentation visibility.
This method works for all HTTP methods and is the cleanest way to exclude endpoints.
Forgetting include_in_schema=False means the endpoint will appear in docs by default.
Use this feature to keep internal or sensitive endpoints out of public API docs.