Skip to main content

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.
There is no setup step per seller. Each payment carries a random request id and a blinding salt that the SDK generates. The only read before signing is the asset’s active settlement cycle: the flow builds the payment with 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.
The flow signs the amount the requirements ask for, on the network the client is connected to. It does not compare network with the client’s network and it applies no spend limit. Check network, payTo, and the amount yourself before you sign.

What the SDK expects

The requirements types deserialize the canonical x402 JSON (camelCase). At minimum they must include:
  • scheme, exactly 4mica-credit (sdk_4mica::x402::SCHEME_4MICA_CREDIT), and network.
  • payTo (recipient address), asset, and maxAmountRequired (v1) or amount (v2). The zero address as asset means native ETH.
  • Optionally extra.validation ({ validator, subject, deadline?, params? }) to gate the payment on an external validator.
Any other scheme returns X402Error::InvalidScheme.

Version 1

Version 1 returns payment requirements in the JSON response body. Sign them into an X-PAYMENT header.

Version 2

Version 2 delivers requirements in a base64-encoded payment-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 an X402SignedPayment:

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.
Wrap v2 requirements as 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.