// Your API server receives a 402 Payment Required request
app.get('/api/protected-resource', async (req, res) => {
const paymentProof = req.headers['x-payment-proof'];
if (!paymentProof) {
// No payment provided, return 402
return res.status(402).json({
error: 'Payment Required',
amount: 1000000,
recipient: 'YOUR_WALLET_ADDRESS',
network: 'solana'
});
}
try {
// Verify the payment with Oura402
const verification = await fetch('https://api.oura402.dev/v1/payments/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
payment_proof: paymentProof,
amount: 1000000,
recipient: 'YOUR_WALLET_ADDRESS',
network: 'solana'
})
});
const result = await verification.json();
if (result.verified) {
// Settle the payment
await fetch('https://api.oura402.dev/v1/payments/settle', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
payment_id: result.payment_id
})
});
// Payment verified and settled, return the resource
return res.json({ data: 'Your protected resource' });
} else {
return res.status(402).json({ error: 'Invalid payment' });
}
} catch (error) {
return res.status(500).json({ error: 'Payment verification failed' });
}
});