x402-guard with LangChain (JS and Python)

Use this when a LangChain tool is about to pay an x402 seller. Attach the guard to the official x402 client, or wrap the HTTP session inside your payment adapter. This guide does not load a wallet key.

JavaScript

Install (until npm is live): curl -fsSL -o x402-guard.ts https://moneytools.dev/products/payment-safety/examples/x402-guard.ts
import { tool } from "@langchain/core/tools";
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);

export const fetchPaid = tool(async ({ url }) => {
  const res = await paidFetch(url);
  return await res.text();
}, { name: "fetch_paid", description: "Fetch a URL that may require an x402 payment", schema: z.object({ url: z.string().url() }) });

Python

Install (until PyPI is live): curl -fsSL -o moneytools_x402_guard.py https://moneytools.dev/products/payment-safety/examples/x402-guard.py
After publish: pip install moneytools-x402-guard (not the unrelated PyPI package named x402-guard).
from langchain_core.tools import tool
from moneytools_x402_guard import attach_to_x402_client, wrap_requests, before_pay

# Option A — official x402 client hook (client already has your signer):
# from x402 import x402Client
# attach_to_x402_client(client, {"payer": "0xYourAgentWallet", "max_usd": 0.05, "daily_max_usd": 1, "escalate": "high"})

# Option B — call the hook yourself, then let your existing adapter pay:
@tool
def fetch_paid(url: str) -> str:
    """Fetch a URL that may require an x402 payment."""
    decision = before_pay(
        {"seller_url": url, "method": "GET"},
        {"payer": "0xYourAgentWallet", "max_usd": 0.05, "daily_max_usd": 1, "escalate": "high"},
    )
    if not decision["proceed"]:
        return f"payment blocked: {decision['reason']}"
    # only then use your existing x402-aware session / adapter
    return "ok to pay; call your existing x402 session next"

Python callers should set a User-Agent; the guard already does for MoneyTools API calls. The default Python-urllib UA is blocked on some workers.dev hosts.

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