First, write all your FastAPI application as normally:
fromfastapiimportFastAPIfromfastapi.openapi.utilsimportget_openapiapp=FastAPI()@app.get("/items/")asyncdefread_items():return[{"name":"Foo"}]defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",summary="This is a very custom OpenAPI schema",description="Here's a longer description of the custom **OpenAPI** schema",routes=app.routes,)openapi_schema["info"]["x-logo"]={"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"}app.openapi_schema=openapi_schemareturnapp.openapi_schemaapp.openapi=custom_openapi
Then, use the same utility function to generate the OpenAPI schema, inside a custom_openapi() function:
fromfastapiimportFastAPIfromfastapi.openapi.utilsimportget_openapiapp=FastAPI()@app.get("/items/")asyncdefread_items():return[{"name":"Foo"}]defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",summary="This is a very custom OpenAPI schema",description="Here's a longer description of the custom **OpenAPI** schema",routes=app.routes,)openapi_schema["info"]["x-logo"]={"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"}app.openapi_schema=openapi_schemareturnapp.openapi_schemaapp.openapi=custom_openapi
Now you can add the ReDoc extension, adding a custom x-logo to the info "object" in the OpenAPI schema:
fromfastapiimportFastAPIfromfastapi.openapi.utilsimportget_openapiapp=FastAPI()@app.get("/items/")asyncdefread_items():return[{"name":"Foo"}]defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",summary="This is a very custom OpenAPI schema",description="Here's a longer description of the custom **OpenAPI** schema",routes=app.routes,)openapi_schema["info"]["x-logo"]={"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"}app.openapi_schema=openapi_schemareturnapp.openapi_schemaapp.openapi=custom_openapi
You can use the property .openapi_schema as a "cache", to store your generated schema.
That way, your application won't have to generate the schema every time a user opens your API docs.
It will be generated only once, and then the same cached schema will be used for the next requests.
fromfastapiimportFastAPIfromfastapi.openapi.utilsimportget_openapiapp=FastAPI()@app.get("/items/")asyncdefread_items():return[{"name":"Foo"}]defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",summary="This is a very custom OpenAPI schema",description="Here's a longer description of the custom **OpenAPI** schema",routes=app.routes,)openapi_schema["info"]["x-logo"]={"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"}app.openapi_schema=openapi_schemareturnapp.openapi_schemaapp.openapi=custom_openapi
Now you can replace the .openapi() method with your new function.
fromfastapiimportFastAPIfromfastapi.openapi.utilsimportget_openapiapp=FastAPI()@app.get("/items/")asyncdefread_items():return[{"name":"Foo"}]defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",summary="This is a very custom OpenAPI schema",description="Here's a longer description of the custom **OpenAPI** schema",routes=app.routes,)openapi_schema["info"]["x-logo"]={"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"}app.openapi_schema=openapi_schemareturnapp.openapi_schemaapp.openapi=custom_openapi