Connect

Metered spend

x402 answers how a machine pays. It is silent on whether this machine should be paying this, to this address, at this price, again. That is the question a finance team has asked for a century, and the one an agent with a wallet cannot answer for itself.

The middleware wraps your agent’s fetch. Every 402 it sees is matched against a mandate a person signed before the wallet is ever asked to authorise anything.

A 402 is an invoice

It names a payee, a price and a thing being sold, and it asks to be paid. The only reason it was never treated as one is that there was nothing to compare it against.

  • 01payTo becomes the payeeAn endpoint that asks to be paid somewhere else is a PAYEE_MISMATCH.
  • 02amount becomes the unit priceA price raised after the mandate was signed is PRICE_OVER_TOLERANCE.
  • 03resource decides which mandate appliesNo open order for that URL and the answer is NO_PO, before any payment is signed.
  • 04one call is one unitPast the call count a person pre-authorised, it is QTY_OVER_RECEIVED.

The mandate

Not a config file. A purchase order on Whitechain, raised by a verified person, whose spec is the endpoint URL. The middleware finds it by hashing the URL it was asked to pay.

No open mandate covers /api/x402/resource. Every scenario below will refuse with NO_PO, which is the correct answer and also the least interesting one. Raise an order whose description is the full resource URL to authorise it.

How a mandate is written

Raise an order in Orders with the endpoint URL as its description, the number of calls as the quantity, and the price per call as the unit price. A second person signs for that call count before the agent may spend.

Description to use
http://localhost:3000/api/x402/resource

Skipping the middleware does not work

A wrapper around fetch is a control the agent can simply not call. So the payer account’s key is a threshold structure that Hedera enforces itself: the agent and a quorum of independent guardians must both sign.

  • 01The agent alone cannot payThe facilitator checks the signatures against the account’s key on the ledger and refuses: payer did not sign.
  • 02A guardian reads before it signsIt decodes the transaction, checks it pays exactly the quote to the mandate’s account, then runs the mandate check itself. A lying middleware gets nothing signed.
  • 03No single party is enoughOne hacked guardian, or a hacked relayer, cannot move money. It takes the agent and a quorum of guardians being wrong together.

Try to get a payment through

Each of these is something an unguarded agent pays without hesitating. The line to watch is whether the wallet was asked to sign.

  • 01A call the mandate coversThe agreed endpoint, the agreed payee, the agreed price. Expect: Pays
  • 02An endpoint nobody authorisedThe agent is talked into calling a different paid API. Expect: NO_PO
  • 03The payee swapped at quote timeThe endpoint asks to be paid somewhere other than the agreed address. Expect: PAYEE_MISMATCH
  • 04The price raised after the factThe same endpoint quotes ten times what the mandate agreed. Expect: PRICE_OVER_TOLERANCE
  • 05The same quote replayedA quote that was already decided is presented again. Expect: DUPLICATE_INVOICE
  • 06The agent skips the middlewareA compromised agent ignores the guard and signs the payment itself. Hedera, not the middleware, refuses. Expect: Refused by the facilitator

Wiring it in

One wrap. The middleware never holds a key: signing stays with your x402 client and is only ever reached by a quote that passed.

import { createGuard, withZowall } from "@zowall/x402";
import { buildAgentSignedTransfer, createGuardian, guardianQuorum, toPayload }
  from "@zowall/x402/hedera";

const guarded = withZowall({
  guard: createGuard({ keccak256, findMandate, roles }),
  // Only ever called for a quote that passed. The agent signs, then a quorum of
  // guardians each decode the transaction and sign if it matches the mandate.
  signPayment: async (quote) => {
    const tx = await buildAgentSignedTransfer({ client, requirements: quote, payer, agent });
    const { bytes } = await guardianQuorum(tx, quote, guardians, 2);
    return { payload: toPayload(bytes) };
  },
});

// Anything that is not a 402 passes straight through.
const res = await guarded("https://api.example.com/premium");
The endpoint these scenarios call is a resource server we run, so that it can be shown misbehaving. It is a real x402 v2 server: it quotes in a PAYMENT-REQUIRED header and has the official Hedera facilitator verify and settle each payment on testnet, so the honest scenario moves real testnet HBAR and returns a transaction you can open on HashScan.