This class, APIRouter, is used to group path operations, for example to structure an app in multiple files. It can then be included in the FastAPI app, or in another APIRouter (ultimately included in the app).
Attributes
| Attribute | Type | Description |
|---|
| lifespan_context | Lifespan[Any] | This attribute stores the lifespan context manager for the router, which handles startup and shutdown events. |
| on_startup | list[Callable[[], Any]] = [] | This attribute stores a list of startup event handler functions, which are executed when the application starts. |
| on_shutdown | list[Callable[[], Any]] = [] | This attribute stores a list of shutdown event handler functions, which are executed when the application shuts down. |
| prefix | str | This attribute stores an optional path prefix for the router, which is prepended to all path operations defined within this router. |
| tags | `list[str | Enum]` = [] |
| dependencies | Sequence[params.Depends] | This attribute stores a list of dependencies to be applied to all the path operations in this router, ensuring that these dependencies are resolved before the path operation function is called. |
| deprecated | `bool | None` |
| include_in_schema | bool | This attribute controls whether all path operations in this router are included in the generated OpenAPI schema, affecting their visibility in the API documentation. |
| responses | `dict[int | str, dict[str, Any]]` = {} |
| callbacks | list[BaseRoute] = [] | This attribute stores OpenAPI callbacks that should apply to all path operations in this router, which are used for defining out-of-band interactions in the OpenAPI documentation. |
| dependency_overrides_provider | `Any | None` |
| route_class | type[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)] | This attribute stores the custom route (path operation) class to be used by this router, allowing for customization of how routes are handled. |
| default_response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] | This attribute stores the default response class to be used for path operations within this router, determining the default serialization and media type of responses. |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] | This attribute stores the function used to generate unique IDs for path operations shown in the generated OpenAPI documentation, which is useful for client generation. |
| strict_content_type | bool | This attribute enables strict checking for request Content-Type headers, affecting how requests with a body are parsed, particularly for JSON content. |
Constructor
Signature
def APIRouter(
prefix: str = "",
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
default_response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
responses: dict[int | str, dict[str, Any]]| None = None,
callbacks: list[BaseRoute]| None = None,
routes: list[BaseRoute]| None = None,
redirect_slashes: bool = True,
default: ASGIApp | None = None,
dependency_overrides_provider: Any | None = None,
route_class: type[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)] = APIRoute,
on_startup: Sequence[Callable[[], Any]]| None = None,
on_shutdown: Sequence[Callable[[], Any]]| None = None,
lifespan: Lifespan[Any]| None = None,
deprecated: bool | None = None,
include_in_schema: bool = True,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id),
strict_content_type: bool = Default(True)
) - > None
Parameters
| Name | Type | Description |
|---|
| prefix | str = "" | An optional path prefix for the router. |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| default_response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | The default response class to be used. Read more in the FastAPI docs for Custom Response - HTML, Stream, File, others. |
| responses | `dict[int | str, dict[str, Any]] |
| callbacks | `list[BaseRoute] | None` = None |
| routes | `list[BaseRoute] | None` = None |
| redirect_slashes | bool = True | Whether to detect and redirect slashes in URLs when the client doesn't use the same format. |
| default | `ASGIApp | None` = None |
| dependency_overrides_provider | `Any | None` = None |
| route_class | type[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)] = APIRoute | Custom route (path operation) class to be used by this router. Read more about it in the FastAPI docs for Custom Request and APIRoute class. |
| on_startup | `Sequence[Callable[[], Any]] | None` = None |
| on_shutdown | `Sequence[Callable[[], Any]] | None` = None |
| lifespan | `Lifespan[Any] | None` = None |
| deprecated | `bool | None` = None |
| include_in_schema | bool = True | To include (or not) all the path operations in this router in the generated OpenAPI. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations. |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients. |
| strict_content_type | bool = Default(True) | Enable strict checking for request Content-Type headers. When True (the default), requests with a body that do not include a Content-Type header will not be parsed as JSON. This prevents potential cross-site request forgery (CSRF) attacks that exploit the browser's ability to send requests without a Content-Type header, bypassing CORS preflight checks. In particular applicable for apps that need to be run locally (in localhost). When False, requests without a Content-Type header will have their body parsed as JSON, which maintains compatibility with certain clients that don't send Content-Type headers. Read more about it in the FastAPI docs for Strict Content-Type. |
Methods
route()
@classmethod
def route(
path: str,
methods: Collection[str]| None = None,
name: str | None = None,
include_in_schema: bool = True
) - > Callable[[DecoratedCallable], DecoratedCallable]
Adds a route to the router. This method is a decorator that registers a function as a handler for a specific URL path and HTTP methods. It allows you to define basic routes without the full API route configuration.
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path for the route. |
| methods | `Collection[str] | None` = None |
| name | `str | None` = None |
| include_in_schema | bool = True | A boolean indicating whether this route should be included in the generated OpenAPI schema (e.g., visible at /docs). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a route handler. |
add_api_route()
@classmethod
def add_api_route(
path: str,
endpoint: Callable[..., Any],
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
methods: set[str]| list[str]| None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)]| DefaultPlaceholder = Default(JSONResponse),
name: str | None = None,
route_class_override: type[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)]| None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str]| DefaultPlaceholder = Default(generate_unique_id),
strict_content_type: bool | DefaultPlaceholder = Default(True)
) - > None
Adds an API route to the router, providing extensive configuration options for the path operation. This method is used internally by the HTTP method decorators (e.g., .get(), .post()) to register path operations with detailed OpenAPI documentation and response handling.
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path for the API route. |
| endpoint | Callable[..., Any] | The function that will handle requests to this route. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| methods | `set[str] | list[str] |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | `type[Response] | DefaultPlaceholder` = Default(JSONResponse) |
| name | `str | None` = None |
| route_class_override | `type[APIRoute] | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | `Callable[[APIRoute], str] | DefaultPlaceholder` = Default(generate_unique_id) |
| strict_content_type | `bool | DefaultPlaceholder` = Default(True) |
Returns
| Type | Description |
|---|
None | This method does not return any value; it modifies the router's internal state by adding a new route. |
api_route()
@classmethod
def api_route(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
methods: list[str]| None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Adds an API route to the router, providing extensive configuration options for the path operation. This method is a decorator that registers a function as a handler for a specific URL path and HTTP methods, allowing for detailed OpenAPI documentation and response handling.
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path for the API route. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| methods | `list[str] | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as an API route handler. |
add_api_websocket_route()
@classmethod
def add_api_websocket_route(
path: str,
endpoint: Callable[..., Any],
name: str | None = None,
dependencies: Sequence[params.Depends]| None = None
) - > None
Adds a WebSocket route to the router. This method registers a function as a handler for a WebSocket connection at a specified URL path, allowing for real-time, bidirectional communication.
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path for the WebSocket route. |
| endpoint | Callable[..., Any] | The function that will handle WebSocket connections for this route. |
| name | `str | None` = None |
| dependencies | `Sequence[params.Depends] | None` = None |
Returns
| Type | Description |
|---|
None | This method does not return any value; it modifies the router's internal state by adding a new WebSocket route. |
websocket()
@classmethod
def websocket(
path: str,
name: str | None = None,
dependencies: Sequence[params.Depends]| None = None
) - > Callable[[DecoratedCallable], DecoratedCallable]
Decorate a WebSocket function. Read more about it in the FastAPI docs for WebSockets (https://fastapi.tiangolo.com/advanced/websockets/).
Parameters
| Name | Type | Description |
|---|
| path | str | WebSocket path. |
| name | `str | None` = None |
| dependencies | `Sequence[params.Depends] | None` = None |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a WebSocket endpoint. |
websocket_route()
@classmethod
def websocket_route(
path: str,
name: str | None = None
) - > Callable[[DecoratedCallable], DecoratedCallable]
Adds a WebSocket route to the router. This method is a decorator that registers a function as a handler for a WebSocket connection at a specified URL path.
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path for the WebSocket route. |
| name | `str | None` = None |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a WebSocket route handler. |
include_router()
@classmethod
def include_router(
router: [APIRouter](apirouter.md?sid=fastapi_routing_apirouter),
prefix: str = "",
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
default_response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
responses: dict[int | str, dict[str, Any]]| None = None,
callbacks: list[BaseRoute]| None = None,
deprecated: bool | None = None,
include_in_schema: bool = True,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > None
Include another APIRouter in the same current APIRouter. Read more about it in the FastAPI docs for Bigger Applications (https://fastapi.tiangolo.com/tutorial/bigger-applications/).
Parameters
| Name | Type | Description |
|---|
| router | [APIRouter](apirouter.md?sid=fastapi_routing_apirouter) | The APIRouter to include. |
| prefix | str = "" | An optional path prefix for the router. |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| default_response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | The default response class to be used. Read more in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class). |
| responses | `dict[int | str, dict[str, Any]] |
| callbacks | `list[BaseRoute] | None` = None |
| deprecated | `bool | None` = None |
| include_in_schema | bool = True | Include (or not) all the path operations in this router in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
None | This method does not return any value; it modifies the current router by adding routes and event handlers from the included router. |
get()
@classmethod
def get(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP GET operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a GET endpoint. |
put()
@classmethod
def put(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP PUT operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a PUT endpoint. |
post()
@classmethod
def post(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP POST operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a POST endpoint. |
delete()
@classmethod
def delete(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP DELETE operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a DELETE endpoint. |
options()
@classmethod
def options(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP OPTIONS operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as an OPTIONS endpoint. |
head()
@classmethod
def head(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP HEAD operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a HEAD endpoint. |
patch()
@classmethod
def patch(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP PATCH operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a PATCH endpoint. |
trace()
@classmethod
def trace(
path: str,
response_model: Any = Default(None),
status_code: int | None = None,
tags: list[str | Enum]| None = None,
dependencies: Sequence[params.Depends]| None = None,
summary: str | None = None,
description: str | None = None,
response_description: str = "Successful Response",
responses: dict[int | str, dict[str, Any]]| None = None,
deprecated: bool | None = None,
operation_id: str | None = None,
response_model_include: IncEx | None = None,
response_model_exclude: IncEx | None = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse),
name: str | None = None,
callbacks: list[BaseRoute]| None = None,
openapi_extra: dict[str, Any]| None = None,
generate_unique_id_function: Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id)
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add a path operation using an HTTP TRACE operation. Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/).
Parameters
| Name | Type | Description |
|---|
| path | str | The URL path to be used for this path operation. For example, in http://example.com/items, the path is /items. |
| response_model | Any = Default(None) | The type to use for the response. It could be any valid Pydantic field type. So, it doesn't have to be a Pydantic model, it could be other things, like a list, dict, etc. It will be used for: Documentation: the generated OpenAPI (and the UI at /docs) will show it as the response (JSON Schema). Serialization: you could return an arbitrary object and the response_model would be used to serialize that object into the corresponding JSON. Filtering: the JSON sent to the client will only contain the data (fields) defined in the response_model. If you returned an object that contains an attribute password but the response_model does not include that field, the JSON sent to the client would not have that password. Validation: whatever you return will be serialized with the response_model, converting any data as necessary to generate the corresponding JSON. But if the data in the object returned is not valid, that would mean a violation of the contract with the client, so it's an error from the API developer. So, FastAPI will raise an error and return a 500 error code (Internal Server Error). Read more about it in the FastAPI docs for Response Model (https://fastapi.tiangolo.com/tutorial/response-model/). |
| status_code | `int | None` = None |
| tags | `list[str | Enum] |
| dependencies | `Sequence[params.Depends] | None` = None |
| summary | `str | None` = None |
| description | `str | None` = None |
| response_description | str = "Successful Response" | The description for the default response. It will be added to the generated OpenAPI (e.g. visible at /docs). |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| operation_id | `str | None` = None |
| response_model_include | `IncEx | None` = None |
| response_model_exclude | `IncEx | None` = None |
| response_model_by_alias | bool = True | Configuration passed to Pydantic to define if the response model should be serialized by alias when an alias is used. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude). |
| response_model_exclude_unset | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that were not set and have their default values. This is different from response_model_exclude_defaults in that if the fields are set, they will be included in the response, even if the value is the same as the default. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_defaults | bool = False | Configuration passed to Pydantic to define if the response data should have all the fields, including the ones that have the same value as the default. This is different from response_model_exclude_unset in that if the fields are set but contain the same default values, they will be excluded from the response. When True, default values are omitted from the response. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter). |
| response_model_exclude_none | bool = False | Configuration passed to Pydantic to define if the response data should exclude fields set to None. This is much simpler (less smart) than response_model_exclude_unset and response_model_exclude_defaults. You probably want to use one of those two instead of this one, as those allow returning None values when it makes sense. Read more about it in the FastAPI docs for Response Model - Return Type (https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none). |
| include_in_schema | bool = True | Include this path operation in the generated OpenAPI schema. This affects the generated OpenAPI (e.g. visible at /docs). Read more about it in the FastAPI docs for Query Parameters and String Validations (https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi). |
| response_class | type[[Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)] = Default(JSONResponse) | Response class to be used for this path operation. This will not be used if you return a response directly. Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others (https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse). |
| name | `str | None` = None |
| callbacks | `list[BaseRoute] | None` = None |
| openapi_extra | `dict[str, Any] | None` = None |
| generate_unique_id_function | Callable[[[APIRoute](apiroute.md?sid=fastapi_routing_apiroute)], str] = Default(generate_unique_id) | Customize the function used to generate unique IDs for the path operations shown in the generated OpenAPI. This is particularly useful when automatically generating clients or SDKs for your API. Read more about it in the FastAPI docs about how to Generate Clients (https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function). |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as a TRACE endpoint. |
add_event_handler()
@classmethod
def add_event_handler(
event_type: str,
func: Callable[[], Any]
) - > None
Add an event handler function for startup or shutdown. This method is kept for backward compatibility after Starlette removed support for on_startup/on_shutdown handlers. Ref: https://github.com/Kludex/starlette/pull/3117
Parameters
| Name | Type | Description |
|---|
| event_type | str | The type of event to handle, either "startup" or "shutdown". |
| func | Callable[[], Any] | The callable function to register as an event handler. |
Returns
on_event()
@classmethod
def on_event(
event_type: str
) - > Callable[[DecoratedCallable], DecoratedCallable]
Add an event handler for the router. on_event is deprecated, use lifespan event handlers instead. Read more about it in the FastAPI docs for Lifespan Events (https://fastapi.tiangolo.com/advanced/events/#alternative-events-deprecated).
Parameters
| Name | Type | Description |
|---|
| event_type | str | The type of event. startup or shutdown. |
Returns
| Type | Description |
|---|
Callable[[DecoratedCallable], DecoratedCallable] | A decorator that registers the decorated function as an event handler. |