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, verifies the payment, executes the request, and settles valid payments after successful execution. If the handler returns an error (4xx/5xx), settlement is skipped.
Returns a 402 Payment Required response if the request lacks a valid payment.
§Settlement Modes
SettlementMode::Sequential(default): verify → execute → settle. Safer — settlement only runs after the handler succeeds.SettlementMode::Concurrent: verify → (settle ∥ execute) → await settle. Lower latency — overlaps settlement with handler execution.
§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_settlement_modeselects sequential or concurrent settlement.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.
Enums§
- Settlement
Mode - Controls when on-chain settlement executes relative to the inner service.