Path Operation Configuration

Warning

The current page still doesn’t have a translation for this language.

But you can help translating it: Contributing.

There are several parameters that you can pass to your path operation decorator to configure it.

Warning

Notice that these parameters are passed directly to the path operation decorator, not to your path operation function.

Response Status Code

You can define the (HTTP) status_code to be used in the response of your path operation.

You can pass directly the int code, like 404.

But if you don’t remember what each number code is for, you can use the shortcut constants in status:

  1. from typing import Optional, Set
  2. from fastapi import FastAPI, status
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. tags: Set[str] = []
  11. @app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
  12. async def create_item(item: Item):
  13. return item

That status code will be used in the response and will be added to the OpenAPI schema.

Technical Details

You could also use from starlette import status.

FastAPI provides the same starlette.status as fastapi.status just as a convenience for you, the developer. But it comes directly from Starlette.

Tags

You can add tags to your path operation, pass the parameter tags with a list of str (commonly just one str):

  1. from typing import Optional, Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. tags: Set[str] = []
  11. @app.post("/items/", response_model=Item, tags=["items"])
  12. async def create_item(item: Item):
  13. return item
  14. @app.get("/items/", tags=["items"])
  15. async def read_items():
  16. return [{"name": "Foo", "price": 42}]
  17. @app.get("/users/", tags=["users"])
  18. async def read_users():
  19. return [{"username": "johndoe"}]

They will be added to the OpenAPI schema and used by the automatic documentation interfaces:

Path Operation Configuration - 图1

Summary and description

You can add a summary and description:

  1. from typing import Optional, Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. tags: Set[str] = []
  11. @app.post(
  12. "/items/",
  13. response_model=Item,
  14. summary="Create an item",
  15. description="Create an item with all the information, name, description, price, tax and a set of unique tags",
  16. )
  17. async def create_item(item: Item):
  18. return item

Description from docstring

As descriptions tend to be long and cover multiple lines, you can declare the path operation description in the function docstring and FastAPI will read it from there.

You can write Markdown in the docstring, it will be interpreted and displayed correctly (taking into account docstring indentation).

  1. from typing import Optional, Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. tags: Set[str] = []
  11. @app.post("/items/", response_model=Item, summary="Create an item")
  12. async def create_item(item: Item):
  13. """
  14. Create an item with all the information:
  15. - **name**: each item must have a name
  16. - **description**: a long description
  17. - **price**: required
  18. - **tax**: if the item doesn't have tax, you can omit this
  19. - **tags**: a set of unique tag strings for this item
  20. """
  21. return item

It will be used in the interactive docs:

Path Operation Configuration - 图2

Response description

You can specify the response description with the parameter response_description:

  1. from typing import Optional, Set
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. tags: Set[str] = []
  11. @app.post(
  12. "/items/",
  13. response_model=Item,
  14. summary="Create an item",
  15. response_description="The created item",
  16. )
  17. async def create_item(item: Item):
  18. """
  19. Create an item with all the information:
  20. - **name**: each item must have a name
  21. - **description**: a long description
  22. - **price**: required
  23. - **tax**: if the item doesn't have tax, you can omit this
  24. - **tags**: a set of unique tag strings for this item
  25. """
  26. return item

Info

Notice that response_description refers specifically to the response, the description refers to the path operation in general.

Check

OpenAPI specifies that each path operation requires a response description.

So, if you don’t provide one, FastAPI will automatically generate one of “Successful response”.

Path Operation Configuration - 图3

Deprecate a path operation

If you need to mark a path operation as deprecated, but without removing it, pass the parameter deprecated:

  1. from fastapi import FastAPI
  2. app = FastAPI()
  3. @app.get("/items/", tags=["items"])
  4. async def read_items():
  5. return [{"name": "Foo", "price": 42}]
  6. @app.get("/users/", tags=["users"])
  7. async def read_users():
  8. return [{"username": "johndoe"}]
  9. @app.get("/elements/", tags=["items"], deprecated=True)
  10. async def read_elements():
  11. return [{"item_id": "Foo"}]

It will be clearly marked as deprecated in the interactive docs:

Path Operation Configuration - 图4

Check how deprecated and non-deprecated path operations look like:

Path Operation Configuration - 图5

Recap

You can configure and add metadata for your path operations easily by passing parameters to the path operation decorators.