const apiKey = 'your_api_key';
// 1. Reserve a voucher using the context code the payer shared with you
const reserveRes = await fetch('/api/v1/vouchers/reserve-voucher', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey
},
body: JSON.stringify({ code: contextCode })
});
const voucher = await reserveRes.json();
// 2. Redeem with expected payment
await fetch(`/api/v1/vouchers/${voucher.id}/redeem-voucher`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey
},
body: JSON.stringify({
expectedPayment: {
network: 'polygon',
currency: 'EURC',
currencyAddress: '0x73b3db5a96a4b9d9bcfc22b8f1b3d85a5e5b5e5b',
amount: '100',
wallet: { address: '0xYourWalletAddress' }
}
})
});
// 3. Poll until resolved
let status;
while (status !== 'RESOLVED') {
const res = await fetch(`/api/v1/vouchers/${voucher.id}`, {
headers: { 'x-api-key': apiKey }
});
const data = await res.json();
status = data.status;
if (status === 'RESOLVED') break;
await new Promise(r => setTimeout(r, 3000));
}