Pay an API in 10 lines.
Every Vend402 endpoint speaks the open x402 protocol. Hit the URL, get a 402 challenge, sign an EIP-3009 USDC authorization, retry with the X-PAYMENT header. The facilitator settles on-chain. No accounts, no API keys.
1. Install
The official buyer-side SDK. One class, zero protocol knowledge required.
npm install @vend402/sdk viem2. Pay & call
Drop in a wallet private key with a little USDC. vend.fetch() probes the endpoint, parses the 402 challenge, enforces your spending cap, signs the authorization locally, and retries — all in one call.
// agent.ts — Node 20+ or any modern runtime
// npm i @vend402/sdk viem
import { Vend402Client, getSettlement } from "@vend402/sdk";
const vend = new Vend402Client({
privateKey: process.env.AGENT_PRIVATE_KEY, // Base wallet — needs USDC only, no ETH
network: "base-sepolia", // or "base"
maxPaymentPerCallUsdc: 0.05, // safety cap: never pay more per call
});
// Call ANY Vend402-monetized endpoint. The SDK handles the 402 challenge,
// signs a gasless EIP-3009 USDC authorization, and retries automatically.
const res = await vend.fetch(
"https://vend402.com/api/x402/weather"
);
console.log(res.status); // 200
console.log(await res.json()); // paid response
console.log(getSettlement(res)); // { success, transaction, network }
3. Handle errors
Every failure mode is a typed PaymentError with a machine-readable code — so agents can react programmatically.
import { PaymentError } from "@vend402/sdk";
try {
const res = await vend.fetch(url);
} catch (err) {
if (err instanceof PaymentError) {
// err.code:
// SPENDING_CAP_EXCEEDED — endpoint charges more than your cap; nothing was signed
// PAYMENT_REJECTED — server refused the payment (see err.message)
// UNSUPPORTED_NETWORK — endpoint wants a chain you didn't configure
// MALFORMED_402 — response wasn't a valid x402 challenge
console.error(err.code, err.message);
}
}Pre-release: the SDK source ships in this repo under /sdk and will be published to npm shortly. Prefer raw protocol access? See the curl tab.
Replace /api/x402/weather with the endpoint from any catalog listing. Every Vend402 API speaks the same protocol.