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');
}

Language-Specific Guides

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

  1. Keep API keys secret - Never expose them in frontend code

  2. Validate amounts - Always check the payment amount matches what you expect

  3. Handle errors gracefully - Network issues happen, handle them properly

  4. Log everything - Keep logs for debugging and reconciliation

  5. Start small - Test with small amounts before going live

  6. Use WebSockets - Get real-time notifications when payments arrive

Next Steps

Pick your language and see detailed examples:

Last updated