Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
r402
x402 Payment Protocol SDK for Rust. Protocol version 2 only.
[]
= { = "0.20", = ["evm", "http"] }
# r402 = { version = "0.20", features = ["evm", "svm", "http"] }
# r402 = { version = "0.20", features = ["full"] }
default = ["evm", "http"]. Chain crates are feature-gated re-exports (svm, near, xrpl, hedera, avm, aptos, keeta, tvm, stellar, concordium). full is every production chain plus HTTP and MCP. tron / casper are opt-in, not in full.
Protect a Route (Server)
use address;
use ;
use ;
use X402Middleware;
let x402 = try_new?
.with_base_url
.with_scheme;
let app = new.route;
Send Payments (Client)
use PrivateKeySigner;
use Eip155ExactClient;
use ;
use Arc;
let signer = new;
let client = new.with_payments;
let res = client.get.send.await?;
Settlement Modes
SettlementMode is not a paymentFlow. Spec paymentFlow (authorization / upfront / escrow) is the on-wire ordering of verify and settle around the handler. Sequential / Concurrent / Background are a resource-server scheduler for the after-handler settle of authorization: whether this HTTP response waits for that settle. Facilitators and clients never see the knob; it is not written into PAYMENT-REQUIRED.
- Sequential (default) is spec
authorization: verify → handler → settle → respond withPayment-Response. - Concurrent and Background overlap that after-handler settle with the handler (async I/O). They are not a fourth flow.
upfront/escrowreject them (IncompatibleSettlementMode) because those flows already settle before the handler.
Configurable via with_settlement_mode() after with_price_tag.
Sequential (default)
Verify → execute → settle. Spec authorization. On-chain settlement only after the handler succeeds. Payment-Response is attached.
sequenceDiagram
participant C as Client
participant S as Server
participant F as Facilitator
participant H as Handler
C->>S: HTTP Request + Payment-Signature
S->>F: verify(payment)
F-->>S: VerifyResponse ✓
S->>H: execute request
Note over S,H: Balance verified but NOT locked —<br/>handler executing (variable latency)
H-->>S: response body
S->>F: settle(payment)
Note over S,F: On-chain transfer (2–5 s)
F-->>S: SettleResponse (tx_hash)
S-->>C: 200 OK + Payment-Response header
Concurrent
Verify → (settle ∥ execute) → await both. Still authorization on the wire. Overlaps the after-handler settle RPC with the handler. The response still waits and still carries Payment-Response. On handler error the settlement task is detached — the payer may be charged even if the handler failed.
sequenceDiagram
participant C as Client
participant S as Server
participant F as Facilitator
participant H as Handler
C->>S: HTTP Request + Payment-Signature
S->>F: verify(payment)
F-->>S: VerifyResponse ✓
par settle ∥ execute
S->>F: settle(payment)
Note over S,F: On-chain transfer
F-->>S: SettleResponse (tx_hash)
and
S->>H: execute request
H-->>S: response body
end
S-->>C: 200 OK + Payment-Response header
Background
Verify → spawn settle → execute → return. Still authorization on the wire. No Payment-Response (headers leave before settle finishes). For SSE / LLM streams.
sequenceDiagram
participant C as Client
participant S as Server
participant F as Facilitator
participant H as Handler
C->>S: HTTP Request + Payment-Signature
S->>F: verify(payment)
F-->>S: VerifyResponse ✓
S-)F: settle(payment) [fire-and-forget]
S->>H: execute request
H-->>S: response body (or stream)
S-->>C: 200 OK (no Payment-Response header)
Note over S,F: Settlement completes asynchronously
F-)S: SettleResponse (logged)
Comparison
Applies only to paymentFlow = authorization. upfront / escrow stay Sequential.
| Mode | Total latency | Safety | Payment-Response |
Best for |
|---|---|---|---|---|
| Sequential | verify + handler + settle | Settlement only on handler success | ✅ Included | Spec authorization; buffered responses |
| Concurrent | verify + max(handler, settle) | Settlement may occur on handler failure | ✅ Included | Overlap settle with a slow handler |
| Background | verify + handler | Settlement errors are non-fatal (logged) | ❌ Not attached | SSE / LLM streaming (headers must leave before the body) |
Note:
UptoActualAmountis honoured only bySettlementMode::Sequential. Concurrent and Background start settlement before the handler returns and therefore charge the signed maximum. They cannot meter a stream.
License
Licensed under either of MIT or Apache-2.0, at your option.