Quick Start Examples
Code examples for accepting x402 payments with Aether402 in various programming languages.
Quick Start
Verify an x402 Payment
The simplest way to accept a payment:
const response = await fetch('https://api.aether402.dev/v1/payments/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.API_KEY}`
},
body: JSON.stringify({
payment_proof: 'BASE64_ENCODED_PAYMENT_PROOF',
amount: 1000000,
recipient: 'YOUR_WALLET_ADDRESS'
})
});
const data = await response.json();
if (data.verified) {
console.log('Payment verified! Transaction:', data.transaction_id);
} else {
console.log('Payment invalid');
}import requests
import os
response = requests.post(
'https://api.aether402.dev/v1/payments/verify',
headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("API_KEY")}'
},
json={
'payment_proof': 'BASE64_ENCODED_PAYMENT_PROOF',
'amount': 1000000,
'recipient': 'YOUR_WALLET_ADDRESS'
}
)
data = response.json()
if data['verified']:
print(f'Payment verified! Transaction: {data["transaction_id"]}')
else:
print('Payment invalid')curl -X POST https://api.aether402.dev/v1/payments/verify \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"payment_proof": "BASE64_ENCODED_PAYMENT_PROOF",
"amount": 1000000,
"recipient": "YOUR_WALLET_ADDRESS"
}'Language-Specific Guides
JavaScript/TypeScript - Node.js and browser examples
Python - Sync and async examples
Rust - Reqwest and tokio examples
Integration Patterns
Protect an API Endpoint
Add x402 payments to any API endpoint:
app.get('/api/data', async (req, res) => {
const paymentProof = req.headers['x-payment-proof'];
if (!paymentProof) {
return res.status(402).json({
error: 'Payment Required',
amount: 1000000,
recipient: 'YOUR_WALLET_ADDRESS'
});
}
// Verify payment with Aether402
const verified = await verifyPayment(paymentProof, 1000000);
if (verified) {
return res.json({ data: 'Your protected content' });
}
return res.status(402).json({ error: 'Invalid payment' });
});Real-Time Payment Notifications
Get notified instantly when payments arrive:
// WebSocket connection for real-time updates
const ws = new WebSocket('wss://api.aether402.dev/v1/payments/stream');
ws.onmessage = (event) => {
const payment = JSON.parse(event.data);
console.log('Payment received:', payment.amount);
};Build a Paywall
Simple paywall for content:
async function requirePayment(contentId, amount) {
const paymentProof = getUserPaymentProof();
const response = await verifyPayment({
payment_proof: paymentProof,
amount: amount,
metadata: { contentId }
});
return response.verified;
}Best Practices
Keep API keys secret - Never expose them in frontend code
Validate amounts - Always check the payment amount matches what you expect
Handle errors gracefully - Network issues happen, handle them properly
Log everything - Keep logs for debugging and reconciliation
Start small - Test with small amounts before going live
Use WebSockets - Get real-time notifications when payments arrive
Next Steps
Pick your language and see detailed examples:
Last updated
