Available on crate feature
server only.Expand description
Axum middleware for enforcing x402 payments on protected routes.
This middleware validates incoming payment headers using a configured x402 facilitator, and settles valid payments either before or after request execution (configurable).
Returns a 402 Payment Required response if the request lacks a valid payment.
§Settlement Timing
By default, settlement occurs after the request is processed. You can change this behavior:
X402Middleware::settle_before_execution- Settle payment before request execution.X402Middleware::settle_after_execution- Settle payment after request execution (default). This allows processing the request before committing the payment on-chain.
§Configuration Notes
X402Middleware::with_price_tagsets the assets and amounts accepted for payment (static pricing).X402Middleware::with_dynamic_pricesets a callback for dynamic pricing based on request context.X402Middleware::with_base_urlsets the base URL for computing full resource URLs. If not set, defaults tohttp://localhost/(avoid in production).X402LayerBuilder::with_descriptionis optional but helps the payer understand what is being paid for.X402LayerBuilder::with_mime_typesets the MIME type of the protected resource (default:application/json).X402LayerBuilder::with_resourceexplicitly sets the full URI of the protected resource.
Structs§
- X402
Layer Builder - Builder for configuring the X402 middleware layer.
- X402
Middleware - The main X402 middleware instance for enforcing x402 payments on routes.
- X402
Middleware Service - Axum service that enforces x402 payments on incoming requests.