x402-paid-agent-starter / tree / typescript/scripts/pay.ts ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Pays one call to a running starter (used for the Base Sepolia proof, acceptance A5).
// Usage: PAYER_KEY=0x... URL=http://localhost:8402/paid npx tsx scripts/pay.ts
import { privateKeyToAccount } from 'viem/accounts';
import { exactPaymentHeader } from '../src/evm-pay.js';
import { decodeHeader } from '../src/x402.js';

const key = process.env.PAYER_KEY;
if (!key) { process.stderr.write('Set PAYER_KEY in the environment.\n'); process.exit(1); }
const url = process.env.URL ?? 'http://localhost:8402/paid';
const account = privateKeyToAccount(key as `0x${string}`);
const first = await fetch(url);
if (first.status !== 402) { process.stderr.write(`expected 402, got ${first.status}\n`); process.exit(1); }
const terms = await first.json();
const header = await exactPaymentHeader(terms, account, { maxAtomic: BigInt(process.env.MAX_ATOMIC ?? '100000') });
const paid = await fetch(url, { headers: { 'X-PAYMENT': header } });
const receipt = paid.headers.get('x-payment-response');
process.stdout.write(`status ${paid.status}\n${await paid.text()}\n`);
if (receipt) process.stdout.write(`settlement ${JSON.stringify(decodeHeader(receipt))}\n`);

agentgate-source: code mirror on Cloudflare Pages. All repos