APIRoute
This class defines a single API route, mapping a specific URL path and HTTP methods to an executable endpoint function. It manages comprehensive metadata for the route, including response modeling, dependency injection, and OpenAPI schema generation. Additionally, it handles the validation and serialization of request and response data, supporting both standard and streaming responses.
Attributes
| Attribute | Type | Description |
|---|---|---|
| path | str | The URL path pattern for this route used to match incoming requests. |
| endpoint | Callable[..., Any] | The callable function or class instance that handles requests for this route. |
| stream_item_type | `Any | None` |
| response_model | Any | The model used for data validation and serialization of the response body. |
| summary | `str | None` |
| response_description | str = Successful Response | The description for the successful response in the generated OpenAPI schema. |
| deprecated | `bool | None` |
| operation_id | `str | None` |
| response_model_include | `IncEx | None` |
| response_model_exclude | `IncEx | None` |
| response_model_by_alias | bool = True | Whether to use the field alias for serialization if one is defined in the response model. |
| response_model_exclude_unset | bool = False | Whether to exclude fields that were not explicitly set in the response model. |
| response_model_exclude_defaults | bool = False | Whether to exclude fields that have their default values in the response model. |
| response_model_exclude_none | bool = False | Whether to exclude fields that have a value of None in the response model. |
| include_in_schema | bool = True | Whether this route should be included in the generated OpenAPI schema. |
| response_class | `type[Response] | DefaultPlaceholder` |
| dependency_overrides_provider | `Any | None` |
| callbacks | `list[BaseRoute] | None` |
| openapi_extra | `dict[str, Any] | None` |
| generate_unique_id_function | `Callable[["APIRoute"], str] | DefaultPlaceholder` |
| strict_content_type | `bool | DefaultPlaceholder` |
| tags | `list[str | Enum]` |
| responses | `dict[int | str, dict[str, Any]]` |
| name | str | The name of the route, defaulting to the endpoint function name if not provided. |
| methods | set[str] | The set of HTTP methods (e.g., GET, POST) that this route handles. |
| unique_id | str | The unique identifier for the route, derived from the operation_id or generated via a function. |
| status_code | `int | None` |
| response_field | `ModelField | None` |
| stream_item_field | `ModelField | None` |
| dependencies | list[params.Depends] | A list of dependencies to be executed before the endpoint is called. |
| description | str | A detailed description of the route, extracted from the endpoint docstring and truncated at the first form feed character. |
| response_fields | `dict[int | str, ModelField]` |
| dependant | [Dependant](../dependencies/models/dependant.md?sid=fastapi_dependencies_models_dependant) | An internal object containing metadata about the endpoint's parameters and dependencies. |
| body_field | `ModelField | None` |
| is_sse_stream | bool | A boolean flag indicating if the route is a generator that should stream as Server-Sent Events. |
| is_json_stream | bool | A boolean flag indicating if the route is a generator that should stream as JSON Lines. |
| app | Callable | The final ASGI application callable that handles the request-response cycle for this route. |
Constructor
Signature
def APIRoute(
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,
name: str | 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),
dependency_overrides_provider: Any | 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)
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| path | str | The URL path for the route. |
| endpoint | Callable[..., Any] | The function to be called when the route is matched. |
| response_model | Any = Default(None) | The model to use for the response body. |
| 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 response in OpenAPI. |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| name | `str | 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 | Whether to use aliases for the response model. |
| response_model_exclude_unset | bool = False | Whether to exclude unset fields from the response model. |
| response_model_exclude_defaults | bool = False | Whether to exclude default values from the response model. |
| response_model_exclude_none | bool = False | Whether to exclude None values from the response model. |
| include_in_schema | bool = True | Whether to include the route in the OpenAPI schema. |
| response_class | `type[Response] | DefaultPlaceholder` = Default(JSONResponse) |
| dependency_overrides_provider | `Any | 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) |
Signature
def APIRoute(
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,
name: str | 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),
dependency_overrides_provider: Any | 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)
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| path | str | The URL path pattern for this route. |
| endpoint | Callable[..., Any] | The function or callable to be executed when the route is matched. |
| response_model | Any = Default(None) | The data model used for serializing and validating the response body. |
| 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 successful response in the OpenAPI schema. |
| responses | `dict[int | str, dict[str, Any]] |
| deprecated | `bool | None` = None |
| name | `str | 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 | Whether to use field aliases defined in the model during serialization. |
| response_model_exclude_unset | bool = False | Whether to exclude fields that were not explicitly set in the model instance. |
| response_model_exclude_defaults | bool = False | Whether to exclude fields that have their default values. |
| response_model_exclude_none | bool = False | Whether to exclude fields that have a value of None. |
| include_in_schema | bool = True | Whether this route should be included in the generated OpenAPI schema. |
| response_class | `type[Response] | DefaultPlaceholder` = Default(JSONResponse) |
| dependency_overrides_provider | `Any | 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) |
Methods
get_route_handler()
@classmethod
def get_route_handler() - > Callable[[Request], Coroutine[Any, Any, [Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)]]
Builds and returns a callable coroutine that handles the full request-response lifecycle for this route. This handler manages dependency injection, validation, and response serialization.
Returns
| Type | Description |
|---|---|
Callable[[Request], Coroutine[Any, Any, [Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)]] | An asynchronous function that accepts a Request and returns a Response. |
matches()
@classmethod
def matches(
scope: Scope
) - > tuple[Match, Scope]
Checks if the current ASGI scope matches this route's path and methods. If a match is found, it updates the scope with a reference to this route.
Parameters
| Name | Type | Description |
|---|---|---|
| scope | Scope | The ASGI scope dictionary representing the current connection or request. |
Returns
| Type | Description |
|---|---|
tuple[Match, Scope] | A tuple containing the Match status and the potentially modified ASGI scope. |