2020-01-18 16:21:54 +01:00
There are several parameters that you can pass to your * path operation decorator * to configure it.
2018-12-15 14:39:11 +04:00
!!! warning
2020-01-18 16:21:54 +01:00
Notice that these parameters are passed directly to the * path operation decorator * , not to your * path operation function * .
2018-12-15 14:39:11 +04:00
## Response Status Code
2020-01-18 16:21:54 +01:00
You can define the (HTTP) `status_code` to be used in the response of your * path operation * .
2018-12-15 14:39:11 +04:00
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 from `starlette` :
2018-12-18 22:55:30 +04:00
```Python hl_lines="5 18"
2018-12-21 16:22:33 +04:00
{!./src/path_operation_configuration/tutorial001.py!}
2018-12-15 14:39:11 +04:00
` ``
That status code will be used in the response and will be added to the OpenAPI schema.
## Tags
2020-01-18 16:21:54 +01:00
You can add tags to your *path operation*, pass the parameter ` tags` with a ` list` of ` str` (commonly just one ` str`):
2018-12-15 14:39:11 +04:00
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="17 22 27"
2018-12-21 16:22:33 +04:00
{!./src/path_operation_configuration/tutorial002.py!}
2018-12-15 14:39:11 +04:00
` ``
They will be added to the OpenAPI schema and used by the automatic documentation interfaces:
<img src="/img/tutorial/path-operation-configuration/image01.png">
## Summary and description
You can add a ` summary` and ` description`:
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="20 21"
2018-12-21 16:22:33 +04:00
{!./src/path_operation_configuration/tutorial003.py!}
2018-12-15 14:39:11 +04:00
` ``
## Description from docstring
2020-01-18 16:21:54 +01:00
As descriptions tend to be long and cover multiple lines, you can declare the *path operation* description in the function <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr> and **FastAPI** will read it from there.
2018-12-15 14:39:11 +04:00
2020-01-18 16:21:54 +01:00
You can write <a href="https://en.wikipedia.org/wiki/Markdown" class="external-link" target="_blank">Markdown</a> in the docstring, it will be interpreted and displayed correctly (taking into account docstring indentation).
2019-04-16 22:49:18 +04:00
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="19 20 21 22 23 24 25 26 27"
2018-12-21 16:22:33 +04:00
{!./src/path_operation_configuration/tutorial004.py!}
2018-12-15 14:39:11 +04:00
` ``
It will be used in the interactive docs:
<img src="/img/tutorial/path-operation-configuration/image02.png">
## Response description
You can specify the response description with the parameter ` response_description`:
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="21"
2018-12-21 16:22:33 +04:00
{!./src/path_operation_configuration/tutorial005.py!}
2018-12-15 14:39:11 +04:00
` ``
!!! info
2020-01-18 16:21:54 +01:00
Notice that ` response_description` refers specifically to the response, the ` description` refers to the *path operation* in general.
2018-12-15 14:39:11 +04:00
2020-01-18 16:21:54 +01:00
!!! check
OpenAPI specifies that each *path operation* requires a response description.
2020-01-17 16:24:05 +01:00
2018-12-15 14:39:11 +04:00
So, if you don't provide one, **FastAPI** will automatically generate one of "Successful response".
<img src="/img/tutorial/path-operation-configuration/image03.png">
2020-01-18 16:21:54 +01:00
## Deprecate a *path operation*
2018-12-15 14:39:11 +04:00
2020-01-18 16:21:54 +01:00
If you need to mark a *path operation* as <abbr title="obsolete, recommended not to use it">deprecated</abbr>, but without removing it, pass the parameter ` deprecated`:
2018-12-15 14:39:11 +04:00
` ``Python hl_lines="16"
2018-12-21 16:22:33 +04:00
{!./src/path_operation_configuration/tutorial006.py!}
2018-12-15 14:39:11 +04:00
` ``
It will be clearly marked as deprecated in the interactive docs:
<img src="/img/tutorial/path-operation-configuration/image04.png">
2020-01-18 16:21:54 +01:00
Check how deprecated and non-deprecated * path operations * look like:
2018-12-15 14:39:11 +04:00
<img src="/img/tutorial/path-operation-configuration/image05.png">
## Recap
2020-01-18 16:21:54 +01:00
You can configure and add metadata for your * path operations * easily by passing parameters to the * path operation decorators * .