APIRouter class is used to group path operations together, typically for organizing an application into multiple files. It provides the same interface as FastAPI for defining routes but can be included in the main application or other routers.
Class Signature
Constructor Parameters
str
default:""
An optional path prefix for the router. Must start with
/ and not end with /.list[str | Enum] | None
default:"None"
A list of tags to be applied to all path operations in this router. It will be added to the generated OpenAPI.
Sequence[Depends] | None
default:"None"
A list of dependencies to be applied to all path operations in this router.
type[Response]
default:"JSONResponse"
The default response class to be used for all path operations in this router.
dict[int | str, dict[str, Any]] | None
default:"None"
Additional responses to be shown in OpenAPI for all path operations in this router.
list[BaseRoute] | None
default:"None"
OpenAPI callbacks that should apply to all path operations in this router.
bool
default:"True"
Whether to detect and redirect slashes in URLs when the client doesn’t use the same format.
bool | None
default:"None"
Mark all path operations in this router as deprecated in the generated OpenAPI.
bool
default:"True"
Whether to include all path operations in this router in the generated OpenAPI.
Lifespan[Any] | None
default:"None"
A Lifespan context manager handler for startup and shutdown events.
type[APIRoute]
default:"APIRoute"
Custom route class to be used by this router.
Callable[[APIRoute], str]
default:"generate_unique_id"
Customize the function used to generate unique IDs for path operations.
bool
default:"True"
Enable strict checking for request Content-Type headers.
Methods
Path Operation Decorators
Path Operation Decorators
@router.get(path, **kwargs)
Define a GET endpoint.@router.post(path, **kwargs)
Define a POST endpoint.@router.put(path, **kwargs)
Define a PUT endpoint.@router.patch(path, **kwargs)
Define a PATCH endpoint.@router.delete(path, **kwargs)
Define a DELETE endpoint.@router.options(path, **kwargs)
Define an OPTIONS endpoint.@router.head(path, **kwargs)
Define a HEAD endpoint.@router.trace(path, **kwargs)
Define a TRACE endpoint.All decorators accept the same parameters as FastAPI path operation decorators:Any
Pydantic model for response validation.
int | None
Default status code for the response.
list[str | Enum] | None
Tags for OpenAPI documentation.
Sequence[Depends] | None
Additional dependencies for this specific route.
str | None
Short summary for OpenAPI documentation.
str | None
Detailed description for OpenAPI documentation.
bool | None
Mark this route as deprecated.
add_api_route()
add_api_route()
add_api_route(path, endpoint, **kwargs)
Add an API route programmatically.include_router()
include_router()
include_router(router, *, prefix="", tags=None, dependencies=None, **kwargs)
Include another APIRouter in this router.APIRouter
required
The APIRouter to include.
str
default:""
URL path prefix for all routes in the included router.
list[str | Enum] | None
Additional tags to be applied to all routes.
Sequence[Depends] | None
Additional dependencies to be applied to all routes.
bool | None
Mark all routes as deprecated.
bool
default:"True"
Include routes in OpenAPI schema.
add_api_websocket_route()
add_api_websocket_route()
websocket()
websocket()
Attributes
str
The path prefix for this router.
list[str | Enum]
Tags applied to all routes in this router.
list[Depends]
Dependencies applied to all routes in this router.
list[BaseRoute]
List of all routes registered in this router.
bool | None
Whether all routes are marked as deprecated.
bool
Whether routes are included in the OpenAPI schema.