x402-guard with the Vercel AI SDK

Use this when a Vercel AI SDK tool is about to fetch an x402-paid URL. Attach the guard to your existing official x402 client, then use that paying fetch inside the tool. This guide does not load a wallet key.

Install (until npm is live): curl -fsSL -o x402-guard.ts https://moneytools.dev/products/payment-safety/examples/x402-guard.ts
After publish: npm install @moneytools/x402-guard
import { generateText, tool } from "ai";
import { z } from "zod";
import { wrapFetchWithPayment, x402Client } from "@x402/fetch";
import { attachToX402Client } from "./x402-guard.ts";
// after publish: import { attachToX402Client } from "@moneytools/x402-guard";

// Use the x402Client you already built (register your scheme/signer per @x402/fetch docs).
const client = new x402Client();
attachToX402Client(client, {
  payer: "0xYourAgentWallet",
  maxUsd: 0.05,
  dailyMaxUsd: 1,
  escalate: "high",
});
const paidFetch = wrapFetchWithPayment(fetch, client);

const fetchPaid = tool({
  description: "Fetch a URL. If the server returns HTTP 402, the official x402 client may pay, but only after x402-guard allows it.",
  parameters: z.object({ url: z.string().url() }),
  execute: async ({ url }) => {
    const res = await paidFetch(url);
    return { status: res.status, body: await res.text() };
  },
});

const { text } = await generateText({
  model: "openai/gpt-4.1-mini", // any model your app already uses
  prompt: "Fetch https://seller.example/data if it is safe to pay.",
  tools: { fetchPaid },
});
console.log(text);

The official client still signs. The guard only aborts onBeforePaymentCreation when Precheck, the local/daily cap, or Settle says no, or when Approval Relay is required.

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/

All framework guides · Payment-safety suite · llms.txt