How can i restrict the routes that are shown in the OpenAI apidocs when using django ninja
- User
How can i restrict the routes that are shown in the OpenAI apidocs when using django ninja
- Assistant
To restrict or hide routes from being shown in the OpenAPI docs generated by Django Ninja, you can use the
include_in_schema=Falseparameter when defining your API routes.This is useful when you want to exclude internal, admin, or otherwise private/debug endpoints from appearing in the automatically generated API documentation at
/api/docsor/api/openapi.json.
✅ How to Hide a Route from OpenAPI Docs in Django Ninja
When declaring a route in your Ninja API, use the
include_in_schema=Falseparameter like this:from ninja import NinjaAPI api = NinjaAPI() @api.get("/hidden-endpoint", include_in_schema=False) def hidden_endpoint(request): return {"message": "This endpoint will not appear in the OpenAPI docs"}
✅ Hiding Multiple Routes (Example)
from ninja import Router, NinjaAPI api = NinjaAPI() router = Router() @router.get("/visible") def visible(request): return {"message": "You can see me in the docs"} @router.get("/invisible", include_in_schema=False) def invisible(request): return {"message": "You can't see me in the docs"} api.add_router("/example", router)Only
/example/visiblewill show in the Swagger UI.
🔐 Bonus: Securing "Hidden" Routes
Note that hiding a route from the OpenAPI schema does not make it secure — it’ll still be accessible if someone knows the endpoint.
To make it secure:
- Add authentication
- Use permission classes
- Disable/404 in production if necessary
Example with
HttpBearerAuth:from ninja.security import HttpBearer class AuthBearer(HttpBearer): def authenticate(self, request, token): if token == "secret-token": return token api = NinjaAPI(auth=AuthBearer()) @api.get("/secure", include_in_schema=False) def secure_view(request): return {"secret": "This data is protected"}
✅ Summary
Objective Solution Hide endpoint from docs include_in_schema=FalseKeep endpoint functional Still accessible via URL Actually secure it Use authentication/authorization
Let me know if you want to customize the docs UI or show only certain tags — Django Ninja supports that too!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.