Skip to main content

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

AttributeTypeDescription
pathstrThe path string for the API route, used to match incoming requests.
endpointCallable[..., Any]The callable function or method that handles the API route.
stream_item_type`AnyNone`
response_model`AnyNone`
summary`strNone`
response_descriptionstr = "Successful Response"A description of the successful response, used in OpenAPI documentation.
deprecated`boolNone`
operation_id`strNone`
response_model_include`IncExNone`
response_model_exclude`IncExNone`
response_model_by_aliasbool = trueA boolean indicating whether to serialize response model fields by their alias names.
response_model_exclude_unsetbool = falseA boolean indicating whether to exclude fields that were not explicitly set from the response.
response_model_exclude_defaultsbool = falseA boolean indicating whether to exclude fields that have their default values from the response.
response_model_exclude_nonebool = falseA boolean indicating whether to exclude fields with a value of None from the response.
include_in_schemabool = trueA boolean indicating whether to include the route in the OpenAPI schema.
response_class`type[Response]DefaultPlaceholder`
dependency_overrides_provider`AnyNone`
callbacks`list[BaseRoute]None`
openapi_extra`dict[str, Any]None`
generate_unique_id_function`Callable[["APIRoute"], str]DefaultPlaceholder`
strict_content_type`boolDefaultPlaceholder`
tags`list[strEnum]`
responses`dict[intstr, dict[str, Any]]`
namestrThe name of the API route, used for URL reversing and in OpenAPI documentation.
path_regexThe compiled regular expression used to match the route's path.
path_formatThe path string with parameter placeholders, used for URL generation.
param_convertorsA dictionary mapping parameter names to their type converters.
methodsset[str]A set of HTTP methods (e.g., 'GET', 'POST') that this route responds to.
unique_idA unique identifier for the API route, derived from operation_id or a generated function.
status_code`intNone`
response_field`ModelFieldNone`
stream_item_field`ModelFieldNone`
dependencieslist[params.Depends]A list of dependencies that will be injected into the route's endpoint.
descriptionstrA detailed description of the API route, used in OpenAPI documentation.
response_fields`dict[intstr, ModelField]`
dependantAn internal object representing the route's dependencies and their resolution.
_flat_dependantAn internal flattened representation of the route's dependencies.
_embed_body_fieldsAn internal boolean indicating whether body fields should be embedded.
body_fieldThe Pydantic model field representing the request body, used for parsing and validation.
is_sse_streamA boolean indicating if the route is configured to stream Server-Sent Events.
is_json_streamA boolean indicating if the route is configured to stream JSONL.
appThe 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

NameTypeDescription
pathstrThe URL path for the route.
endpointCallable[..., Any]The callable function or method that handles the route's logic.
response_modelAny = NoneThe Pydantic model used for serializing the response body. If not provided, it attempts to infer from the endpoint's return annotation.
status_code`intNone` = None
tags`list[strEnum]
dependencies`Sequence[params.Depends]None` = None
summary`strNone` = None
description`strNone` = None
response_descriptionstr = "Successful Response"A description for the successful response in OpenAPI documentation.
responses`dict[intstr, dict[str, Any]]
deprecated`boolNone` = None
name`strNone` = None
methods`set[str]list[str]
operation_id`strNone` = None
response_model_include`IncExNone` = None
response_model_exclude`IncExNone` = None
response_model_by_aliasbool = TrueWhether to use field aliases for the response model.
response_model_exclude_unsetbool = FalseWhether to exclude unset fields from the response model.
response_model_exclude_defaultsbool = FalseWhether to exclude fields with default values from the response model.
response_model_exclude_nonebool = FalseWhether to exclude fields with None values from the response model.
include_in_schemabool = TrueWhether to include this route in the OpenAPI schema.
response_class`type[Response]DefaultPlaceholder` = Default(JSONResponse)
dependency_overrides_provider`AnyNone` = 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`boolDefaultPlaceholder` = 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

TypeDescription
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

NameTypeDescription
scopeScopeThe ASGI scope representing the incoming request.

Returns

TypeDescription
tuple[Match, Scope]A tuple containing a Match enum indicating the match status and the updated scope.