Server Path
Lesson 3 of 8
12 min

Verifying and Settling Payments

Learning Objectives

  • Understand two-step payment (verification + settlement)
  • Learn how withX402() handles verification
  • Access payment details in your handler
  • Log transactions for accounting

Two-Step Payment

x402 payments happen in two steps:

Step 1: Verification (off-chain, instant)

Check that the signature is valid
Ensure payment details match requirements

Step 2: Settlement (on-chain, takes a few seconds)

Submit transaction to blockchain
Wait for confirmation
Return proof to client
typescript
// With paymentMiddleware, verification happens automatically
app.use(
  paymentMiddleware(
    {
      'GET /api/data': {
        accepts: [{
          scheme: 'exact',
          price: '$0.001',
          network: 'eip155:8453',
          payTo: process.env.RECEIVING_WALLET_ADDRESS!,
        }],
        description: 'Data endpoint',
        mimeType: 'application/json',
      },
    },
    server
  )
);

app.get('/api/data', (req, res) => {
  // Access payment info from headers (set by middleware)
  const paymentHeader = req.headers['x-payment'];
  if (paymentHeader) {
    const payment = JSON.parse(Buffer.from(paymentHeader, 'base64').toString());
    console.log('Received payment from:', payment.from);
    console.log('Amount:', payment.amount);
  }

  res.json({ data: 'your data' });
});
The response will include an X-PAYMENT-RESPONSE header with the transaction hash after settlement completes.

Interactive Playground

typescript
Output

Click "Run Code" to see the output