X402 flow
X402Flow handles the client side of the x402 payment protocol for 4Mica. It turns the payment requirements from a 402 Payment Required response into a signed payment header, and can call a facilitator’s /settle endpoint.
Create a flow from your client. Start on Base Sepolia with a test wallet.
client.payment.new_payment, scoped to that cycle and stamped by core’s clock, and signs it with the client as the payer.
The flow makes no HTTP calls to the seller. You send the requests, and the examples below use reqwest (with its json feature), serde, serde_json, and base64.
What the SDK expects
The requirements types deserialize the canonical x402 JSON (camelCase). At minimum they must include:scheme, exactly4mica-credit(sdk_4mica::x402::SCHEME_4MICA_CREDIT), andnetwork.payTo(recipient address),asset, andmaxAmountRequired(v1) oramount(v2). The zero address asassetmeans native ETH.- Optionally
extra.validation({ validator, subject, deadline?, params? }) to gate the payment on an external validator.
X402Error::InvalidScheme.
Version 1
Version 1 returns payment requirements in the JSON response body. Sign them into anX-PAYMENT header.
Version 2
Version 2 delivers requirements in a base64-encodedpayment-required header and signs them into a PAYMENT-SIGNATURE header. If accepted.extra.validation is present, the SDK attaches the validation requirement to the signed payment.
sign_payment_v2 returns X402Error::InvalidVersion unless the challenge’s x402Version is 2. It echoes the challenge’s resource and extensions back in the signed envelope.
The signed payment
Both methods return anX402SignedPayment:
What a resource server does
The Rust SDK has no HTTP server middleware. To charge for a route, use@4mica/x402 (TypeScript) or 4mica-x402 (Python); see payment middleware.
A resource server answers an unpaid request with 402 and its payment requirements. When the client retries with a payment header, the server sends the payment to a facilitator’s /verify endpoint, then to /settle, and serves the response only when settlement succeeds.
If you write that server in Rust, settle_payment covers the /settle call. It POSTs { x402Version, paymentPayload, paymentRequirements } to {facilitator_url}/settle, where paymentPayload is the envelope object, not the base64 header.
X402Requirements::V2(accepted). The requirements must be the same x402 version as the payment, or the call returns X402Error::InvalidVersion. End a facilitator URL that has a path with a trailing slash, because settle is joined onto it as a relative URL.
settled.settlement is the facilitator’s JSON body. When the facilitator relays core’s receipt, it is under extra.receipt. Check it with verify_guarantee before you serve the response.
settle_payment only calls /settle. Verify a payment before you grant
access. A rejection comes back as HTTP 200 with success: false, so read the
body and do not rely on the absence of an error. A non-2xx status returns
X402Error::SettlementFailed with the status and body.Next steps
Payment middleware
Charge for a route with
@4mica/x402 or 4mica-x402.Client operations
Deposit collateral, sign payments, and settle cleared cycles.