Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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" });
Expand Down