Search code examples
pythonswaggerfastapiswagger-ui

How to generate a standalone Swagger UI docs page for each endpoint in FastAPI, instead of generating API docs for all the endpoints in one page?


Is there a way to generate each endpoint on its own page instead of generating the documentation for all of the endpoints on a single page like below:

I would like to show, let's say, the GET endpoint for books on its own dedicated page without all the other endpoints. I want this because I am using an iframe tag to embed the UI for a specific endpoint.

enter image description here


Solution

  • You could achieve having separate Swagger UI (OpenAPI) autodocs generated by using Sub applications.

    In the following example, you could access the Swagger UI autodocs for the main API at http://127.0.0.1:8000/docs, and the docs for the sub API at http://127.0.0.1:8000/subapi/docs.

    Example

    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/app")
    def read_main():
        return {"message": "Hello World from main app"}
    
    
    subapi = FastAPI()
    
    
    @subapi.get("/sub")
    def read_sub():
        return {"message": "Hello World from sub API"}
    
    
    app.mount("/subapi", subapi)
    

    If you instead would like to group/sort the endpoints in a single /docs page, please have a look at this answer.