tezvyn:

Exclude a FastAPI Endpoint from OpenAPI Docs

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate
Exclude a FastAPI Endpoint from OpenAPI Docs

Hide an endpoint from your API docs by setting `include_in_schema=False`. Use this for internal or deprecated endpoints. The footgun: this only hides the endpoint from documentation; it remains fully functional and accessible if the URL is known.

WHY IT EXISTS: Sometimes you need API endpoints that are not part of the public, documented contract. These could be for internal health checks, administrative tasks, or experimental features that you don't want client code generators or public users to see.

THE MENTAL MODEL: Think of include_in_schema=False as putting a "Staff Only" sign on a door in a public building. The door is still there and unlocked, but it's not on the public map, and you're signaling that regular visitors shouldn't use it.

HOW IT WORKS: In the path operation decorator for your endpoint (e.g., @app.get(...)), you add the boolean parameter include_in_schema=False. FastAPI's OpenAPI generation process will then skip this specific path operation when building the openapi.json file. As a result, it will not appear in the automatic documentation UIs like Swagger UI or ReDoc.

WHEN TO USE IT: Use this for endpoints that are: first, purely for internal system use, like a /health check that load balancers hit but users don't need to know about; second, deprecated and awaiting removal, to discourage new usage; or third, highly experimental and subject to change without notice.

WHEN NOT TO USE IT: Do not use include_in_schema=False as a security mechanism. Hiding an endpoint from the docs is security by obscurity, which is not real security. If an endpoint requires restricted access, you must implement proper authentication and authorization, regardless of whether it's in the schema.

ONE CANONICAL EXAMPLE: from fastapi import FastAPI

app = FastAPI() This endpoint is documented and public. @app.get("/users/me") async def read_current_user(): return {"username": "jane.doe"} This internal endpoint is hidden from the docs. @app.get("/internal/status", include_in_schema=False) async def get_internal_status(): return {"status": "ok"}

In this example, a client visiting /docs would see the /users/me endpoint but would be unaware of /internal/status.

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.