XE Pay with x402 exact

Take x402 v2 exact payments in USDC on Base, Arbitrum, Optimism and Solana into your own vaults, sweep them to your XE account, and keep the payer's money when the work fails.

x402 lets an API answer 402 Payment Required and get paid in the next request. Its standard scheme, exact, moves a fixed amount of USDC to the address in payTo. With XE Pay, that address is your vault on each chain, and the money in it can only go to your XE account. You keep x402's own client and middleware and change two things: the facilitator URL and payTo.

Facilitatorhttps://pay.xe.marketized.io/api/facilitator (/supported, /verify, /settle)
ChainsBase Sepolia eip155:84532, Arbitrum Sepolia eip155:421614, Optimism Sepolia eip155:11155420, Solana devnet solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1
AssetUSDC, 6 decimals: 10000 is $0.01
On XEXUSD in your account (1 USDC = 1 XUSD)

The facilitator runs the x402 Foundation's facilitator code for exact without changes. On EVM chains it submits the payer's EIP-3009 transferWithAuthorization and pays the gas. On Solana it co-signs the payer's transfer as fee payer (4uF7Q9Y9kTFjGGWy7sgscWJFUAXpcAzmFbB5rKbKPRnH); x402's checks keep it out of every instruction.

#Vaults

Every XE account has one vault on each chain. Its address follows from the XE account alone, so it is your payTo before it exists; the first sweep deploys it.

ChainHow the address is madeWhere USDC lands
Base, Arbitrum, Optimism SepoliaA minimal clone at CREATE2(factory, salt = your XE account); XEPayVaults.vaultOf(account) returns itThe vault contract
Solana devnetThe program address ["xe_pay_vault", your XE account] of program 24mU6pTsEfmzmmseyxFDJU1NByUPnBEzA7b8jLPK2So6The vault's USDC token account
ChainXEPayVaults factory
Base Sepolia0x7EcFe178485E1DDfaD2A21AC730b05f93F4C387e
Arbitrum Sepolia0x04F802be68013B8101A62B76A8c8E58901871Cad
Optimism Sepolia0xC45a1D7d2921286a0AF724909b2d9b8D7C79B9f8

Money in a vault goes to two places only:

  • A sweep, which anyone may start: the vault's whole USDC balance goes over XE's XUSD route to your XE account, as XUSD. Nobody can send it anywhere else.
  • Your own order. Your XE account can move the money anywhere, by a message it sends from XE: on EVM through its interchain account on that chain (execute(target, value, data)), on Solana through its XE-controlled wallet (Withdraw).

Vaults have no owner, no admin and no upgrade path. One address per merchant is needed because an exact transfer carries no merchant tag: only the address says whose payment it was.

Read your vaults and their balances:

shell
curl https://pay.xe.marketized.io/api/merchants/<your XE account>/vaults
json
{  "account": "b2636e52…7b23",  "settings": { "mode": "vault", "sweep": { "kind": "daily" } },  "vaults": [    { "network": "eip155:84532", "name": "Base Sepolia", "vault": "0xA4029DA1182e0a491620AFFc3dc9715Fd1998404", "balance": "0", "sweepFee": "0" },    { "network": "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", "name": "Solana Devnet", "vault": "sfuj9mJB46t54MvUiPEPCAWcWpsvKcfABiDUjMLpFTq", "balance": "0", "sweepFee": "0" }  ]}

#Selling an API

A merchant with x402's Hono middleware. Install hono, @x402/hono, @x402/core, @x402/evm and @x402/svm.

typescript
import { Hono } from "hono";import { paymentMiddleware } from "@x402/hono";import { HTTPFacilitatorClient, x402ResourceServer } from "@x402/core/server";import { ExactEvmScheme } from "@x402/evm/exact/server";import { ExactSvmScheme } from "@x402/svm/exact/server"; const XE_PAY = "https://pay.xe.marketized.io/api";const XE_ACCOUNT = process.env.XE_ACCOUNT; // your XE account: 64 hex // Your vault on each chain. Its address depends only on your XE account.const { vaults } = await (await fetch(`${XE_PAY}/merchants/${XE_ACCOUNT}/vaults`)).json(); // x402 has no default USDC for Optimism Sepolia, so name it there (0.01 USDC = 10000 units).const price = (network) =>  network === "eip155:11155420"    ? { amount: "10000", asset: "0x5fd84259d66Cd46123540766Be93DFE6D43130D7", extra: { name: "USDC", version: "2" } }    : "$0.01"; const server = new x402ResourceServer(new HTTPFacilitatorClient({ url: `${XE_PAY}/facilitator` }))  .register("eip155:*", new ExactEvmScheme())  .register("solana:*", new ExactSvmScheme()); const app = new Hono();app.use(  paymentMiddleware(    {      "GET /report": {        accepts: vaults.map((v) => ({ scheme: "exact", network: v.network, payTo: v.vault, price: price(v.network) })),        description: "Today's report",      },    },    server,  ),);app.get("/report", (c) => c.json({ report: "…" })); export default app;

