Skip to content
TheChatArchiveThe Chat Archive
chatgpt-4o-latest-20250326· OpenAIHugging Face LMArena Human Preference 140K

How can i restrict the routes that are shown in the OpenAI apidocs when using django ninja

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-no-thinking’s answer.
  1. User

    How can i restrict the routes that are shown in the OpenAI apidocs when using django ninja

  2. Assistant

    To restrict or hide routes from being shown in the OpenAPI docs generated by Django Ninja, you can use the include_in_schema=False parameter 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/docs or /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=False parameter 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/visible will 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

    ObjectiveSolution
    Hide endpoint from docsinclude_in_schema=False
    Keep endpoint functionalStill accessible via URL
    Actually secure itUse authentication/authorization

    Let me know if you want to customize the docs UI or show only certain tags — Django Ninja supports that too!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.