Agent Payments (x402) - cred.diy API documentation
Agent Payments (x402)
An AI agent has no card, no checkout session and no patience for a redirect. x402 is the open standard that turns HTTP's long-unused 402 Payment Required into a price quote: the server answers with what it costs, the caller signs a stablecoin transfer, retries the same request, and gets served. Cred speaks x402 version 2 in both directions.
The handshake
- Client makes a normal request.
- Server answers 402 with a PAYMENT-REQUIRED header — base64 JSON naming the scheme, network, asset, amount and recipient.
- Client signs it and retries with a PAYMENT-SIGNATURE header.
- Server verifies and settles through a facilitator, then returns the result plus a PAYMENT-RESPONSE receipt.
Pay to keep calling the API
When your CRD platform balance runs out, credits-api no longer just fails — it quotes a top-up. An autonomous caller can buy credit and complete the request it was already making, without a human opening the dashboard.
// Any credits-api call can now be paid for inline. When your CRD runs
// out the API answers 402 with a price instead of simply failing.
const call = () => fetch(
'https://eutdgemlrpvnxfkkpvlu.supabase.co/functions/v1/credits-api/balance?user_id=user_123',
{ headers: { 'x-api-key': 'sk_live_your_api_key' } }
);
let response = await call();
if (response.status === 402) {
const offer = JSON.parse(atob(response.headers.get('PAYMENT-REQUIRED')));
// offer.accepts[0] -> { scheme, network, amount, asset, payTo, extra }
const payload = await wallet.signX402(offer.accepts[0]);
// Retry the *same* request with the signed payment attached.
response = await fetch(response.url, {
headers: {
'x-api-key': 'sk_live_your_api_key',
'PAYMENT-SIGNATURE': btoa(JSON.stringify(payload))
}
});
// 200 OK — credits were topped up and the original request ran.
// PAYMENT-RESPONSE header carries the on-chain settlement receipt.
}Sell your credits to agents
Turn on x402 under Dashboard → Agent Payments, give a receiving wallet and a price per credit, and any agent can buy your credits with stablecoin. Payments settle straight to your wallet — Cred quotes, verifies and issues, but never custodies the funds. Buyers with no Cred account are identified by the wallet they paid from.
// 1. Discover what Cred accepts — no credentials needed.
const res = await fetch(
'https://eutdgemlrpvnxfkkpvlu.supabase.co/functions/v1/credits-api/x402/supported' +
'?project_id=your_project_id'
);
const support = await res.json();
// {
// x402Version: 2,
// enabled: true,
// kinds: [{ x402Version: 2, scheme: 'exact', network: 'eip155:84532' }],
// asset: { address: '0x036CbD...', name: 'USDC', decimals: 6 },
// facilitator: 'https://x402.org/facilitator',
// platform_topup: { credits: 100, symbol: 'CRD', price_usd: 1, amount: '1000000' },
// project: {
// accepts_x402: true,
// purchase_url: '.../credits-api/x402/purchase',
// credit_type: { id: '...', name: 'Game Coins', symbol: 'GC' },
// price_per_credit_usd: 0.01,
// min_credits: 1,
// max_credits: 10000
// }
// }Buying, end to end
// 2. Ask to buy. With no payment attached you get a 402 carrying the price.
const url =
'https://eutdgemlrpvnxfkkpvlu.supabase.co/functions/v1/credits-api/x402/purchase';
const order = { project_id: 'your_project_id', credits: 500 };
const quote = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(order)
});
// quote.status === 402
const paymentRequired = JSON.parse(atob(quote.headers.get('PAYMENT-REQUIRED')));
// 3. Sign the chosen requirements with your wallet (EIP-3009 for USDC), then
// retry the same request with the signed payload.
const paid = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'PAYMENT-SIGNATURE': btoa(JSON.stringify(signedPaymentPayload))
},
body: JSON.stringify(order)
});
const receipt = await paid.json();
// {
// success: true,
// credits_purchased: 500,
// new_balance: 500,
// buyer_id: 'x402:0xAgentWallet...',
// payer: '0xAgentWallet...',
// price_paid: 5,
// currency: 'USDC',
// transaction: '0xabc...'
// }
// The settlement receipt is also on the PAYMENT-RESPONSE response header.x402 Endpoints
| Endpoint | Description |
|---|---|
| GET /x402/supported | Public. What Cred accepts, and a project's credit price list |
| POST /x402/purchase | Public. Buy a project's credits by paying in stablecoin |
| GET /x402/receipts | Payments taken for your project, with transaction hashes |
| any endpoint, on 402 | Attach PAYMENT-SIGNATURE to top up CRD and run the request |
Deployment configuration
x402 stays switched off until X402_PAY_TO names a receiving wallet — an advertised price with nowhere to pay it would strand every agent that tried. Everything else has a Base Sepolia default.
| Variable | Default |
|---|---|
| X402_PAY_TO | required — treasury wallet for CRD top-ups |
| X402_NETWORK | eip155:84532 |
| X402_ASSET | 0x036CbD…CF7e (USDC) |
| X402_FACILITATOR_URL | https://x402.org/facilitator |
| X402_CRD_PRICE_USD | 0.01 per CRD |
| X402_CRD_TOPUP_CREDITS | 100 CRD per top-up |
| X402_ENABLED | set to false to turn off without unsetting the wallet |
The defaults target Base Sepolia testnet through the public facilitator. Before pointing X402_NETWORK at a mainnet chain, move X402_FACILITATOR_URL to a production facilitator — the x402 spec explicitly says the public one is for development and testnets.
Settling in a different asset
By default every project quotes the platform's asset. Override it per project in Dashboard → Agent Payments: token contract, decimals, network, and the token's EIP-712 domain name and version. GET /x402/supported?project_id=… then publishes your asset alongside the platform's, so an agent learns which token to hold before it signs anything.
The asset and its decimals are one setting. An override without decimals is ignored rather than half-applied — quoting a new contract with the old contract's decimals is a payment wrong by orders of magnitude.
Stablecoins only. The quote is your price per credit times the number of credits, expressed in the asset's units, with no price source — which assumes one unit is one dollar. True of a USD stablecoin, false of anything that floats.
Through MCP
The Cred MCP server exposes the same thing to agents as tools: x402_supported to discover prices and buy_credits_x402 to purchase. Any other tool that hits a spent CRD balance returns the x402 payment requirements as its structured error content, so a paying client can sign and retry. Neither tool needs an API key — the payment is the credential.
Your first credit is one request away.
Create a project, copy an API key, and issue a credit from your server.
