You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Injecting a dependency (like get_current_superuser so only superusers are allowed to access the docs)
importFastAPIfromfastapiimportDependsfromfastapi.openapi.docsimportget_redoc_html, get_swagger_ui_htmlfromfastapi.openapi.utilsimportget_openapifrom .dependenciesimportget_current_superuser# let's just remove the standard docs urlsapplication=FastAPI(
docs_url=None,
redoc_url=None,
openapi_url=None,
title="My API",
version="0.1.0",
)
# and create the router with the dependency we wantdocs_router=APIRouter(dependencies=[Depends(get_current_superuser)])
# and rewrite the endpoints@docs_router.get("/docs", include_in_schema=False)asyncdefget_swagger_documentation() ->fastapi.responses.HTMLResponse:
returnget_swagger_ui_html(openapi_url="/openapi.json", title="docs")
@docs_router.get("/redoc", include_in_schema=False)asyncdefget_redoc_documentation() ->fastapi.responses.HTMLResponse:
returnget_redoc_html(openapi_url="/openapi.json", title="docs")
@docs_router.get("/openapi.json", include_in_schema=False)asyncdefopenapi() ->dict[str, Any]:
out: dict=get_openapi(title=application.title, version=application.version, routes=application.routes)
returnout# finally, let's include these in the original FastAPI applicationapplication.include_router(docs_router)
The text was updated successfully, but these errors were encountered:
This is a question I see people asking a lot, so might be useful.
Removing fastapi docs completely:
Injecting a dependency (like get_current_superuser so only superusers are allowed to access the docs)
The text was updated successfully, but these errors were encountered: