Skip to main content
@vet402/sdk safePayx402Key: stays with the payer
vet402December 2026

Pay safely from a payment runtime

Add safePay or wrapFetchWithSafePay at the one place a payment runtime pays an x402 URL. vet402 checks the seller first, swaps in a seller with the same input shape when it stops a payment, and the payer's own wallet signs.

Why one place

A runtime that pays x402 sellers for its users has one function where a 402 turns into a signed payment. Put safePay there and each payment the runtime makes is checked against vet402's purchase record first. When vet402 stops a payment, the job can still finish with another seller vet402 saw deliver the same use in the last 30 days, instead of failing.

safePay is in @vet402/sdk from 0.10.8. Until that version is on npm, build packages/sdk from the repository. The full option list is in the SDK README under Pay safely.

  • The key stays with the payer. safePay takes an account with signTypedData, and vet402 does not receive it.
  • vet402 holds no funds. Money moves from the payer's wallet to the seller.
  • vet402 stores no receipt and sends nothing to the seller. The runtime decides where receipts and refund requests go.

In your pay function

Replace the call that pays with safePay. Pass the payer's signer and the most the job may spend.

import { safePay } from "@vet402/sdk";

// The one place your runtime pays an x402 URL for a user or an agent.
// account is the payer's own signer (any object with address and signTypedData).
export async function payX402(job: { url: string; init?: RequestInit; maxUsd: number }, account) {
  const result = await safePay(job.url, job.init, { account, maxUsd: job.maxUsd });

  if (result.status === "paid" || result.status === "no_payment_required") {
    // finalUrl may be another seller with the same input shape.
    return { ok: true, url: result.finalUrl, response: result.response, receipt: result.receipt };
  }
  // Nothing more was paid. Hand the choice back to the user or agent.
  return { ok: false, status: result.status, reasons: result.reasonCodes, alternatives: result.alternatives };
}

As a fetch wrapper

When the runtime already wraps fetch to pay 402s, wrapFetchWithSafePay(fetch, account) has the same shape. It resolves with the answer when one arrived and throws SafePayError with the full result otherwise.

import { wrapFetchWithSafePay, SafePayError } from "@vet402/sdk";

// Same call shape as fetch. Swap it in where the runtime already calls a paying fetch.
const payingFetch = wrapFetchWithSafePay(fetch, account, {
  maxUsd: 0.05,
  onResult: (r) => log({ url: r.finalUrl, attempts: r.attempts, spentUsd: r.spentUsd }),
});

try {
  const res = await payingFetch("https://api.example.com/v1/quote", { method: "POST", body });
  return await res.json();
} catch (e) {
  if (e instanceof SafePayError) return { stopped: e.result.status, alternatives: e.result.alternatives };
  throw e;
}

What it does

  • vet402 checks the URL first. ALLOW pays it. WARN, BLOCK or no record stops the payment before a signature exists.
  • After a stop, safePay tries the listings vet402 names for the same use, in vet402's order. It pays one on its own when the listing's inputShapeHash equals the checked listing's, so the same request fits both.
  • Any other alternative comes back in alternatives with the reason (input_shape_unknown, input_shape_differs, network_differs, not_tried) for the runtime or its user to decide.
  • At most one payment succeeds in a call. Once an answer arrives, nothing else is tried.
  • When a payment was signed and the seller did not settle, safePay stops and counts that amount, so a retry cannot pay twice.
  • maxUsd caps the total signed across the attempts in one call. spentUsd reports it.
  • When vet402 cannot be read in time, the result is degraded and nothing more is signed.

Read the result

  • status: paid, no_payment_required, refused, failed or degraded.
  • attempts: each URL tried, vet402's verdict, whether it was signed and paid, the transaction and the USD signed.
  • delivery: whether the answer arrived (a 2xx status, a body, and the content type the seller declared), and whether the body fits a declared output schema.
  • receipt: the transaction, the seller's 402, the answer's status and body hash and vet402's verdict, signed by the payer's wallet as EIP-712 typed data.
  • refundRequest: when a signed payment did not deliver, a ready text and the evidence for the seller. The runtime sends it, or passes it to its user.

Plan without the SDK

A runtime in another language can read the same plan and sign with its own code. The hosted MCP tool safe_pay_plan returns the payee, the price of the last delivered purchase as suggested_max_usd, and each alternative with same_input_shape and pay_automatically. GET /api/v1/check returns the same alternatives and inputShapeHash. See the MCP docs and the API reference.

# Hosted MCP, no key: the plan safePay would follow, for your own signer
curl -s https://vet402.com/mcp -H 'content-type: application/json' \
  -H 'accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"safe_pay_plan","arguments":{"url":"https://api.example.com/v1/quote"}}}'

Report a result

A runtime may send a receipt to POST /api/v1/safepay/reports. vet402 counts a report when the receipt signature checks and the transaction is found on chain, once per transaction. The seller page shows the counts of delivered and not delivered reports, with no body and no buyer. Until the report store is open, the route answers 503 with not_accepting_yet and keeps nothing.

curl -s -X POST https://vet402.com/api/v1/safepay/reports \
  -H 'content-type: application/json' \
  -d "{\"receipt\": $(jq -c .receipt result.json)}"