Routing API

Router

The main router class.

Constructor

Router(prefix: str = "")
Parameter Type Description
prefixstrOptional URL prefix

Methods

add_route

add_route(path: str, endpoint: Callable, *, methods: set[str] | list[str] | tuple[str, ...] = ("GET",), name: str | None = None) -> Route

Add an HTTP route.

add_websocket

add_websocket(path: str, endpoint: Callable, *, name: str | None = None) -> WebSocketRoute

Add a WebSocket route.

route

route(path: str, *, methods: set[str] | list[str] | tuple[str, ...] = ("GET",), name: str | None = None) -> Callable

Decorator for registering an HTTP route.

get

get(path: str, *, name: str | None = None) -> Callable

post

post(path: str, *, name: str | None = None) -> Callable

put

put(path: str, *, name: str | None = None) -> Callable

patch

patch(path: str, *, name: str | None = None) -> Callable

delete

delete(path: str, *, name: str | None = None) -> Callable

head

head(path: str, *, name: str | None = None) -> Callable

options

options(path: str, *, name: str | None = None) -> Callable

websocket

websocket(path: str, *, name: str | None = None) -> Callable

include_router

include_router(other: Router) -> None

match

match(path: str, method: str) -> RouteMatch

Match an HTTP request to a route.

match_websocket

match_websocket(path: str) -> WebSocketMatch

Match a WebSocket connection to a route.

url_for

url_for(name: str, **params: Any) -> str

Generate a URL for a named route.

Properties

  • routes — All HTTP routes
  • websocket_routes — All WebSocket routes
  • route_count — Number of HTTP routes
  • websocket_count — Number of WebSocket routes
  • total_count — Total routes

Route

HTTP route definition.

Attributes

  • path — The URL path pattern
  • endpoint — The endpoint function
  • methods — HTTP methods allowed
  • name — Route name
  • regex — Compiled regex
  • parameters — Parameter names

match_path

match_path(path: str) -> dict[str, Any] | None

WebSocketRoute

WebSocket route definition.

Attributes

  • path — WebSocket URL path pattern
  • endpoint — The endpoint function
  • name — Route name
  • regex — Compiled regex
  • parameters — Parameter names

match_path

match_path(path: str) -> dict[str, Any] | None
Supported Converters

str, int, float, path, uuid, slug