Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

FastAPI · Class

APIRouter

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.

How it works

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.

Syntax

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))

Parameters

NameTypeDescription
prefixstrOptional path prefix for the router.
tagslist[str | Enum] | NoneTags applied to all path operations and included in generated OpenAPI.
dependenciesSequence[Depends] | NoneDependencies applied to the router's path operations.
default_response_classtype[Response]Default response class for responses from the router.
responsesdict[int | str, dict[str, Any]] | NoneAdditional responses included in the generated OpenAPI.
callbackslist[BaseRoute] | NoneCallback routes associated with operations in the router.
routeslist[BaseRoute] | NoneInitial routes for the router.
redirect_slashesboolWhether to redirect slash variants of paths.
defaultAnyDefault value used for route behavior where supported.
dependency_overrides_providerAnyProvider used for dependency overrides.
route_classtype[APIRoute]Route class used when creating routes.
on_startupSequence[Callable[[], Any]] | NoneStartup handlers for the router.

Returns

An APIRouter instance.

Key points

  • The class provides a complete routing abstraction with constructor options, attributes, and methods.
  • Import it directly from the top-level 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.
  • Router configuration can centralize parameters, responses, dependencies, tags, and comparable operation settings.
  • A router can be mounted more than once, allowing different prefixes for reuse.

Common mistakes

  • Including one router repeatedly is supported, but each inclusion can apply a different prefix.
  • Do not modify the routes collection directly after inclusion; use documented decorators or include_router instead.

Example: Grouped routes with APIRouter

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.

When to use it

Modular route organization

Organize endpoints by feature or domain and mount the group in a FastAPI application.

Shared route configuration

Apply a common URL prefix, tags, dependencies, or response configuration to related operations.

Router reuse

Reuse the same route group under multiple URL prefixes.

OpenAPI callbacks

Define callback operations whose routes contribute to generated OpenAPI callback documentation.

Interview questions

QWhat problem does APIRouter solve?

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.

QHow do shared router options affect its operations?

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.

QHow are callback routes attached to an API operation?

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.

  • APIRoute
  • FastAPI
  • include_router
  • Depends
  • Default
  • generate_unique_id

Source: APIRouter class - FastAPI. Checked 2026-10-15.