pub struct APIRouter { /* private fields */ }Expand description
Router for grouping related routes with shared configuration.
APIRouter allows you to organize routes into logical groups with
common prefixes, tags, dependencies, and other shared settings.
§Example
let users_router = APIRouter::new()
.prefix("/users")
.tags(vec!["users"])
.get("", list_users)
.get("/{id}", get_user)
.post("", create_user);
let items_router = APIRouter::new()
.prefix("/items")
.tags(vec!["items"])
.get("", list_items);
let app = App::builder()
.include_router(users_router)
.include_router(items_router)
.build();Implementations§
Source§impl APIRouter
impl APIRouter
Sourcepub fn with_prefix(prefix: impl Into<String>) -> Self
pub fn with_prefix(prefix: impl Into<String>) -> Self
Creates a new router with the given prefix.
Sourcepub fn prefix(self, prefix: impl Into<String>) -> Self
pub fn prefix(self, prefix: impl Into<String>) -> Self
Sets the URL prefix for all routes.
The prefix is prepended to all route paths when the router is included in an application.
Sets the default tags for all routes.
Tags are used for organizing routes in OpenAPI documentation. Route-specific tags are merged with these router-level tags.
Sourcepub fn dependency(self, dep: RouterDependency) -> Self
pub fn dependency(self, dep: RouterDependency) -> Self
Adds a dependency that runs before all routes.
Dependencies are executed in the order they are added. If a dependency returns an error response, subsequent dependencies and the route handler are not executed.
Sourcepub fn dependencies(self, deps: Vec<RouterDependency>) -> Self
pub fn dependencies(self, deps: Vec<RouterDependency>) -> Self
Adds multiple dependencies.
Sourcepub fn response(self, status_code: u16, def: ResponseDef) -> Self
pub fn response(self, status_code: u16, def: ResponseDef) -> Self
Adds a response definition for OpenAPI documentation.
Sourcepub fn responses(self, responses: HashMap<u16, ResponseDef>) -> Self
pub fn responses(self, responses: HashMap<u16, ResponseDef>) -> Self
Sets shared response definitions.
Sourcepub fn deprecated(self, deprecated: bool) -> Self
pub fn deprecated(self, deprecated: bool) -> Self
Marks all routes as deprecated.
Sourcepub fn include_in_schema(self, include: bool) -> Self
pub fn include_in_schema(self, include: bool) -> Self
Sets whether routes should be included in OpenAPI schema.
Sourcepub fn route<H, Fut>(
self,
path: impl Into<String>,
method: Method,
handler: H,
) -> Self
pub fn route<H, Fut>( self, path: impl Into<String>, method: Method, handler: H, ) -> Self
Adds a route with the given method and path.
Sourcepub fn options<H, Fut>(self, path: impl Into<String>, handler: H) -> Self
pub fn options<H, Fut>(self, path: impl Into<String>, handler: H) -> Self
Adds an OPTIONS route.
Sourcepub fn include_router(self, other: APIRouter) -> Self
pub fn include_router(self, other: APIRouter) -> Self
Includes another router’s routes with an optional additional prefix.
This allows nesting routers for hierarchical organization.
§Example
let v1_users = APIRouter::new()
.prefix("/users")
.get("", list_users);
let v1_router = APIRouter::new()
.prefix("/v1")
.include_router(v1_users);
// Routes: /v1/usersSourcepub fn include_router_with_config(
self,
other: APIRouter,
config: IncludeConfig,
) -> Self
pub fn include_router_with_config( self, other: APIRouter, config: IncludeConfig, ) -> Self
Includes another router with configuration overrides.
This allows applying additional configuration when including a router, such as prepending a prefix, adding tags, or injecting dependencies.
§Merge Rules
Following FastAPI’s merge semantics:
- prefix: config.prefix + router.prefix + route.path
- tags: config.tags + router.tags + route.tags
- dependencies: config.deps + router.deps + route.deps
- responses: route > router > config (later values win)
- deprecated: config overrides router if set
- include_in_schema: config overrides router if set
§Example
let users_router = APIRouter::new()
.prefix("/users")
.get("", list_users);
let config = IncludeConfig::new()
.prefix("/api/v1")
.tags(vec!["api"])
.dependency(auth_dep);
let app_router = APIRouter::new()
.include_router_with_config(users_router, config);
// Routes: /api/v1/users with ["api", "users"] tagsSourcepub fn get_prefix(&self) -> &str
pub fn get_prefix(&self) -> &str
Returns the prefix for this router.
Returns the tags for this router.
Sourcepub fn get_dependencies(&self) -> &[RouterDependency]
pub fn get_dependencies(&self) -> &[RouterDependency]
Returns the dependencies for this router.
Sourcepub fn get_responses(&self) -> &HashMap<u16, ResponseDef>
pub fn get_responses(&self) -> &HashMap<u16, ResponseDef>
Returns the response definitions.
Sourcepub fn is_deprecated(&self) -> Option<bool>
pub fn is_deprecated(&self) -> Option<bool>
Returns whether routes are deprecated.
Sourcepub fn get_include_in_schema(&self) -> bool
pub fn get_include_in_schema(&self) -> bool
Returns whether routes should be included in schema.
Sourcepub fn get_routes(&self) -> &[RouterRoute]
pub fn get_routes(&self) -> &[RouterRoute]
Returns the routes in this router.
Sourcepub fn into_route_entries(self) -> Vec<RouteEntry>
pub fn into_route_entries(self) -> Vec<RouteEntry>
Converts router routes to RouteEntry values for the app.
This applies the router’s prefix, tags, and dependencies to all routes.
The returned routes can be added to an AppBuilder.