Skip to main content
Handlers are async functions that process HTTP requests and produce responses. They form the core logic of your application.

Basic handlers

A handler is any async function that implements the Handler trait:

Handler signatures

Handlers can take extractors as arguments and return any type that implements IntoResponse:
1

Zero or more extractors

Extractors pull data from the request. They must implement FromRequestParts or FromRequest.
2

Return type implements IntoResponse

The return type must be convertible to a response.
3

Must be async

All handlers must be async functions.

Extractors in handlers

Handlers can extract data from requests using extractors:
The order of extractors matters. See Extractors for details.

Return types

Handlers can return various types:

Error handling in handlers

Use Result types to handle errors:
For more complex error handling, see Error Handling.

Converting handlers to services

Handlers can be converted to Tower services:

Applying middleware to handlers

You can apply middleware directly to individual handlers:

Debugging handler errors

Use the #[axum::debug_handler] macro to get better error messages during development:
This macro provides clearer compilation errors when your handler signature is incorrect.

Next steps

Extractors

Learn how to extract data from requests

Responses

Explore different response types