axum
Ergonomic modular web framework for Rust on tokio with extractors, tower middleware, and typed handlers.
Official Documentation ↗v0.8. Check the official docs for the latest API surface.Installation
cargo add axumMethods
No methods match this filter.
Ergonomic modular web framework for Rust on tokio with extractors, tower middleware, and typed handlers.
Overview
axum is a web framework focused on composition: handlers are plain async functions, extractors decode request pieces into typed values, and responses are anything implementing IntoResponse. Routing, state, and middleware come together without macros governing your code.
It is built on tokio and tower, so the middleware ecosystem of tower-http applies directly. Error handling through Result plus IntoResponse keeps failure mapping in one place, and the type system rejects invalid handler signatures at compile time.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
Router / route— Builds the application routing table; handlers attach per HTTP method.axum::serve— Runs the router on a tokio listener; the v0.7+ replacement for Server::bind.extractors— Typed arguments that decode request parts; the body consumer must come last.with_state— Shares state such as pools and clients with handlers through a typed extractor.error handling— Handlers may return Result; custom error types map variants to status codes centrally.
Usage Notes
- Order extractors with the body consumer last to satisfy the compiler once and for all.
- Share state through with_state and typed State extraction instead of globals.
- Return typed errors implementing IntoResponse instead of unwrapping in handlers.
Common Pitfalls
- Handlers that panic take down the connection path; pair with tower catch-panics layers in production.
- Blocking calls inside handlers stall the runtime; use spawn_blocking for CPU-heavy work.
- Route path syntax differs across versions, verify :id versus {id} style for your axum version.
FAQ
- Why does my extractor ordering fail to compile?
- Axum requires the body-consuming extractor, Json or body itself, to be the last argument because it takes the request stream. Positional reorder, Path, Query, then Json, resolves the compile error.
- How do I add middleware?
- Route layers compose with .layer, and tower and tower-http provide the ecosystem: TraceLayer, CorsLayer, timeouts, and rate limits. Wrap Router-wide with .layer on the router or per route with MethodRouter layers.
- What changed in axum 0.7 and 0.8?
- 0.7 replaced hyper Server::bind with axum::serve and tokio listeners; 0.8 changed path parameter syntax from :id wildcards to the new form and refined extractors. Migrate deliberately, since route strings are affected.
Report a problem with this API docOpens a pre-filled issue on GitHub.