A request without payment gets a 402 whose PAYMENT-REQUIRED header lists one exact entry per chain, each paying your vault:

text
exact  eip155:84532       10000  payTo 0xA4029DA1182e0a491620AFFc3dc9715Fd1998404exact  eip155:421614      10000  payTo 0x69AaCB5dA79Fa69CdF9fC3eA3D7f2245ad661d3Cexact  eip155:11155420    10000  payTo 0xDE82c81825862c5008C2B6E30ac450e00c01254Bexact  solana:EtWTRABZ…   10000  payTo sfuj9mJB46t54MvUiPEPCAWcWpsvKcfABiDUjMLpFTq   feePayer 4uF7Q9Y9…PRnH

With XE's TypeScript kit the vault addresses are worked out offline, with no request: xeAccepts(XE_ACCOUNT, { price: "$0.01" }) from @xe-network/pay returns the same accepts list.

#Paying

Payers use x402's own client, unchanged. An EVM payer needs test USDC and no ETH:

typescript
import { x402Client } from "@x402/core/client";import { ExactEvmScheme } from "@x402/evm/exact/client";import { wrapFetchWithPayment, decodePaymentResponseHeader } from "@x402/fetch";import { privateKeyToAccount } from "viem/accounts"; const account = privateKeyToAccount(process.env.PAYER_KEY as `0x${string}`); // test funds onlyconst client = new x402Client().register("eip155:84532", new ExactEvmScheme(account));const pay = wrapFetchWithPayment(fetch, client); const res = await pay("https://pay.xe.marketized.io/api/feeds/h100/premium"); // $0.01console.log(res.status, decodePaymentResponseHeader(res.headers.get("payment-response")!));

For Solana devnet, register ExactSvmScheme from @x402/svm/exact/client with a @solana/kit signer under solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1.

PAYMENT-RESPONSE names the chain transaction. To check a payment yourself, read that transaction and the vault's USDC balance on the chain.

#Sweeps to XE

A sweep moves a vault's whole balance to your XE account in one transfer, and costs the same for one payment or a thousand.

Who sweepsWhen
XE Pay's keeperAt your threshold, or once a day (the default, "sweep": { "kind": "daily" })
XE Pay, at onceAfter every payment, in the "Instant to XE" mode
You, or anyonePOST https://pay.xe.marketized.io/api/merchants/<account>/sweep with { "network": "eip155:84532" }, at most once a minute per vault
On chainsweep() on the vault (EVM; send the route's fee as value, the rest comes back), or the program's Sweep { merchant } (Solana)
shell
curl -X POST https://pay.xe.marketized.io/api/merchants/<your XE account>/sweep \  -H 'content-type: application/json' -d '{"network":"eip155:84532"}'

The XUSD arrives on XE as a payment waiting to be received; XE wallets receive it. Your mode and sweep policy are set with a message signed by your XE account (GET and POST /api/merchants/<account>/settings).

Gas, measured on forks of the three EVM testnets:

GasPaid by
An exact payment into a vaultabout 93,000 (110,000 the first)The facilitator
The first sweep (deploys the vault)321,000 to 344,000Whoever sweeps
Later sweeps233,000 to 252,000Whoever sweeps

#When the work fails

XE Pay follows x402's order for payments that can wait: verify, do the work, then settle.

  1. The facilitator's /verify checks the payment without moving money, and the invoice is held for that payment alone. Another payment for the same invoice gets 409 in_progress.
  2. The work runs.
  3. Only if it succeeds is the payment settled and the invoice marked paid.

If the work fails, nothing is settled and the payer keeps their money: the signed authorization is never submitted. If the work succeeds but the settle fails (it is tried three times), the answer is 402 with "settling", and the work is kept. Sending the same PAYMENT-SIGNATURE again settles it without doing the work again.

The same 402 also offers XE's own schemes, which move money before the work. Their failed calls are paid back on XE:

SchemePaid withIf the work fails
exactUSDC, by signatureNothing is settled; the payer keeps the USDC
xe-serviceXE or XUSD held in escrow by a service requestThe merchant settles at once for the least usage (1 unit), so the rest of the escrow comes back now rather than at the deadline, and sends that unit back with memo x402-refund:<invoice>
xe-memoAn XE or XUSD send with the invoice's memoThe payment is sent back on XE with memo x402-refund:<invoice>
xe-bridgeUSDC from Solana over the XUSD routeThe XUSD goes back to the Solana wallet that paid, over the route, with the merchant paying the route's fee. The route carries no less than 0.10 XUSD, so smaller payments aren't sent back automatically and the answer says so

An xe-service request that is never answered refunds itself at its deadline. A request that was declined can't pay for a later call.