Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FastAPI - Add description for path parameter in swagger

Tags:

fastapi

Imagine there is an app like that:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}

How could one add description for path parameter item_id in swagger?

like image 532
Альберт Александров Avatar asked Mar 06 '26 19:03

Альберт Александров


2 Answers

You can add description for a specific parameter by using the description argument to the parameter type object:

item_id: int = Path(..., description="An id representing an item")

The ... represents the default value and should be included*.

Edit*: The current version of FastAPI throws an error if ... is included for Path objects, so leave it out if you're using a recent (2023) version.

like image 178
MatsLindh Avatar answered Mar 09 '26 20:03

MatsLindh


Like this

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    """
    item_id: Your item ID description will be here
    """
    return {"item_id": item_id}
    

swagger doc enter image description here

like image 31
Zilay Avatar answered Mar 09 '26 20:03

Zilay



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!