Modular route organization
Organize endpoints by feature or domain and mount the group in a FastAPI application.
FastAPI · Class
APIRouter groups related FastAPI path operations into a reusable router. Shared prefixes, tags, dependencies, responses, lifecycle handlers, and schema settings can be configured once and applied when the router is included in an application.
APIRouter groups related path operations so they can be configured and included as a unit. Its constructor accepts shared routing, dependency, response, documentation, lifecycle, and schema-generation settings; routes are typically declared with its decorators and then mounted into a FastAPI application.
APIRouter(*, prefix="", tags=None, dependencies=None, default_response_class=Default(JSONResponse), responses=None, callbacks=None, routes=None, redirect_slashes=True, default=None, dependency_overrides_provider=None, route_class=APIRoute, on_startup=None, on_shutdown=None, lifespan=None, deprecated=None, include_in_schema=True, generate_unique_id_function=Default(generate_unique_id), strict_content_type=Default(True))| Name | Type | Description |
|---|---|---|
prefix | str | Optional path prefix for the router. |
tags | list[str | Enum] | None | Tags applied to all path operations and included in generated OpenAPI. |
dependencies | Sequence[Depends] | None | Dependencies applied to the router's path operations. |
default_response_class | type[Response] | Default response class for responses from the router. |
responses | dict[int | str, dict[str, Any]] | None | Additional responses included in the generated OpenAPI. |
callbacks | list[BaseRoute] | None | Callback routes associated with operations in the router. |
routes | list[BaseRoute] | None | Initial routes for the router. |
redirect_slashes | bool | Whether to redirect slash variants of paths. |
default | Any | Default value used for route behavior where supported. |
dependency_overrides_provider | Any | Provider used for dependency overrides. |
route_class | type[APIRoute] | Route class used when creating routes. |
on_startup | Sequence[Callable[[], Any]] | None | Startup handlers for the router. |
An APIRouter instance.
fastapi package.prefix prepends a shared path segment to routes registered on the router.tags applies common OpenAPI tags to every path operation in the router.from fastapi import APIRouter, FastAPI
router = APIRouter(prefix="/items", tags=["items"])
@router.get("/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
app = FastAPI()
app.include_router(router)Creates a router with shared path and OpenAPI metadata, defines a route on it, and includes that router in the application.
Organize endpoints by feature or domain and mount the group in a FastAPI application.
Apply a common URL prefix, tags, dependencies, or response configuration to related operations.
Reuse the same route group under multiple URL prefixes.
Define callback operations whose routes contribute to generated OpenAPI callback documentation.
APIRouter is a smaller routing component that groups path operations. It supports the same kinds of shared configuration as FastAPI, including prefixes, tags, dependencies, responses, and other operation settings.
Follow-up: How would you reuse one router under multiple URL bases? Include the same router multiple times with different prefixes.
Set shared configuration when constructing the router, such as prefix, tags, or dependencies. Individual operations then inherit those settings when the router is included in the application.
Follow-up: Where do router tags appear? They are added to the generated OpenAPI documentation, such as the /docs interface.
Define callback operations on a separate APIRouter, then pass that router's .routes collection to the callbacks argument of the relevant path-operation decorator. The router itself is not passed as the callback value.
Follow-up: Why use .routes here? FastAPI uses those route definitions to generate callback OpenAPI documentation.
APIRouteFastAPIinclude_routerDependsDefaultgenerate_unique_id