Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate FastApi documentation into sections

Currently the OpenAPI documentation looks like this: documentation aspect Is it possible to separate it into multiple sections?

For example, 2 sections, one being the "books" section that contains the methods from "/api/bookcollection/books/" endpoints and the other containing the endpoints with "/api/bookcollection/authors/".

I have consulted the FastApi documentation, but I do not find anything close to the operation I want to do.

like image 989
jbolt Avatar asked Jul 23 '26 00:07

jbolt


2 Answers

The OpenAPI allows the use of tags to group endpoints. FastAPI also supports this feature. The documentation section can be found here.

Example:

from fastapi import FastAPI

tags_metadata = [
    {
        "name": "users",
        "description": "Operations with users. The **login** logic is also here.",
    },
    {
        "name": "items",
        "description": "Manage items. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Items external docs",
            "url": "https://fastapi.tiangolo.com/",
        },
    },
]

app = FastAPI(openapi_tags=tags_metadata)


@app.get("/users/", tags=["users"])
async def get_users():
    return [{"name": "Harry"}, {"name": "Ron"}]


@app.get("/items/", tags=["items"])
async def get_items():
    return [{"name": "wand"}, {"name": "flying broom"}]

enter image description here

like image 170
alex_noname Avatar answered Jul 24 '26 14:07

alex_noname


Another solution for this would be just to create different routers for every resource that you want to have in different documentation sections.

source

like image 45
jbolt Avatar answered Jul 24 '26 13:07

jbolt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!