X402 Paywall
A framework-agnostic HTTP paywall implementation for the X402 payment protocol.
This crate provides PayWall, a composable middleware that protects
HTTP resources with X402 payments. It handles the complete payment lifecycle including
verification and settlement through a configured facilitator.
Quick Start
use address;
use url;
use ;
use PayWall;
let facilitator = from_url;
let paywall = builder
.facilitator
.accepts
.resource
.build;
Modules
- [
paywall]: The mainPayWallstruct and payment flow logic. - [
processor]: Payment processing types includingRequestProcessorandPaymentState. - [
errors]: Error types for payment failures and HTTP error responses.
Payment Flow
The standard payment flow using PayWall::handle_payment:
- Update Accepts: Filter payment requirements based on facilitator support.
- Process Request: Extract and validate the
PAYMENT-SIGNATUREheader. - Verify: Verify the payment signature with the facilitator.
- Run Handler: Execute the resource handler.
- Settle: Settle the payment on successful response.
For custom flows, use the step-by-step API directly. See PayWall for details.
Framework Integration
While framework-agnostic, x402-paywall works seamlessly with any HTTP framework.
Here's an example with Axum:
use ;
async
let app = new
.route;
Error Handling
ErrorResponse implements IntoResponse for Axum and can be
easily adapted to other frameworks. It returns appropriate HTTP status codes:
402 Payment Required: No payment signature provided.400 Bad Request: Invalid payment payload or unsupported requirements.500 Internal Server Error: Facilitator communication failures.