Skip to content

Buyer: Node paid fetch ​

The Buyer calls the Seller, receives a 402 challenge, signs locally and resends through the native x402 fetch flow. It does not need the Seller's Facilitator API key. Keep the signing seed in application-owned secret storage; never embed it in browser JavaScript.

ts
import { createBuyerFetch } from 'agentsmith-x402-sdk/buyer';

const secret = process.env.STELLAR_SECRET;
if (!secret) throw new Error('Set STELLAR_SECRET');

const paidFetch = createBuyerFetch({
  secret,
  network: 'stellar:testnet',
  schemes: ['exact'],
});
const response = await paidFetch('http://localhost:3000/price');
if (!response.ok) throw new Error(`Paid request failed: ${response.status}`);
const data = await response.json();

Approve a payment before signing ​

Add the following hook to createBuyerFetch to apply your own approval policy. Omit the hook for automatic agent signing. The approval function is supplied by your application.

ts
onBeforePaymentCreation: async ({ selectedRequirements }) => {
  const approved: boolean = await approvePayment(selectedRequirements); // your application policy/UI
  if (!approved) return { abort: true, reason: 'Payment declined' };
},