diff --git a/src/content/docs/agents/tools/payments/x402/pay-with-tool-plugins.mdx b/src/content/docs/agents/tools/payments/x402/pay-with-tool-plugins.mdx index 8fec047db45..f3e710efc4b 100644 --- a/src/content/docs/agents/tools/payments/x402/pay-with-tool-plugins.mdx +++ b/src/content/docs/agents/tools/payments/x402/pay-with-tool-plugins.mdx @@ -13,8 +13,16 @@ The following examples show how to add x402 payment handling to AI coding tools. Both examples require: - A wallet private key (set as `X402_PRIVATE_KEY` environment variable) +- The hosts the tool is allowed to pay, comma-separated (set as `X402_ALLOWED_HOSTS` environment variable) - The x402 packages: `@x402/fetch`, `@x402/evm`, and `viem` +Because the tool pays without asking, both examples limit what it can spend before any payment is signed: + +- **A per-payment cap.** The x402 client caps each payment at $1 by default. `setSpendControls` sets the most you will pay for one request. +- **An allowlist of hosts.** The `onBeforePaymentCreation` hook runs before each payment is signed and refuses any host not in `X402_ALLOWED_HOSTS`. It checks the URL the agent asked for, not the `resource.url` in the 402 response, which the seller writes. Without it, a page with injected instructions could steer the agent into paying a site you never chose. + +The same hook is where a human confirmation or a payee check belongs, for example comparing the quoted `payTo` with an address the seller publishes for its domain. + ## OpenCode plugin OpenCode plugins expose tools to the agent. To create an `x402-fetch` tool that handles 402 responses, create `.opencode/plugins/x402-payment.ts`: @@ -42,13 +50,28 @@ export const X402PaymentPlugin: Plugin = async () => ({ throw new Error("X402_PRIVATE_KEY environment variable is not set."); } - // Your human-in-the-loop confirmation flow... - // const approved = await confirmPayment(args.url, estimatedCost); - // if (!approved) throw new Error("Payment declined by user"); - const account = privateKeyToAccount(privateKey as `0x${string}`); const client = new x402Client(); registerExactEvmScheme(client, { signer: account }); + // The most you will pay for one request (the default is $1). + client.setSpendControls({ maxAmountPerPayment: "$0.10" }); + + // Runs before each payment is signed. Pay only hosts you listed, checked + // against the URL the agent asked for (not the 402's resource.url). + const allowed = (process.env.X402_ALLOWED_HOSTS ?? "") + .split(",") + .map((h) => h.trim()) + .filter(Boolean); + const host = new URL(args.url).hostname; + client.onBeforePaymentCreation(async () => { + if (!allowed.includes(host)) { + return { abort: true, reason: `${host} is not in X402_ALLOWED_HOSTS` }; + } + // Your human-in-the-loop confirmation flow... + // const approved = await confirmPayment(args.url); + // if (!approved) return { abort: true, reason: "Payment declined by user" }; + }); + const paidFetch = wrapFetchWithPayment(fetch, client); const response = await paidFetch(args.url, { @@ -96,13 +119,28 @@ if (!privateKey) { } try { - // Your human-in-the-loop confirmation flow... - // const approved = await confirmPayment(url); - // if (!approved) process.exit(0); - const account = privateKeyToAccount(privateKey); const client = new x402Client(); registerExactEvmScheme(client, { signer: account }); + // The most you will pay for one request (the default is $1). + client.setSpendControls({ maxAmountPerPayment: "$0.10" }); + + // Runs before each payment is signed. Pay only hosts you listed, checked + // against the URL the agent asked for (not the 402's resource.url). + const allowed = (process.env.X402_ALLOWED_HOSTS ?? "") + .split(",") + .map((h) => h.trim()) + .filter(Boolean); + const host = new URL(url).hostname; + client.onBeforePaymentCreation(async () => { + if (!allowed.includes(host)) { + return { abort: true, reason: `${host} is not in X402_ALLOWED_HOSTS` }; + } + // Your human-in-the-loop confirmation flow... + // const approved = await confirmPayment(url); + // if (!approved) return { abort: true, reason: "Payment declined by user" }; + }); + const paidFetch = wrapFetchWithPayment(fetch, client); const res = await paidFetch(url, { method: "GET" });