{"slug": "make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc", "title": "Make AI agents pay your API per call — one command, no API keys (x402 + USDC)", "summary": "A developer released x402-kit, a scaffolding tool that generates paid APIs, paid MCP servers and paywalled file stores using the HTTP 402 status code and USDC payments on Base, so AI agents can pay per call without accounts or API keys. The kit runs on Express, Hono, Next.js, Cloudflare Workers or FastAPI, supports free and paid tools in the same MCP server, and only settles payment after a handler succeeds, so failed calls are not charged. A bundled `pay` command uses the stock x402 Foundation client, caps spend with `--max-usd` (default $0.05), and prints the settlement transaction with a Basescan link.", "body_md": "AI agents can't sign up for your API. They can't fill in a billing form, verify an email or paste a key into a dashboard. But they can hold a wallet and sign a payment.\n\nThat's what **x402** is for. It's the HTTP `402 Payment Required` status code, finally put to use. Your server answers a request with `402` and a price. The agent signs a USDC payment and retries. Your server settles the payment and returns the content. No accounts, no keys, no invoices.\n\nThis post shows the fastest way I know to ship it: one command that builds a paid API, a paid MCP server or a paid file store. It runs on Express, Hono, Next.js, Cloudflare Workers or FastAPI.\n\n```\nnpm create @scriptmasterlabs/x402 my-api\n```\n\nNo global install. Prefer to run it straight from source? `npx gitlab:script-master-labs-llc-group/x402-kit my-api` or `npx github:Timwal78/x402-kit my-api` do the same thing.\n\n| Mode | What it does | \n|---|---|\n| `--mode mcp` | A **paid MCP server** . Mix free and paid tools; agents pay per tool call. | \n| `--mode wrap` | A **paywall in front of an API you already run** , with no changes to that API. | \n| `--mode file` | **Sell files** : PDFs, datasets, ebooks, audio. Each file can have its own price. | \n\nPick a stack with `--stack express | hono | next | workers | fastapi`.\n\n```\nnpm create @scriptmasterlabs/x402 my-mcp -- --stack express --mode mcp --pay-to 0xYourWallet --price 0.01\ncd my-mcp\nnpm install\nnpm start\n```\n\n`--pay-to` is the wallet that receives USDC on Base. Payments go straight to it; the facilitator never holds your money. If you leave it out, paid routes answer `503 seller_not_configured` until you set `PAY_TO` in `.env`. The kit never falls back to a placeholder wallet.\n\nCall the paid tool without paying:\n\n```\ncurl -i -X POST localhost:3000/mcp -H 'content-type: application/json' \\\n  -d '{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"text_stats\",\"arguments\":{\"text\":\"hello agents\"}}}'\nHTTP/1.1 402 Payment Required\nPAYMENT-REQUIRED: eyJ4NDAyVmVyc2lvbiI6Mi...\n```\n\nThe challenge, which says what to pay, how much, in what asset and to which wallet, is in the JSON body and in the base64 `PAYMENT-REQUIRED` header. Any standard x402 client can read it, sign, and retry with a `PAYMENT-SIGNATURE` header.\n\nThe free tool (`ping`) answers right away. `tools/list` labels the paid tools with their price, so agents know before they call.\n\nOpen `src/tools.mjs`:\n\n``` js\nexport const tools = [\n  {\n    name: 'ping',\n    description: 'Free health check.',\n    handler: async () => ({ ok: true }),\n  },\n  {\n    name: 'text_stats',\n    description: 'Word, sentence and character counts plus the top words.',\n    priceUsd: '0.01',               // remove this line and the tool is free\n    inputSchema: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'] },\n    handler: async ({ text }) => { /* ... */ },\n  },\n];\n```\n\nIf a handler throws, the caller gets a tool error and **is not charged**. The kit only settles a payment after your code succeeds.\n\nFund a throwaway wallet with a little USDC on Base, then:\n\n```\nBUYER_PRIVATE_KEY=0x<throwaway wallet> npx @scriptmasterlabs/create-x402 pay http://localhost:3000/mcp \\\n  --method POST --data '{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"text_stats\",\"arguments\":{\"text\":\"hello agents\"}}}'\n```\n\n`pay` uses the **stock x402 Foundation client** (`@x402/fetch` + `@x402/evm`). It reads the 402, refuses anything above `--max-usd` (default $0.05), pays, and prints the settlement transaction with a Basescan link.\n\nThen open `http://localhost:3000/x402/receipts`. Every settled payment is listed there with its transaction hash.\n\n```\nnpm create @scriptmasterlabs/x402 my-proxy -- --stack hono --mode wrap \\\n  --upstream https://api.yourservice.com --pay-to 0xYourWallet --price 0.02\n```\n\nEvery request is proxied to your upstream after payment. In `.env` you can:\n\n`ROUTE_PRICES={\"/v1/search\":\"0.05\"}`\n`FREE_PATHS=/health,/docs*`\n`UPSTREAM_AUTH_HEADER=Authorization: Bearer <key>`. Buyers never see it.\n\n```\nnpx @scriptmasterlabs/create-x402 add\n```\n\nThis drops a dependency-free core into your project, plus an `X402.md` with the wiring:\n\n```\n// Express\napp.get('/report', paid(x402, '0.02', 'One full report'), handler);\n\n// Hono / Next.js route handlers / Cloudflare Workers\nreturn x402.protect(request, { priceUsd: '0.02' }, () => Response.json(data));\n```\n\nPython projects get `x402_seller.py` with `await x402.protect(request, \"0.02\", \"One report\", handler)`.\n\nEvery generated project ships an `AGENTS.md`. Give Claude Code or Cursor one sentence:\n\nRun `npm create @scriptmasterlabs/x402 my-api` to make my API charge AI agents per call, then follow the generated AGENTS.md.\n\n`render.yaml` blueprint is included. `npx wrangler deploy`. `PUBLIC_URL` to your https URL.`/.well-known/x402` (machine-readable paid resources) and `/llms.txt` (for AI crawlers).`/x402/badge.svg` is a live \"N paid calls · $X\" badge for your README.\nA facilitator verifies the signed payment and broadcasts it on-chain. The kit's default is **OMNIGATE** (`https://mcp-x402.onrender.com`), the facilitator my company, Script Master Labs, runs for USDC on Base and Arc.\n\nWhat that means for you:\n\n`/supported`` FACILITATOR_URLS` is a comma list tried in order. To keep the facilitator you use today and only fall back to OMNIGATE when it errors:\n\n```\nFACILITATOR_URLS=https://your-current-facilitator.example,https://mcp-x402.onrender.com\n```\n\nBefore publishing, every stack and mode was scaffolded and paid with the stock x402 client against the live facilitator, plus a real $0.01 USDC settlement on Base through a kit-built app. Both wallets in that settlement were ours, so it proves the kit works end to end, not that anyone else uses it yet.\n\nnpm: **@scriptmasterlabs/create-x402** · Repo and issues: **[https://gitlab.com/script-master-labs-llc-group/x402-kit](https://gitlab.com/script-master-labs-llc-group/x402-kit)** (GitHub copy: [https://github.com/Timwal78/x402-kit](https://github.com/Timwal78/x402-kit))\n\nIf you build something with it, I'd like to see it.", "url": "https://wpnews.pro/news/make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc", "canonical_source": "https://dev.to/scriptmasterlabs01/make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc-48ig", "published_at": "2026-11-05 20:45:16+00:00", "updated_at": "2026-11-05 20:49:31.537018+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "ai-tools", "developer-tools", "ai-infrastructure"], "entities": ["x402", "USDC", "Base", "x402-kit", "ScriptMaster Labs", "Cloudflare Workers", "FastAPI", "x402 Foundation"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc", "markdown": "https://wpnews.pro/news/make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc.md", "text": "https://wpnews.pro/news/make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc.txt", "jsonld": "https://wpnews.pro/news/make-ai-agents-pay-your-api-per-call-one-command-no-api-keys-x402-usdc.jsonld"}}