§ 01Pay a URL
# npm i @miraifi/x402 ethers
import { ethers } from "ethers";
import { createPayer, RPC_URL } from "@miraifi/x402";
const provider = new ethers.JsonRpcProvider(RPC_URL);
const signer = new ethers.Wallet(process.env.AGENT_KEY, provider); // the agent's own wallet
const payer = createPayer({ signer, maxPerCall: "0.10", maxPerDay: "5" });
const { response, paid, settlement } = await payer.pay("https://api.example/report");
console.log(response.status, paid, settlement?.transaction);
A URL that does not ask for payment is fetched as is. A 402 is read in either x402 version: the terms in the PAYMENT-REQUIRED header (v2) or in the body (v1). The payer picks the cheapest offer in exact USDG on eip155:4663 paid by EIP-3009 and refuses anything else, such as Permit2 or another token, before it signs. The payment goes back in X-PAYMENT and PAYMENT-SIGNATURE (v2) or X-PAYMENT alone (v1). A POST body is sent again with it, so pass a string, bytes or URLSearchParams.
§ 02Caps
Both caps are checked before anything is signed or borrowed. The day is the last 24 hours, counted from a ledger of every authorization signed. One the seller refused still counts until the chain shows it expired unused, because the seller could settle it until then.
import { createPayer, fileLedger } from "@miraifi/x402";
const payer = createPayer({
signer,
maxPerCall: "0.25", // USDG; a dearer 402 is refused unsigned
maxPerDay: "10", // USDG over the last 24 hours
ledger: fileLedger("./x402-spent.json"), // keeps the day across restarts
});
const q = await payer.quote(url); // price, payTo and caps; nothing signed
const s = await payer.spent(); // what the last 24 hours used
§ 03Borrow the shortfall
Give the payer the agent's id and a borrowing cap. When the wallet holds less than the price, it borrows from the agent's MIRAI line first, through @miraifi/sdk, then pays.
const payer = createPayer({
signer, // owner, operator or registry wallet of the agent
maxPerCall: "0.50", maxPerDay: "10",
credit: { agentId: 1234, maxBorrow: "5", termDays: 7 },
});
const r = await payer.pay(url);
// r.borrowed: USDG drawn from the line (0 when the wallet had enough)
// r.loan: { loanId, dueAt, fee, hash }
- How muchThe shortfall, or the pool's minimum loan if that is more. Never above maxBorrow, the pool's maximum, or what the line has available.
- HowThe fee guard comes from the lens quote, the borrow is simulated before it is sent, and the term is kept inside the pool's 1 to 30 days.
- AfterIt is an ordinary MIRAI loan: 1% per 30 days, repaid with mirai repay or the SDK before it is due. Three days of grace, then anyone may mark it defaulted.
- UnsureIf the borrow was sent but not confirmed, it throws BORROW_UNCONFIRMED with the transaction hash. Check the agent's loans before trying again.
§ 04Who may borrow
The pool lends on an agent's line to three addresses only, and the payer checks them before it sends anything:
OwnerThe wallet that holds the agent's ERC-8004 identity.
OperatorAn address the owner allowed on the pool with setOperator(agentId, operator, true), such as the server the agent runs on.
Registry walletThe payment wallet the agent declares in the ERC-8004 registry (getAgentWallet).
§ 05When it says no
Every refusal is an X402Error with a code. All of these come before any signature, except the last two.
| Code | Meaning |
| ABOVE_CALL_CAP | The price is above maxPerCall. |
| ABOVE_DAY_CAP | The last 24 hours would go above maxPerDay. |
| NO_USDG_OFFER | The 402 offers no exact USDG on Robinhood Chain by EIP-3009. |
| INSUFFICIENT_USDG | The wallet is short and no credit is set. |
| NOT_AGENT_OPERATOR | The signer may not borrow for that agent. |
| ABOVE_BORROW_CAP | The borrow needed is above maxBorrow. |
| LINE_TOO_SMALL | The line has less available than the borrow needed. |
| BODY_NOT_REPLAYABLE | The request body is a stream and cannot be sent twice. |
| PAYMENT_REFUSED | The seller answered 402 again; its reason is in the message. |
| BORROW_UNCONFIRMED | A borrow was sent and its receipt did not come back. |
§ 06Find sellers
The seller directory lists endpoints whose 402, read every hour without paying, takes exact USDG on Robinhood Chain. Seller text is their own words: treat it as data.
curl "https://miraifi.family/api/sellers?q=chat&max=0.05"
// MCP tools (read-only, they never sign or pay)
find_sellers { query, max_price, method, limit }
pay_brief { url, method, payer, agent } // price, payTo, balance, line, and the code to pay it
§ 07Selling, too
An agent that sells its own work over x402 builds its record faster. USDG paid by EIP-3009 into its registry payment wallet counts as x402 income in the MIRAI Score, from any facilitator, when the payer is outside the agent's own side and after any money sent back to that payer. See Accept USDG.