{"slug": "x402-trinity-kills-the-cloud-server-back-end-for-local-ai-and-robotics", "title": "X402-trinity – kills the cloud server back end for local AI and robotics", "summary": "X402-trinity, a new open-source library, enables AI agents to autonomously pay for APIs and services via the HTTP 402 Payment Required protocol, eliminating the need for a hosted wallet service or backend server. The library, available in TypeScript and Python, handles payment challenges locally with hard spending limits and supports Base chain USDC transactions via EIP-3009, with the seller never holding private keys. It ships with zero runtime dependencies, is 7 KB gzipped, and includes safety features like mandatory caps and replay protection.", "body_md": "**A drop-in `fetch` replacement that lets an AI agent pay for things by itself — with hard\nspending limits, no hosted wallet service, and no changes to the agent's own code.**\n\nZero runtime dependencies. 7 KB gzipped. Buyer *and* seller in TypeScript; buyer in Python.\n\n```\nnpm install x402-trinity\n```\n\nWhen an agent hits a paid resource it gets `402 Payment Required`. Without something to\nhandle that, the request simply fails.\n\nx402-trinity handles it: reads the challenge, checks it against limits you set, signs, retries. All of it locally — no hosted wallet service, no third-party API, and the key never leaves your process.\n\nGas exists but the payer doesn't pay it — under EIP-3009 the facilitator submits the transfer, so an agent's wallet only ever needs USDC.\n\n``` js\nimport { createX402Fetch } from 'x402-trinity';\n\nconst x402Fetch = createX402Fetch({\n  privateKey: process.env.X402_PRIVATE_KEY,   // never hardcode\n  policy: {\n    maxAmountPerRequest: '5000',              // 0.005 USDC max per call  (REQUIRED)\n    totalBudget: '1000000',                   // 1.00 USDC lifetime       (REQUIRED)\n    allowHosts: ['api.example.com'],\n    allowPayTo: ['0x...'],\n  },\n});\n\nconst r = await x402Fetch('https://api.example.com/data');   // 402 handled, returns 200\n```\n\nOr patch the global so unmodified code pays automatically:\n\n``` js\nimport { installX402 } from 'x402-trinity';\nconst uninstall = installX402(cfg);   // globalThis.fetch now pays 402s\n```\n\n**Confirm which wallet will pay before funding anything:**\n\n```\nX402_PRIVATE_KEY=0x... npx x402-trinity-whoami\n```\n\nIt prints the address and its balance on every chain, and **never prints the key**. If the\naddress is not the wallet you meant, stop before sending anything.\n\n``` js\nimport { createX402Seller } from 'x402-trinity/seller';\nimport { createFileNonceStore } from 'x402-trinity/budget-file';\n\nconst seller = createX402Seller({\n  payTo: '0xYourWallet',      // 100% of every payment lands here\n  price: '10000',             // 0.01 USDC, atomic units\n  network: 'base',\n  facilitator: 'https://your-facilitator.example',   // REQUIRED — no default exists\n  nonceStore: createFileNonceStore('./.x402-nonces.json'),  // REQUIRED — see below\n});\n\n// in any fetch-style handler:\nconst gate = await seller.guard(request);\nif (gate.response) return gate.response;          // unpaid or refused — hand back the 402\nreturn new Response(yourData, { headers: seller.receiptHeader(gate.settlement) });\n```\n\nBoth required fields are deliberate. There is no default facilitator because settling is someone's real money and guessing an endpoint is not a default. And the replay guard has to outlive the process: an in-memory one forgets every settled payment on restart, so a buyer could re-present a spent authorization and get the resource again for free. The constructor throws rather than let either be implicit.\n\nThe seller never holds a private key and never touches funds. It quotes a price and asks a facilitator to verify and settle; money moves buyer → you directly on-chain.\n\n**This release ships Base + USDC only.** Any other EVM chain works through `customChains`;\nyour address is the same on all of them.\n\n| **Protocols** | x402 **v1 and v2** , detected per response. Unknown versions and MPP challenges declined with a clear reason, never guessed | \n| **Chains** | Base mainnet + USDC, shipped as the default. Any other EVM chain via `customChains` | \n| **Networks** | short names *and* CAIP-2 (`eip155:8453` ) | \n| **Custody** | `privateKey` , additive`shards` , or`remoteSign` (HSM/MPC) — your choice, not the architecture's | \n| **Speed** | 3.2 µs warm / 1.0 ms cold (TypeScript); 1.2 µs / 1.7 ms (Python) | \n| **Runtime** | auto-detects Cloudflare Workers and changes strategy; also Node, Bun, Deno | \n| **Safety** | mandatory caps, allowlists, never-pay-twice reconciliation, timing-hardened signing | \n\nBase is the default. The signing is chain-agnostic, so add whatever you need — including a testnet to rehearse on:\n\n``` js\nconst fetch2 = createX402Fetch({\n  privateKey: process.env.X402_PRIVATE_KEY,\n  maxAmountPerRequest: '10000',\n  totalBudget: '100000',\n  customChains: {\n    'base-sepolia': { id: 84532, asset: '0x036cbd53842c5426634e7929541ec2318f3dcf7e', name: 'USDC', version: '2' },\n  },\n});\n```\n\nVerify the entry against the deployed contract first: call `DOMAIN_SEPARATOR()` and check it\nequals what this library computes. A wrong `name` or `version` produces a signature that looks\nvalid and the contract rejects.\n\nBroadcast to a chain · hold funds · need gas or an RPC · take a cut of a payment (structurally impossible — EIP-3009 has one recipient) · require a hosted signer · pay without limits · guess at a protocol it doesn't speak.\n\n**Caps are mandatory.** `maxAmountPerRequest` and `totalBudget` have no defaults; the wrapper\nrefuses to construct without them. An auto-payer without limits is a money leak controlled by\nwhoever runs the server.\n\n**`totalBudget` alone is per-instance.** It is an in-memory counter that resets on restart, on\na new client, and on every Cloudflare Worker isolate — which is per request. On mainnet that\nturns a lifetime cap into a per-request cap. **Mainnet therefore requires a durable\n`budgetStore`**, or an explicit `acknowledgeEphemeralBudget: true`:\n\n``` js\nimport { createFileBudgetStore } from 'x402-trinity/budget-file';\n\ncreateX402Fetch({\n  ...,\n  budgetStore: createFileBudgetStore('./.x402-budget.json'),   // survives restarts\n});\n```\n\n**The key is in the process.** That is the trade for having no hosted signer. Bound it:\nuse a dedicated wallet holding only what you would accept losing, set `allowPayTo` so an\nexfiltrated key still cannot pay a stranger through this wrapper, and use `remoteSign` if you\nneed enclave-grade custody.\n\n**Timing hardening is hardening, not a proof.** Secret scalars use blinding plus\nalways-add-and-double, which cut the timing spread from 99.6% to 12% (Python: 99.9% → 1.1%).\nBigInt arithmetic is itself variable-time and no pure-JS implementation removes that.\n\nMCP is how an assistant gets tools. This exposes three:\n\n| tool |  | \n|---|---|\n| `check_price` | what a resource costs, **without paying** | \n| `pay_and_fetch` | fetch it, paying if it is within your limits | \n| `wallet_status` | address, balance, spent, remaining | \n\n```\n{\n  \"mcpServers\": {\n    \"x402-trinity\": {\n      \"command\": \"npx\",\n      \"args\": [\"-y\", \"x402-trinity-mcp\"],\n      \"env\": {\n        \"X402_PRIVATE_KEY\": \"0x...\",\n        \"X402_MAX_PER_REQUEST\": \"50000\",\n        \"X402_TOTAL_BUDGET\": \"1000000\",\n        \"X402_BUDGET_FILE\": \"./.x402-budget.json\",\n        \"X402_ALLOW_HOSTS\": \"api.example.com\",\n        \"X402_NETWORKS\": \"base\"\n      }\n    }\n  }\n}\n```\n\n**A model decides when these run.** It cannot be reasoned with about budgets, and a\npaywalled page can claim any price it likes. So the limits are not parameters the model can\nset — they come from the environment, and the server **refuses to start** without\n`X402_PRIVATE_KEY`, `X402_MAX_PER_REQUEST` and `X402_TOTAL_BUDGET`.\n\nSet `X402_BUDGET_FILE` too, or the lifetime ceiling resets every restart. Set\n`X402_ALLOW_HOSTS` and nothing else can be paid, however convincing the challenge looks.\nUse a wallet holding only what you would accept losing.\n\nSame protocol, standard library only. **Buyer only** — to charge for a resource, use the\nTypeScript seller. Aimed at long-lived processes: on-body agents, robotics controllers,\nharvesting scripts.\n\n``` python\nfrom x402_trinity import X402Client, Policy\n\nclient = X402Client(\n    private_key=os.environ[\"X402_PRIVATE_KEY\"],\n    policy=Policy(max_amount_per_request=5000, total_budget=1_000_000,\n                  allow_hosts=[\"api.example.com\"]),\n)\nbody = client.urlopen(\"https://api.example.com/data\").read()\n```\n\nOr as a decorator, so payment-unaware code just works:\n\n``` python\nfrom x402_trinity import x402_telemetry\n\n@x402_telemetry(private_key=KEY, policy=Policy(...))\ndef harvest():\n    return urllib.request.urlopen(\"https://sensor.local/v1/lidar\").read()\n```\n\nThe warm path is **1.1 µs** — a long-lived controller is warm after its first payment.\n\n```\nnpm install\nnpm run build        # dist/*.js + *.min.js + *.d.ts\n```\n\n`esbuild`, `typescript` and `wrangler` are **dev dependencies only**, used for building.\nThe shipped package has **zero runtime dependencies**.\n\nBusiness Source License 1.1. The source is open to read, modify and use non-commercially;\nproduction use is granted except as a hosted or managed service offering its functionality\nto third parties. It converts to MIT on 2029-08-25. See [LICENSE](/devmster/x402-trinity/blob/main/LICENSE).\n\nThis software moves money — read the additional notice there, and set your spending caps.\n\nConvenience fee: 0.1% per transaction and 1 cent every 100 transactions.", "url": "https://wpnews.pro/news/x402-trinity-kills-the-cloud-server-back-end-for-local-ai-and-robotics", "canonical_source": "https://github.com/devmster/x402-trinity", "published_at": "2026-09-09 14:31:25+00:00", "updated_at": "2026-09-09 14:43:58.178395+00:00", "lang": "en", "topics": ["ai-agents", "ai-infrastructure", "developer-tools", "artificial-intelligence"], "entities": ["x402-trinity", "Base", "USDC", "EIP-3009", "Cloudflare Workers"], "alternates": {"html": "https://wpnews.pro/news/x402-trinity-kills-the-cloud-server-back-end-for-local-ai-and-robotics", "markdown": "https://wpnews.pro/news/x402-trinity-kills-the-cloud-server-back-end-for-local-ai-and-robotics.md", "text": "https://wpnews.pro/news/x402-trinity-kills-the-cloud-server-back-end-for-local-ai-and-robotics.txt", "jsonld": "https://wpnews.pro/news/x402-trinity-kills-the-cloud-server-back-end-for-local-ai-and-robotics.jsonld"}}