Get started
Pay per request with x402.
No account and no key. Ask for a quote at the same path under /x402, sign one payment, and send the same request again. Every model endpoint works this way: chat (also in the Messages and Responses formats), completions, embeddings, images, audio, rerank and video.
On this page
A request with no credentials is answered with a quote. You sign one payment and send the same request again. The gateway settles the payment onchain, then serves the request.
Pay per request is server to server: call the / endpoints from your server or agent, not from a web page's script. A browser's cross-origin preflight to them is refused.
You need a wallet on Robinhood Chain that holds USDF or USDG. Need USDF? Buy it on the Buy page, or swap any token for it. A wallet that holds USDG needs no setup. Paying in USDF also takes a little ETH, once, for the approval below.
| Step | You send | The gateway answers |
|---|---|---|
| 1. Ask | The request, unpaid | 402 with the quote and two accepted payments |
| 2. Sign | Nothing: you sign one payment locally | Nothing. Signing costs no gas |
| 3. Send again | The same request, plus PAYMENT-SIGNATURE | Settles the payment onchain, then serves |
| 4. Read | Nothing | PAYMENT-RESPONSE and a receipt with the answer |
Two assets are accepted for the same amount. USDF is listed first.
| Asset | Method | One-time setup | What you sign |
|---|---|---|---|
| USDF | Signature transfer with a witness | Approve USDF to the signature-transfer contract once, in one onchain transaction | EIP-712 PermitWitnessTransferFrom |
| USDG | Transfer with authorization | None | EIP-712 TransferWithAuthorization |
- Each payment costs the payer no gas. The gateway submits the transfer.
- Paying in USDF needs a one-time approval to the signature-transfer contract. That approval is the only transaction a payer sends, and it needs a little ETH for gas. Paying in USDG needs neither.
- An unpaid quote costs nothing.
- A request that fails validation or cannot be routed is refused before any payment.
Samples use gemma-3-4b-it with max_ at 100,000. max_ sets the quote: the most the request can cost, with the answer counted at max_. A quote below the minimum charge in The quote is raised to that minimum and charged in full. At 100,000 the quote clears it, so you are charged what the request uses and the rest comes back to you.
Send the request exactly as the keyed endpoint takes it, to the same path under /, with no credentials.
Ask for a quote
curl -i https://api.usdf.fi/x402/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "gemma-3-4b-it", "messages": [{"role": "user", "content": "Hello"}], "max_tokens": 100000 }'
Response, trimmed
402 Payment Required payment-required: <base64 JSON of the "x402" object below> www-authenticate: Payment id="…", realm="…", method="evm", intent="charge", request="…", expires="…" { "type": "https://paymentauth.org/problems/payment-required", "title": "Payment Required", "status": 402, "detail": "payment required", "error": { "message": "payment required", "type": "payment_required", "code": "x402" }, "quote": { "model": "gemma-3-4b-it", "max_tokens": 100000, "price_version": "<price sheet>", "amount": { "units": "<units>", "usd": "<usd>", "usdf": "<usdf>" }, "asset": "0x4A35a5eC2A50A4d2957B2F2366E017f526cAB318", "accepted": [ { "asset": "0x4A35a5eC2A50A4d2957B2F2366E017f526cAB318", "symbol": "USDF", "method": "permit2", "note": "…" }, { "asset": "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168", "symbol": "USDG", "method": "eip3009", "note": "…" } ] }, "x402": { "x402Version": 2, "resource": { "url": "…", "description": "…", "mimeType": "application/json" }, "accepts": [ { "scheme": "exact", "network": "eip155:4663", "amount": "<units>", "asset": "0x4A35a5eC2A50A4d2957B2F2366E017f526cAB318", "payTo": "0x94544E57E6244202F0fD8071B1D9b1231cd4F6a3", "maxTimeoutSeconds": 300, "extra": { "assetTransferMethod": "permit2" } }, { "scheme": "exact", "network": "eip155:4663", "amount": "<units>", "asset": "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168", "payTo": "0x94544E57E6244202F0fD8071B1D9b1231cd4F6a3", "maxTimeoutSeconds": 300, "extra": { "name": "Global Dollar", "version": "1" } } ] } }
The fields that matter when you pay:
| Field | Meaning |
|---|---|
quote. | The most the request can cost, in units, USD and USDF. This is what you pay up front. |
quote. | The price sheet the quote was made from. |
quote. | The two payments, by asset and method. |
quote. | On non-chat endpoints: how many units the quote covers, and in what. |
accepts[]. | The gateway's wallet the payment goes to. |
accepts[]. | The same amount, in base units. |
accepts[]. | How long a signed payment may stay valid. |
accepts[]. | assetTransferMethod: "permit2" for USDF; the EIP-712 name and version for USDG. |
The quote is the most the request can cost: the prompt at one token per UTF-8 byte, the output at max_. The request is charged what it used at that price, and the rest comes back to you. Every quote has a small minimum that follows the network's gas: the gas of returning a payment at the chain's gas price now, with a margin, rounded up. The quote states it as minimum_. A request whose bound is below it is quoted at that minimum and answered 402 as any other; it is charged the minimum, and a remainder smaller than the gas of returning it is kept rather than refunded. On the other endpoints a larger request raises the quote: more input (embeddings), more documents (rerank), a longer input (speech), longer audio (transcriptions), more or larger images, or a dearer model. A request with an API key has no such floor. When the chain's gas, or a payment's state, cannot be read just then, the answer is 503 refund_gas_unread (no payment is asked for) or 503 payment_state_unread (the payment is not settled): retry after its retry-after delay; nothing was charged. The gateway pays the gas.
Once per wallet, approve USDF to the signature-transfer contract with an ordinary approve. That is the only transaction a payer ever sends, and it needs a little ETH on Robinhood Chain for gas. Read the allowance first.
Each payment is an EIP-712 signature over a signature transfer with a witness:
| Part | Value |
|---|---|
| Domain | { name: "Permit2", chainId: 4663, verifyingContract: 0x000000000022D473030F116dDEE9F6B43aC78BA3 } |
| Primary type | PermitWitnessTransferFrom |
| Types | PermitWitnessTransferFrom(TokenPermissions permitted, address spender, uint256 nonce, uint256 deadline, Witness witness), TokenPermissions(address token, uint256 amount), Witness(address to, uint256 validAfter) |
permitted | { token: USDF, amount } |
spender | The x402 exact proxy |
nonce | A random uint256 |
deadline | Now plus maxTimeoutSeconds |
witness | { to: payTo, validAfter: 0 } |
Both addresses are fixed. The signature-transfer contract's is also in each MPP challenge, as methodDetails.. The proxy is the x402 exact scheme's fixed spender. @usdf/ exports both, as PERMIT2_ and X402_.
Signature transfer 0x000000000022D473030F116dDEE9F6B43aC78BA3 x402 exact proxy 0x402085c248EeA27D92E8b30b2C58ed07f9E20001
Send the signed payment as base64 of this JSON in PAYMENT-SIGNATURE:
Payment payload, USDF
{
"x402Version": 2,
"resource": <the resource from the 402>,
"accepted": <the USDF accept, verbatim>,
"payload": {
"signature": "<signature>",
"permit2Authorization": {
"from": "<payer>",
"permitted": { "token": "0x4A35a5eC2A50A4d2957B2F2366E017f526cAB318", "amount": "<units>" },
"spender": "0x402085c248EeA27D92E8b30b2C58ed07f9E20001",
"nonce": "<random uint256, decimal>",
"deadline": "<unix seconds>",
"witness": { "to": "0x94544E57E6244202F0fD8071B1D9b1231cd4F6a3", "validAfter": "0" }
}
}
}The SDK does the approval and the signing. Its wallet is a viem wallet client on Robinhood Chain, so it can send the approval itself: with approve: true it sends the approval once if the wallet has not given it, then pays in USDF. Without approve: true it pays in USDF only once the wallet has approved, and in USDG otherwise. A wallet that holds only USDG needs no approval: pass asset: "USDG".
With the SDK
npm install @usdf/sdk viem
import { USDF } from "@usdf/sdk";
import { createWalletClient, defineChain, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const robinhood = defineChain({
id: 4663,
name: "Robinhood Chain",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
rpcUrls: { default: { http: ["https://rpc.mainnet.chain.robinhood.com"] } },
});
const wallet = createWalletClient({
account: privateKeyToAccount(process.env.PAYER_KEY as `0x${string}`),
chain: robinhood,
transport: http(),
});
// Pays in USDF. approve: true sends the one-time approval first if the
// wallet has not given it. A wallet that holds USDG instead needs no setup:
// new USDF({ wallet, asset: "USDG" }).
const usdf = new USDF({ wallet, approve: true });
const result = await usdf.run("gemma-3-4b-it", "Hello", { max_tokens: 100000 });
console.log(result.text);The same payment signed by hand, with the x402 client library and viem:
Sign without the SDK
npm install @x402/core@2.29.0 @x402/evm@2.29.0 viem@2.56.8
import { x402Client } from "@x402/core/client";
import {
decodePaymentRequiredHeader,
decodePaymentResponseHeader,
encodePaymentSignatureHeader,
} from "@x402/core/http";
import { createPermit2ApprovalTx, getPermit2AllowanceReadParams } from "@x402/evm";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { createPublicClient, createWalletClient, defineChain, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const url = "https://api.usdf.fi/x402/v1/chat/completions";
const body = JSON.stringify({
model: "gemma-3-4b-it",
messages: [{ role: "user", content: "Hello" }],
max_tokens: 100000,
});
const headers = { "content-type": "application/json" };
const payer = privateKeyToAccount(process.env.PAYER_KEY as `0x${string}`);
const rhc = defineChain({
id: 4663,
name: "Robinhood Chain",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
rpcUrls: { default: { http: ["https://rpc.mainnet.chain.robinhood.com"] } },
});
// Ask without paying: 402, with the accepted payments in the
// PAYMENT-REQUIRED header. The first is USDF by signature transfer,
// the second USDG through EIP-3009.
const unpaid = await fetch(url, { method: "POST", headers, body });
const required = decodePaymentRequiredHeader(
unpaid.headers.get("payment-required") ?? "",
);
const usdf = required.accepts[0];
const token = usdf.asset as `0x${string}`;
// Once per wallet: approve USDF for signature transfers. This is the only
// transaction the payer ever sends.
const rpc = createPublicClient({ chain: rhc, transport: http() });
const allowance = await rpc.readContract(getPermit2AllowanceReadParams({
tokenAddress: token,
ownerAddress: payer.address,
}));
if (allowance < BigInt(usdf.amount)) {
const approve = createPermit2ApprovalTx(token);
await createWalletClient({ account: payer, chain: rhc, transport: http() })
.sendTransaction({ to: approve.to, data: approve.data });
}
// Allow USDF on this network, then sign. Allowing the USDG accept
// instead (required.accepts[1]) pays by EIP-3009, with no approval.
const client = new x402Client().setSpendControls({
allowedAssets: [{ network: usdf.network, asset: usdf.asset }],
});
registerExactEvmScheme(client, { signer: payer });
const payload = await client.createPaymentPayload(required);
// The same request with the signed permit. The gateway settles it
// onchain, then serves it.
const paid = await fetch(url, {
method: "POST",
headers: {
...headers,
"payment-signature": encodePaymentSignatureHeader(payload),
},
body,
});
const result = await paid.json();
console.log(result.choices[0].message.content);
console.log(
result.receipt.paid.usdf,
result.receipt.payment.asset,
"paid in",
result.receipt.payment.tx,
);
console.log(decodePaymentResponseHeader(
paid.headers.get("payment-response") ?? "",
));USDG needs no approval. Each payment is a transfer authorization signed as EIP-712 typed data.
| Part | Value |
|---|---|
| Domain | { name: "Global Dollar", version: "1", chainId: 4663, verifyingContract: USDG } |
| Primary type | TransferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce) |
| Message | { from: payer, to: payTo, value: amount, validAfter: 0, validBefore: now + maxTimeoutSeconds, nonce: random bytes32 } |
| Payload | { signature, authorization: { from, to, value, validAfter, validBefore, nonce } }, with the USDG accept as accepted |
The payment must be valid for at least 30 seconds when it arrives.
Send the identical body again with PAYMENT-SIGNATURE: <encoded payment>. Send a multipart body whole, both times: its quote is read from the file.
Paid request
curl https://api.usdf.fi/x402/v1/chat/completions \ -H "Content-Type: application/json" \ -H "PAYMENT-SIGNATURE: <encoded payment>" \ -d '{ "model": "gemma-3-4b-it", "messages": [{"role": "user", "content": "Hello"}], "max_tokens": 100000 }'
Response, trimmed
payment-response: <base64 JSON: success, transaction, network, payer> x-request-id: <request_id> { "id": "<request_id>", "choices": [ … ], "receipt": { "request_id": "<request_id>", "paid": { "units": "<units>", "usd": "<usd>", "usdf": "<usdf>" }, "usage_cost": { "units": "<units>", "usd": "<usd>", "usdf": "<usdf>" }, "refund": { … }, "payment": { "tx": "<transaction hash>", "explorer": "…", "payer": "<payer>", "amount": { "units": "<units>", "usd": "<usd>", "usdf": "<usdf>" }, "asset": "USDF", "method": "permit2" }, "settlement": { "scheme": "x402 exact", "network": "eip155:4663" }, "data_policy": { … }, "log": { "proof": "https://api.usdf.fi/v1/log/proof/<request_id>", "note": "…" } } }
<encoded payment> is the base64 of the payment payload above.
The receipt reports both what you paid and what the request used:
| Field | Meaning |
|---|---|
request_, model, provider | The request's id, the model without any routing suffix, and the route that served it. |
status | ok, or partial for an answer cut short. |
usage, price | The provider's counts, and the price sheet and rates they were metered at. |
paid | What the request was charged: what it used at the quoted price, never more than the payment. |
usage_ | The metered cost of what the request used. |
refund | The rest of the payment, sent back in the asset paid, usually within a minute. Null when the request used it all. |
payment | tx, explorer, payer, amount (the quote, paid up front), asset and method of the transfer. |
settlement | scheme is x402 exact, or mpp evm charge for MPP, with the network. |
data_ | What the route that served the request does with its content, and what the request asked for. |
agent_ | When the request named an agent with x-erc8004-agent: the identity, as checked. |
log | The request's leaf in the public usage log: its proof URL. |
modality, endpoint, billed | On non-chat endpoints: the modality, the endpoint and the { units, unit } billed. |
lookup | On non-chat endpoints: the public receipt's URL. Every receipt, chat included, is public at GET /. |
The PAYMENT-RESPONSE header is base64 JSON with the transaction. Whatever the request did not use is refunded to the payer in the asset paid.
The payment settles before the model runs. A served request is charged what it used and the rest is refunded. What happens when something fails:
| Situation | What happens |
|---|---|
| The request fails validation or cannot be routed | Refused before payment. Nothing is taken. |
| The request's bound is below the minimum charge | Quoted and charged the minimum, which follows the network's gas (minimum_ in the quote, as The quote says). A remainder smaller than the gas of returning it is kept. |
| The gateway or the provider fails after payment, before any output | Refunded in full, in the asset paid. |
| The provider ends a stream before any output, while you are connected | Refunded in full. |
The provider rejects the request itself (upstream_) | Refunded in full. |
An MCP chat call is cut at its deadline after the provider accepted it, with nothing produced | Refunded in full. |
| You leave a stream | Charged what the provider reports for it. The rest is refunded. |
| The answer is cut short after output started | Charged for the text produced, which is returned, and the rest refunded. The receipt says partial. |
| The model's provider bills reasoning it does not stream, and the request fails or is cut after the provider accepted it | Charged the output budget it held, as the provider bills it, and the rest refunded. The receipt says partial. |
A refund goes back to the payer in the asset paid. The error that reports it names the amount and its transaction, or a reference while it is pending. A pending refund is retried until it lands.
A payment the gateway refuses costs nothing. Each refusal names its reason:
| Code | Status | What to do |
|---|---|---|
permit2_ | 402 | Approve USDF to the signature-transfer contract, then pay again after a minute. Paying in USDG is not affected. |
insufficient_ | 402 | Hold enough of the asset, then pay again after a minute. Paying in the other asset is not affected. |
authorization_ | 402 | Sign a new payment. Each one is single use. |
payment_ | 402 | Check the typed data and the accept you signed. |
quote_ | 402 | The price changed. Ask for a new quote. |
settlement_ | 402 | The transfer did not settle. Nothing was served. Pay again. |
settlement_ | 502 | The transfer's outcome is still unknown. Nothing was served. If it lands, it is refunded automatically; the message names the transaction. |
payment_ | 409 | This payment was already settled another way, for example refunded. Nothing was served. Sign a new payment. |
funds_ | 503 | Payments pause briefly while gateway instances hand over. Nothing was charged. Retry after Retry-After. |
payment_ | 429 | One payment per payer settles at a time. Retry after Retry-After. |
payments_ | 429 | The gateway is settling at capacity. Retry after Retry-After. |
payer_ | 429 | Too many paid requests per minute from this payer. Retry after Retry-After. Nothing was charged. |
rate_ | 429 | Too many paid requests from this IP address. Retry after Retry-After. |
payment_ | 400 | Send an x402 payment or an MPP credential, not both. |
After permit2_ or insufficient_, that address is refused in that asset for one minute, with insufficient_ and Retry-After. It can still pay in the other asset.
Every model endpoint takes the same payment. Send the body the keyed endpoint takes, with no key, to the same path under /.
| Path | Body | Quoted from |
|---|---|---|
POST / | Chat | The prompt at one token per byte, the output at max_ |
POST / | Messages format | The same as chat |
POST / | Responses format | The same as chat |
POST / | Legacy completions, prompt | The same |
POST / | model, input | One token per byte of each input |
POST / | model, prompt, n, size | Image units at the size asked, times n |
POST / | Multipart, with the image | Image units at the larger of the asked and uploaded size |
POST / | Multipart, with the audio file | The longest the file can decode to |
POST / | model, input, voice | Characters of input. The body is the audio; the receipt id is in x-request-id |
POST / | model, prompt, lyrics or instrumental | One track. The body is the audio; the receipt is in x-receipt |
POST / | model, query or queries, documents | One token per byte of each pair |
POST / | model, prompt, seconds | The clip length asked for. Answers 202 with the job and a read_ |
GET / | With Authorization: Bearer <read_ | The job. Add / for the clip. No token: 401 read_ |
GET / | None | The public receipt of any request, with no key |
Tools and batch jobs run on a key. A :batch model sent here is refused with batch_, and nothing is charged.
MPP, the Machine Payments Protocol, is the Payment HTTP authentication scheme. Every / path also answers it. The same 402 carries one WWW-Authenticate: Payment challenge per asset, with method evm and intent charge. The USDG authorization comes first, then USDF by signature transfer. A challenge expires after five minutes.
Pay by sending the same request again with Authorization: Payment <credential>. The credential is base64url JSON: challenge (as issued, unchanged), payload, and source as did:pkh:eip155:4663:<address>. The paid answer carries Payment-Receipt.
A refused credential gets 402 with fresh challenges and a problem type:
| Problem type | Meaning |
|---|---|
malformed-credential | The credential could not be read. |
invalid-challenge | Not issued by this gateway, altered, or already paid. |
payment-expired | The challenge or the signature has expired. |
invalid-payload | The payload does not have the shape its type needs. |
verification-failed | The payment does not match the challenge, or the payer cannot fund it. |
Contract wallets pay through ERC-1271. Over MCP with no key, the credential goes in _meta["org.paymentauth/credential"].
Pay with an MPP credential
npm install viem@2.56.8
import { getAddress, keccak256, stringToHex } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const url = "https://api.usdf.fi/x402/v1/chat/completions";
const body = JSON.stringify({
model: "gemma-3-4b-it",
messages: [{ role: "user", content: "Hello" }],
max_tokens: 100000,
});
const headers = { "content-type": "application/json" };
const payer = privateKeyToAccount(process.env.PAYER_KEY as `0x${string}`);
const fromB64 = (s: string) =>
JSON.parse(Buffer.from(s, "base64url").toString("utf8"));
// Ask without paying: 402, with one Payment challenge per asset
// in WWW-Authenticate.
const unpaid = await fetch(url, { method: "POST", headers, body });
const challenges = [...(unpaid.headers.get("www-authenticate") ?? "")
.matchAll(/Payment ((?:[a-z]+="(?:[^"\\]|\\.)*"(?:, )?)+)/g)].map((m) =>
Object.fromEntries([...m[1].matchAll(/([a-z]+)="((?:[^"\\]|\\.)*)"/g)]
.map((p) => [p[1], p[2].replace(/\\(.)/g, "$1")])),
);
// The USDG challenge: an EIP-3009 authorization whose nonce is
// keccak256(id || realm), valid until the challenge expires.
const c = challenges.find((x) =>
fromB64(x.request).methodDetails.credentialTypes[0] === "authorization")!;
const request = fromB64(c.request);
const nonce = keccak256(stringToHex(c.id + c.realm));
const validBefore = BigInt(Math.floor(Date.parse(c.expires) / 1000));
const signature = await payer.signTypedData({
domain: {
name: "Global Dollar",
version: "1",
chainId: request.methodDetails.chainId,
verifyingContract: getAddress(request.currency),
},
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: {
from: payer.address,
to: getAddress(request.recipient),
value: BigInt(request.amount),
validAfter: 0n,
validBefore,
nonce,
},
});
const credential = {
// The challenge as issued, every parameter (opaque included) unchanged.
challenge: {
id: c.id, realm: c.realm, method: c.method, intent: c.intent,
request: c.request, expires: c.expires, opaque: c.opaque,
...(c.description ? { description: c.description } : {}),
},
payload: {
type: "authorization", from: payer.address, to: request.recipient,
value: request.amount, validAfter: "0",
validBefore: validBefore.toString(), nonce, signature,
},
source: `did:pkh:eip155:${request.methodDetails.chainId}:${payer.address}`,
};
// The same request with the credential. The gateway settles it
// onchain, then serves it.
const paid = await fetch(url, {
method: "POST",
headers: {
...headers,
authorization: `Payment ${Buffer.from(JSON.stringify(credential)).toString("base64url")}`,
},
body,
});
const result = await paid.json();
console.log(result.choices[0].message.content);
console.log(fromB64(paid.headers.get("payment-receipt") ?? ""));A paid request can name the agent behind it. These headers are optional:
| Header | Effect |
|---|---|
x-erc8004-agent | An ERC-8004 agent id, or eip155:4663:<registry>:<agentId>. The payer must be the identity's owner or its agentWallet. This is checked before payment, at no cost. |
AP2-Mandate | An agent payment mandate. Recorded by its hash on the receipt, not verified. |
Signature-Input, Signature, Signature-Agent | An HTTP message signature credential. Recorded by its key id, not verified. |
A malformed id, or one in another registry, is refused with agent_. An unknown agent id gets agent_, and a payer that is not the identity's gets agent_. Each costs nothing.
Paid requests have their own limits, the gateway's defaults (signed in, read live from GET /v1/limits):
| Applies to | Scope | Limit | Error code |
|---|---|---|---|
| a paid x402 or MPP request from one caller address (IP), any endpoint | per caller address | 30 req/min | rate_ |
| a paid x402 or MPP request from one payer (its signature checked), any endpoint, before the shared budget below | per payer address | No limit | payer_ |
| every x402 and MPP settlement being verified or broadcast at once, across every payer | gateway-wide | 60 req/min | payments_ |
| a second payment from the same address while one is still settling | per payer address | 1 payment settling at a time | payment_ |
Settling payments are also capped at 8 in flight at once, across every payer. A payer the gateway has settled for before, and a contract wallet's signature check, each have a budget of their own of the same size. One caller's traffic cannot use up another's.
Over MCP, keyless chat shares these limits.