Skip to main content
Routing is the process of matching incoming HTTP requests to handler functions based on the request’s path and method. Axum provides a powerful and flexible routing system through the Router type.

Creating a router

Create a new router using Router::new():
A newly created router will respond with 404 Not Found to all requests until you add routes.

Adding routes

Use the route method to add routes to your router. Each route consists of a path pattern and a MethodRouter that defines which HTTP methods are handled:

HTTP method routing

Axum provides convenience functions for each HTTP method:
GET routes also handle HEAD requests automatically, but with the response body removed.

Path patterns

Axum supports dynamic path segments using named parameters:

Wildcard captures

Capture the rest of the path with wildcard parameters:

Combining routes

Chaining routes

Chain multiple route calls to add multiple routes:

Merging routers

Combine multiple routers with merge:

Nesting routers

Nest a router under a path prefix:
Nesting at the root ("/") is not supported. Use merge instead.

Fallback handlers

Handle requests that don’t match any route:

Route services

For more control, you can route to any Tower Service:

Placeholder handlers

You can pass types that implement IntoResponse directly as handlers:

Next steps

Handlers

Learn about handler functions and their signatures

State

Share state across handlers using with_state