Call a Paid MCP Server from Your Code (x402)
Run indie.money agents from your own code and pay per call with the open x402 standard — bring your own signer, your key never leaves your machine.
Written By pvdyck
Last updated About 2 hours ago
Call a Paid MCP Server from Your Code (x402)
30-second version
Call any agent's webhook from your code. Get a 402 with the price. Sign and retry. Done — no account, no API key, your key never leaves your machine.
const fetchWithPayment = wrapFetchWithPayment(fetch, client); // client = your signerconst res = await fetchWithPayment("https://<agent-webhook-url>", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ city: "San Francisco" }),}); // 402 → sign → retry happens automatically; you just read the resultBonus — price discovery is free. Call any paid operation without a signature and the 402 response is the quote. Inspect the cost before spending a token — start with a low-cost call to confirm your sign-and-retry loop end to end.
This article is for developers who want to run indie.money agents directly from their own code — an app, a backend service, or an autonomous agent framework that holds its own signing key. Each run is paid for per call using the open x402 payment protocol. There's no account, no API key, and no platform-held wallet: your code signs the payment and keeps the key entirely on your side.
Use this path when your code holds the key. If you'd rather have an AI assistant manage a wallet for you in plain English, start with Use indie.money with AI Assistants. If you want a local signer wired into a desktop AI client (Claude Desktop, Cursor, …), see Bring Your Own Wallet. This article is the programmatic path: you call an agent, your code signs, the agent returns.
New to picking an endpoint? Choosing Your MCP Endpoint compares the managed-wallet and bring-your-own-signer routes side by side.
What is x402
x402 is an open payment protocol that reuses the HTTP 402 Payment Required status code so a client can pay with stablecoins on a per-request basis — no accounts, no sessions, no API keys. When you call a paid endpoint, the server answers with a 402 describing the price; your client signs the payment for that amount and retries. The payer's signature is gasless — the platform broadcasts it on-chain for you. Payment is in USDC on Base.
x402 is a Linux Foundation standard (contributed April 2026, Apache-2.0 licensed), backed by Coinbase, Cloudflare, Google, Stripe, Visa, AWS, Mastercard, and Circle. That means zero protocol fees, zero account creation, zero vendor lock-in. indie.money is one x402 endpoint among many — any x402 client reaches it, so the code you write here is the same code you'd point at any other x402 provider.
indie.money runs on Base (chain 8453) with real USDC.
Pay per call through an MCP client (api-mcp gateway)
The api-mcp gateway is an MCP endpoint that holds no key and signs nothing. You connect any x402-capable MCP client. When you call a paid tool without a payment, the gateway returns a structured payment challenge; your client signs it with its own wallet and retries the same call with the payment attached.
How the challenge → sign → retry loop works
- Your client calls a paid tool with no payment.
- The result comes back flagged as an error carrying the payment challenge — the price, token, network, and pay-to address.
- Your client signs that challenge with its own wallet.
- Your client retries the same tool call with the signed payment attached.
- The agent runs and returns the result.
Price discovery is free. Call any paid operation without a key to see exactly what it costs before you commit a single token. The 402 challenge is itself the price quote.
Registering it in an MCP client
Point your client at the audience URL — it's a standard MCP server. Your client supplies the signer; the gateway never sees your key.
{ "mcpServers": { "indie-money": { "url": "https://api-mcp.indie.money/api-mcp/buyer" } }}Swap buyer for maker, credentials, or public depending on what you're doing.
Pay per call over raw HTTP (no MCP)
You don't need MCP at all. You can call an agent's webhook endpoint directly over HTTP. The first call (no payment) returns an HTTP 402 Payment Required whose body carries an accepts list describing the price, token, network, and pay-to address. Your client signs the payment and retries with a signed authorization; the agent runs and returns the result. This is the raw x402 flow — same standard, no MCP needed.
The simplest way to do this is a wrapped fetch that handles the 402 → sign → retry loop automatically. @x402/fetch does exactly that:
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";import { registerExactEvmScheme } from "@x402/evm/exact/client";import { privateKeyToAccount } from "viem/accounts";const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);const client = new x402Client();registerExactEvmScheme(client, { signer });const fetchWithPayment = wrapFetchWithPayment(fetch, client);// Calls the agent's webhook; the 402 challenge is signed and retried automatically.const res = await fetchWithPayment("https://<agent-webhook-url>", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ city: "San Francisco" }),});The wrapped fetch is a drop-in replacement for the standard one: your code reads the response as usual, and the payment round-trip happens transparently underneath.
Use x402 in an agent framework (Cloudflare Agents)
Any x402-capable client library works — the protocol is vendor-neutral. If your stack already has an x402 integration, point it at an indie.money endpoint and it just works.
As a concrete example, Cloudflare's Agents SDK ships first-class x402 support. You connect to indie.money's bring-your-own-signer MCP gateway, wrap the connection with withX402Client and your signer, then authorize each spend through a confirmation callback:
import { withX402Client } from "agents/x402";import { privateKeyToAccount } from "viem/accounts";// Connect to indie.money's bring-your-own-signer MCP gatewayconst { id } = await this.mcp.connect("https://api-mcp.indie.money/api-mcp/buyer");const account = privateKeyToAccount(this.env.MY_PRIVATE_KEY as `0x${string}`);const paid = withX402Client(this.mcp.mcpConnections[id].client, { network: "base", account,});// onPaymentRequired(requirements) => Promise<boolean> — return true to authorize the spendawait paid.callTool(this.onPaymentRequired, { name: "execute_service", arguments: { /* webhook path + input */ },});The onPaymentRequired callback is your spending gate: it receives the payment requirements and returns true to authorize or false to decline, so an autonomous agent can enforce its own budget rules before any token moves.
Pin your versions. Cloudflare's Agents x402 API moved through a v1 → v2 change recently. Pin the versions you build against and follow the current Cloudflare Agents docs.
Spending controls
Every payment passes through your gate before any token moves. The onPaymentRequired(requirements) callback receives the price and returns true to authorize or false to decline — so an autonomous agent enforces its own budget before it spends:
async onPaymentRequired(requirements) { const price = Number(requirements.amount); // microtokens if (this.spentToday + price > this.dailyCapMicros) return false; // cap reached this.spentToday += price; return true; // authorize}Pair this with free price discovery — your agent knows the exact cost before it authorizes. Set per-run caps, daily budgets, or allow-lists: autonomy without a runaway wallet.
Funding your signer
Your signer's wallet address needs USDC on Base (chain 8453) — fund the address directly with USDC. It's accepted at checkout with no conversion step.
Start with a single low-cost call to confirm your sign-and-retry loop end to end before wiring it into an autonomous agent.
Agents paying agents
indie.money agents are first-class participants in agent-to-agent commerce. The get_agent_card tool returns a discovery card (skills, input/output, pricing) so an autonomous agent can find an indie.money agent, read its price from the 402 challenge, pay with x402, and chain the result into the next paid call — no human in the loop. Your orchestrator buys capability from an indie.money agent the same way it would from any x402 endpoint, so paid pipelines compose cleanly across providers.
See also
- x402 protocol specification — the open standard this article builds on
- x402 Foundation (Linux Foundation) — the foundation governing x402
- Coinbase x402 on GitHub — reference SDKs and examples
- Cloudflare Agents docs —
withX402Clientand the Agents SDK - Use indie.money with AI Assistants — managed-wallet, no-code path
- Bring Your Own Wallet — local signer for desktop AI clients
- Choosing Your MCP Endpoint — managed vs. bring-your-own-signer