APIRoute
This class represents an API route, handling the association of a URL path with an endpoint callable. It manages various aspects of route definition, including response modeling, status codes, dependencies, and OpenAPI schema generation. It extends the base routing.Route class to provide enhanced functionality specific to API endpoints.
Attributes
| Attribute | Type | Description |
|---|---|---|
| path | str | The path string for the API route, used to match incoming requests. |
| endpoint | Callable[..., Any] | The callable function or method that handles the API route. |
| stream_item_type | `Any | None` |
| response_model | `Any | None` |
| summary | `str | None` |
| response_description | str = "Successful Response" | A description of the successful response, used in OpenAPI documentation. |
| deprecated | `bool | None` |
| operation_id | `str | None` |
| response_model_include | `IncEx | None` |
| response_model_exclude | `IncEx | None` |
| response_model_by_alias | bool = true | A boolean indicating whether to serialize response model fields by their alias names. |
| response_model_exclude_unset | bool = false | A boolean indicating whether to exclude fields that were not explicitly set from the response. |
| response_model_exclude_defaults | bool = false | A boolean indicating whether to exclude fields that have their default values from the response. |
| response_model_exclude_none | bool = false | A boolean indicating whether to exclude fields with a value of None from the response. |
| include_in_schema | bool = true | A boolean indicating whether to include the route in the 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 API route, used for URL reversing and in OpenAPI documentation. |
| path_regex | The compiled regular expression used to match the route's path. | |
| path_format | The path string with parameter placeholders, used for URL generation. | |
| param_convertors | A dictionary mapping parameter names to their type converters. | |
| methods | set[str] | A set of HTTP methods (e.g., 'GET', 'POST') that this route responds to. |
| unique_id | A unique identifier for the API route, derived from operation_id or a generated function. | |
| status_code | `int | None` |
| response_field | `ModelField | None` |
| stream_item_field | `ModelField | None` |
| dependencies | list[params.Depends] | A list of dependencies that will be injected into the route's endpoint. |
| description | str | A detailed description of the API route, used in OpenAPI documentation. |
| response_fields | `dict[int | str, ModelField]` |
| dependant | An internal object representing the route's dependencies and their resolution. | |
| _flat_dependant | An internal flattened representation of the route's dependencies. | |
| _embed_body_fields | An internal boolean indicating whether body fields should be embedded. | |
| body_field | The Pydantic model field representing the request body, used for parsing and validation. | |
| is_sse_stream | A boolean indicating if the route is configured to stream Server-Sent Events. | |
| is_json_stream | A boolean indicating if the route is configured to stream JSONL. | |
| app | The ASGI application for this route, handling request-response cycles. |
Constructor
Signature
def APIRoute(
path: str,
endpoint: Callable[..., Any],
response_model: Any = 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)
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| path | str | The URL path for the route. |
| endpoint | Callable[..., Any] | The callable function or method that handles the route's logic. |
| response_model | Any = None | The Pydantic model used for serializing the response body. If not provided, it attempts to infer from the endpoint's return annotation. |
| 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" | A description for the successful response in OpenAPI documentation. |
| 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 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 fields with default values from the response model. |
| response_model_exclude_none | bool = False | Whether to exclude fields with None values from the response model. |
| include_in_schema | bool = True | Whether to include this 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) |
Methods
get_route_handler()
@classmethod
def get_route_handler() - > Callable[[Request], Coroutine[Any, Any, [Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)]]
Returns a callable request handler for the route. This handler processes incoming requests, applies dependencies, and formats the response.
Returns
| Type | Description |
|---|---|
Callable[[Request], Coroutine[Any, Any, [Response](../openapi/models/response.md?sid=fastapi_openapi_models_response)]] | A callable that takes a Request and returns a Coroutine yielding a Response. |
matches()
@classmethod
def matches(
scope: Scope
) - > tuple[Match, Scope]
Checks if the route matches the given scope and returns a match result along with the updated scope. This method is used internally by the routing system to determine if an incoming request should be handled by this route.
Parameters
| Name | Type | Description |
|---|---|---|
| scope | Scope | The ASGI scope representing the incoming request. |
Returns
| Type | Description |
|---|---|
tuple[Match, Scope] | A tuple containing a Match enum indicating the match status and the updated scope. |