import { ethers } from 'ethers';
import {
signX402Payment,
decodePaymentRequirements,
selectPaymentOption,
} from '@prudra/payments';
const agentWallet = new ethers.Wallet(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
// ↑ test-only private key — never use in production
);
// Step 1: Hit endpoint, get 402
const r1 = await fetch('http://localhost:4001/summarise', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Summarise this' }),
});
// r1.status === 402
// Step 2: Decode challenge and sign
const requirements = decodePaymentRequirements(r1.headers.get('payment-required')!);
const option = selectPaymentOption(requirements, 'USDC');
const { xPaymentHeader } = await signX402Payment(agentWallet, option);
// Step 3: Retry with signature
const r2 = await fetch('http://localhost:4001/summarise', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'PAYMENT-SIGNATURE': xPaymentHeader,
},
body: JSON.stringify({ text: 'Summarise this' }),
});
// r2.status === 200
const result = await r2.json();
console.log('Vault ID:', result.vaultId);
console.log('Result:', result);