#!/usr/bin/env node
// Pay PayGuard's GOAT Network Testnet3 x402 route and print the result + settlement tx.
// GOAT_PAYER_FILE=/path/with/private_key=0x... node scripts/goat-testnet3-pay.mjs [url]
// The payer only needs tUSDC (call faucet() or faucetTo(address) on 0x4aa7...Bc43); our facilitator pays the gas.
import { readFileSync } from 'node:fs';
import { x402Client, wrapFetchWithPayment, decodePaymentResponseHeader } from '@x402/fetch';
import { ExactEvmScheme } from '@x402/evm/exact/client';
import { toClientEvmSigner } from '@x402/evm';
import { privateKeyToAccount } from 'viem/accounts';
import { createPublicClient, http, defineChain } from 'viem';
const url = process.argv[2] || 'https://api.youraitools.co/payguard/goat-testnet3/v1/address?address=0x098B716B8Aaf21512996dC57EB0615e2383E2f96';
const NET = 'eip155:48816';
const TUSDC = process.env.GOAT_ASSET || '0x4aa7292d13801011C3A638CA2C16fFa85a06Bc43';
const pk = readFileSync(process.env.GOAT_PAYER_FILE, 'utf8').match(/^private_key=(0x[0-9a-fA-F]{64})$/m)[1];
const account = privateKeyToAccount(pk);
const chain = defineChain({ id: 48816, name: 'GOAT Testnet3', nativeCurrency: { name: 'Bitcoin', symbol: 'BTC', decimals: 18 }, rpcUrls: { default: { http: ['https://rpc.testnet3.goat.network'] } } });
const signer = toClientEvmSigner(account, createPublicClient({ chain, transport: http() }));
console.log('payer', account.address);
// tUSDC is not a default x402 asset, so opt in to exactly this asset with a 0.01 cap.
const client = x402Client.fromConfig({
schemes: [{ network: NET, client: new ExactEvmScheme(signer) }],
spendControls: { allowedAssets: [{ network: NET, asset: TUSDC, maxAmountPerPayment: '10000' }] },
});
const pay = wrapFetchWithPayment(fetch, client);
const t0 = Date.now();
const res = await pay(url, { headers: { accept: 'application/json' } });
const text = await res.text();
console.log('status', res.status, `${Date.now() - t0} ms`);
const pr = res.headers.get('payment-response');
if (pr) {
const s = decodePaymentResponseHeader(pr);
console.log('settlement', JSON.stringify(s));
if (s.transaction) console.log('explorer', `https://explorer.testnet3.goat.network/tx/${s.transaction}`);
}
const req = res.headers.get('payment-required');
if (res.status === 402 && req) { try { console.log('402 reason', JSON.parse(Buffer.from(req, 'base64').toString()).error); } catch {} }
console.log(text.slice(0, 1500));
process.exit(res.ok ? 0 : 1);
agentgate-source: code mirror on Cloudflare Pages. All repos