Top up with x402
Moth DocsTutorials
Sign one USDG transfer and Moth sends it for you, so a top-up needs no ETH for gas.
x402 is a way to pay over HTTP. Your wallet signs a USDG transfer to your own deposit address, Moth sends it on Robinhood Chain and pays the gas, and the balance shows it a few seconds after the transfer lands. Each top-up is $1 to $1,000.
In Moth Console
Open Add credit
Sign in with your wallet and open Credit in Moth Console.
Pick an amount and x402 signature
Choose the amount, then x402 signature under Pay with.
Sign once
Your wallet asks you to sign a USDG transfer to your deposit address. Signing sends nothing itself and costs no gas.
Wait a few seconds
Moth sends the transfer. It shows under Deposits with its transaction, and the balance rises as for any deposit.
- The wallet needs USDG on Robinhood Chain and no ETH.
- A signature is valid for about nine minutes. One that is not sent by then expires and moves nothing.
- x402 shows only while it is open. If it is paused, pay from the connected wallet or the deposit address instead.
From a script or an agent
Ask for a top-up with your Moth key. The first call answers 402 with one offer in the PAYMENT-REQUIRED header, the x402 v2 format. Sign it as an EIP-3009 TransferWithAuthorization on USDG's domain and send the same call again with the PAYMENT-SIGNATURE header. Moth answers 200 with the transaction in PAYMENT-RESPONSE, or 202 with the top-up's id while it is on its way.
import { privateKeyToAccount } from "viem/accounts";
const url = "https://api.runmoth.ai/v1/credits/x402?amount_usd=25";
const headers = { Authorization: `Bearer ${process.env.MOTH_API_KEY}` };
const wallet = privateKeyToAccount(process.env.PAYER_KEY);
// 1. The offer: USDG on Robinhood Chain, to your account's deposit address.
const offer = await fetch(url, { method: "POST", headers });
const accepted = JSON.parse(atob(offer.headers.get("PAYMENT-REQUIRED"))).accepts[0];
// 2. Sign the transfer, valid for a few minutes.
const now = BigInt(Math.floor(Date.now() / 1000));
const authorization = {
from: wallet.address, to: accepted.payTo, value: BigInt(accepted.amount),
validAfter: now - 30n, validBefore: now + 300n,
nonce: `0x${crypto.randomUUID().replaceAll("-", "").padEnd(64, "0")}`,
};
const signature = await wallet.signTypedData({
domain: { ...accepted.extra, chainId: 4663, verifyingContract: accepted.asset },
types: { TransferWithAuthorization: [
{ name: "from", type: "address" }, { name: "to", type: "address" }, { name: "value", type: "uint256" },
{ name: "validAfter", type: "uint256" }, { name: "validBefore", type: "uint256" }, { name: "nonce", type: "bytes32" },
] },
primaryType: "TransferWithAuthorization", message: authorization,
});
// 3. Pay: Moth sends it and pays the gas.
const payment = btoa(JSON.stringify({ x402Version: 2, accepted, payload: { signature, authorization: {
...authorization, value: String(authorization.value), validAfter: String(authorization.validAfter), validBefore: String(authorization.validBefore),
} } }));
const paid = await fetch(url, { method: "POST", headers: { ...headers, "PAYMENT-SIGNATURE": payment } });
console.log(paid.status, await paid.json());The x402 reference lists every field, status and error, and the two reads for a top-up and for whether x402 is open.
Let an agent pay for its own inference turns this into a full loop, with the checks and limits an agent should carry.
Stock x402 clients. Clients built on the x402 SDK accept USDG on eip155:4663 only once it is in their allowed assets, and many cap a payment at $1 unless that is raised.