Skip to main content

Express.js

Install

For Solana support, also install @x402/svm:

Minimal example (EVM)

Minimal example (Solana)

Configuration

The paymentMiddleware takes a route map and a resource server:
Routes map "METHOD /path" to payment requirements:
Network identifiers use CAIP-2 format:
  • eip155:8453 — Base mainnet
  • eip155:84532 — Base Sepolia (testnet)
  • solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp — Solana mainnet
  • solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1 — Solana Devnet

Facilitator URL

Any framework

If you’re not using Express, you can implement the 402 flow directly. The protocol is framework-agnostic.

What your server does

  1. Check for payment: Look for the X-PAYMENT header on incoming requests
  2. No payment? Return 402 with payment requirements in the response body
  3. Has payment? Forward it to the facilitator’s /settle endpoint for on-chain settlement
  4. Settlement succeeds? Return the requested resource

402 response format

When a request arrives without payment, respond with:
maxAmountRequired is in USDC atomic units (6 decimals). "100000" = 0.10 USDC.

Settle request

Forward the client’s X-PAYMENT header to the facilitator:

Settle response