Path Operation Advanced Configuration

Warning

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

But you can help translating it: Contributing.

OpenAPI operationId

Warning

If you are not an “expert” in OpenAPI, you probably don’t need this.

You can set the OpenAPI operationId to be used in your path operation with the parameter operation_id.

You would have to make sure that it is unique for each operation.

  1. from fastapi import FastAPI
  2. app = FastAPI()
  3. @app.get("/items/", operation_id="some_specific_id_you_define")
  4. async def read_items():
  5. return [{"item_id": "Foo"}]

Using the path operation function name as the operationId

If you want to use your APIs’ function names as operationIds, you can iterate over all of them and override each path operation’s operation_id using their APIRoute.name.

You should do it after adding all your path operations.

  1. from fastapi import FastAPI
  2. from fastapi.routing import APIRoute
  3. app = FastAPI()
  4. @app.get("/items/")
  5. async def read_items():
  6. return [{"item_id": "Foo"}]
  7. def use_route_names_as_operation_ids(app: FastAPI) -> None:
  8. """
  9. Simplify operation IDs so that generated API clients have simpler function
  10. names.
  11. Should be called only after all routes have been added.
  12. """
  13. for route in app.routes:
  14. if isinstance(route, APIRoute):
  15. route.operation_id = route.name # in this case, 'read_items'
  16. use_route_names_as_operation_ids(app)

Tip

If you manually call app.openapi(), you should update the operationIds before that.

Warning

If you do this, you have to make sure each one of your path operation functions has a unique name.

Even if they are in different modules (Python files).

Exclude from OpenAPI

To exclude a path operation from the generated OpenAPI schema (and thus, from the automatic documentation systems), use the parameter include_in_schema and set it to False;

  1. from fastapi import FastAPI
  2. app = FastAPI()
  3. @app.get("/items/", include_in_schema=False)
  4. async def read_items():
  5. return [{"item_id": "Foo"}]

Advanced description from docstring

You can limit the lines used from the docstring of a path operation function for OpenAPI.

Adding an \f (an escaped “form feed” character) causes FastAPI to truncate the output used for OpenAPI at this point.

It won’t show up in the documentation, but other tools (such as Sphinx) will be able to use the rest.

  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. \f
  21. :param item: User input.
  22. """
  23. return item