Skip to main content

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

AttributeTypeDescription
pathstrThe URL path pattern for this route used to match incoming requests.
endpointCallable[..., Any]The callable function or class instance that handles requests for this route.
stream_item_type`AnyNone`
response_modelAnyThe model used for data validation and serialization of the response body.
summary`strNone`
response_descriptionstr = Successful ResponseThe description for the successful response in the generated OpenAPI schema.
deprecated`boolNone`
operation_id`strNone`
response_model_include`IncExNone`
response_model_exclude`IncExNone`
response_model_by_aliasbool = TrueWhether to use the field alias for serialization if one is defined in the response model.
response_model_exclude_unsetbool = FalseWhether to exclude fields that were not explicitly set in the response model.
response_model_exclude_defaultsbool = FalseWhether to exclude fields that have their default values in the response model.
response_model_exclude_nonebool = FalseWhether to exclude fields that have a value of None in the response model.
include_in_schemabool = TrueWhether this route should be included in the generated 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 route, defaulting to the endpoint function name if not provided.
methodsset[str]The set of HTTP methods (e.g., GET, POST) that this route handles.
unique_idstrThe unique identifier for the route, derived from the operation_id or generated via a function.
status_code`intNone`
response_field`ModelFieldNone`
stream_item_field`ModelFieldNone`
dependencieslist[params.Depends]A list of dependencies to be executed before the endpoint is called.
descriptionstrA detailed description of the route, extracted from the endpoint docstring and truncated at the first form feed character.
response_fields`dict[intstr, 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`ModelFieldNone`
is_sse_streamboolA boolean flag indicating if the route is a generator that should stream as Server-Sent Events.
is_json_streamboolA boolean flag indicating if the route is a generator that should stream as JSON Lines.
appCallableThe 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

NameTypeDescription
pathstrThe URL path for the route.
endpointCallable[..., Any]The function to be called when the route is matched.
response_modelAny = Default(None)The model to use for the response body.
status_code`intNone` = None
tags`list[strEnum]
dependencies`Sequence[params.Depends]None` = None
summary`strNone` = None
description`strNone` = None
response_descriptionstr = Successful ResponseThe description for the response in OpenAPI.
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 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 default values from the response model.
response_model_exclude_nonebool = FalseWhether to exclude None values from the response model.
include_in_schemabool = TrueWhether to include the 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)

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

NameTypeDescription
pathstrThe URL path pattern for this route.
endpointCallable[..., Any]The function or callable to be executed when the route is matched.
response_modelAny = Default(None)The data model used for serializing and validating the response body.
status_code`intNone` = None
tags`list[strEnum]
dependencies`Sequence[params.Depends]None` = None
summary`strNone` = None
description`strNone` = None
response_descriptionstr = "Successful Response"The description for the successful response in the OpenAPI schema.
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 defined in the model during serialization.
response_model_exclude_unsetbool = FalseWhether to exclude fields that were not explicitly set in the model instance.
response_model_exclude_defaultsbool = FalseWhether to exclude fields that have their default values.
response_model_exclude_nonebool = FalseWhether to exclude fields that have a value of None.
include_in_schemabool = TrueWhether this route should be included in the generated 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)]]

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

TypeDescription
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

NameTypeDescription
scopeScopeThe ASGI scope dictionary representing the current connection or request.

Returns

TypeDescription
tuple[Match, Scope]A tuple containing the Match status and the potentially modified ASGI scope.