Skip to main content
The best experience is simple for the agent and controlled for the operator. Your code calls a protected resource normally, client.fetch handles the paid retry, and spend limits decide whether the payment is signed. Automatic payment does not mean unlimited payment. Treat client.fetch as the transport layer and your limits and policy as the guardrail.

What client.fetch handles

client.fetch has the same signature as the global fetch and handles the payment mechanics around a normal HTTP request.
1

Read payment requirements

On an HTTP 402, it reads the seller’s 4Mica payment options from the response.
2

Check spend limits

It picks the first option the session’s spend limits allow. If none passes, it throws SpendLimitError and signs nothing.
3

Sign the guarantee

It signs a payment guarantee with a fresh request ID, using the session’s wallet.
4

Retry the request

It sends the request again with the payment header attached. It retries once; a second 402 comes back to you as is.
5

Return the response

Your agent receives the paid response like a normal HTTP result.
Any response other than a 402 is returned untouched, and so is a 402 that offers no 4Mica payment on a network the session has open. The session’s network is always open; another network opens once you use it through the { network } option of another call, such as client.balance({ network }).

Pay with client.fetch

After the first paid retry succeeds, check the corresponding wallet activity in 4mica dashboard.

Spend limits

Set limits once, in FourMica.connect({ spend }). Amounts are written like deposits, as a decimal string with the asset.
  • A payment is only allowed in an asset that has a perPayment entry. A seller cannot get round the cap by asking for a different token. With the default, the session pays only USDC.
  • total counts every payment the session sends, reserved before signing so concurrent requests cannot overshoot it. A payment that was never sent because signing threw, or that the seller refused with a 402, is released again. One that was sent stays counted even if the request then fails or the seller answers with an error, because the seller may still redeem it.
  • total is local to one session. The count is kept in memory by the process that called connect: a new connect starts from zero, and several processes sharing a wallet each get their own total. Across all of them, the hard limit is the collateral the wallet has deposited.
  • Violations throw SpendLimitError before anything is signed. err.limit names the limit ("perPayment", "total", "assets" or "payTo") and err.url the resource.
  • A limit in an asset the network does not list does not apply there, so one spend setting can cover several networks.
client.paymentFor(response) returns what was paid for a response: protocol, network, asset, amount (a decimal string), amountRaw and payTo. It returns undefined when the request needed no payment, or when the seller failed the request without taking one.

What your policy should handle

Spend limits are coarse. Your policy handles judgment: whether the seller, route, amount, network, asset, and task context are allowed before the agent makes the request.

Using @x402/fetch

If your client is already built on @x402/fetch, register the 4Mica scheme with it instead of switching to client.fetch.
@x402/fetch only pays in assets its scheme client recognises. FourMicaEvmScheme recognises whatever 4Mica core lists for the network, so the seller’s advertised USDC passes the client’s spend controls and the default $1 per-payment cap applies. Pass spendControls to raise the cap or allow other assets.

Signing rules

Before signing, validate the payment against your policy. The amount should fit the budget, the seller address should match the expected service, the route should match the task, and the network and asset should be approved. If the payment crosses an approval threshold, collect approval before signing. If the task budget has no room left, return a controlled error so the agent can pick another service, ask for approval, or stop. When a seller requirement does not clearly match policy, pause signing and ask for help.

Logging rules

Automatic payment should still be explainable. Each paid request should connect task ID, request ID, guarantee ID, seller domain, seller payTo address, route, amount, network, asset, policy version, reason for payment, and final result or error. These logs answer “why did the agent pay?” after the task is done.