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
An optional path prefix for the router. Must start with
/ and not end with /.A list of tags to be applied to all path operations in this router. It will be added to the generated OpenAPI.
A list of dependencies to be applied to all path operations in this router.
The default response class to be used for all path operations in this router.
Additional responses to be shown in OpenAPI for all path operations in this router.
OpenAPI callbacks that should apply to all path operations in this router.
Whether to detect and redirect slashes in URLs when the client doesn’t use the same format.
Mark all path operations in this router as deprecated in the generated OpenAPI.
Whether to include all path operations in this router in the generated OpenAPI.
A Lifespan context manager handler for startup and shutdown events.
Custom route class to be used by this router.
Customize the function used to generate unique IDs for path operations.
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:Pydantic model for response validation.
Default status code for the response.
Tags for OpenAPI documentation.
Additional dependencies for this specific route.
Short summary for OpenAPI documentation.
Detailed description for OpenAPI documentation.
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.The APIRouter to include.
URL path prefix for all routes in the included router.
Additional tags to be applied to all routes.
Additional dependencies to be applied to all routes.
Mark all routes as deprecated.
Include routes in OpenAPI schema.
add_api_websocket_route()
add_api_websocket_route()
add_api_websocket_route(path, endpoint, name=None, *, dependencies=None)
Add a WebSocket route programmatically.WebSocket path.
WebSocket endpoint function.
Name for the WebSocket route.
Dependencies for this WebSocket route.
websocket()
websocket()
Attributes
The path prefix for this router.
Tags applied to all routes in this router.
Dependencies applied to all routes in this router.
List of all routes registered in this router.
Whether all routes are marked as deprecated.
Whether routes are included in the OpenAPI schema.