Use this when an OpenAI Agents SDK function tool is about to pay an x402 seller. Attach the guard to your existing official x402 client and call that fetch from the tool. This guide does not load a wallet key.
curl -fsSL -o x402-guard.ts https://moneytools.dev/products/payment-safety/examples/x402-guard.tsimport { Agent, run, tool } from "@openai/agents";
import { z } from "zod";
import { wrapFetchWithPayment, x402Client } from "@x402/fetch";
import { attachToX402Client } from "./x402-guard.ts";
const client = new x402Client(); // register your existing scheme/signer
attachToX402Client(client, { payer: "0xYourAgentWallet", maxUsd: 0.05, dailyMaxUsd: 1, escalate: "high" });
const paidFetch = wrapFetchWithPayment(fetch, client);
const fetchPaid = tool({
name: "fetch_paid",
description: "Fetch a URL that may require an x402 payment. Payments are pre-checked and capped.",
parameters: z.object({ url: z.string() }),
execute: async ({ url }) => {
const res = await paidFetch(url);
return { status: res.status, body: await res.text() };
},
});
const agent = new Agent({
name: "Paying agent",
instructions: "You may fetch paid URLs only through the fetch_paid tool.",
tools: [fetchPaid],
});
const result = await run(agent, "Fetch https://seller.example/data if it is safe to pay.");
console.log(result.finalOutput);
If your Agents SDK build uses a slightly different tool() helper, keep the same paidFetch line and drop it into your execute function.
Install is free. Checks are billed per call at 0.005 USDC via x402 after the live free tier (Pay Precheck: 5/IP/endpoint/day; Settle: trial 50 calls / 7 days; Approval Relay: 3/day). The guard never loads a key, never signs, and never pays MoneyTools automatically. Docs: https://moneytools.dev/products/payment-safety/