Skip to main content
x402 middleware lets you charge per API request. When a request arrives without valid payment, the middleware returns 402 Payment Required with payment details. The client pays and retries — all transparent to your application code.

Prerequisites

  • @arcenpay/node installed
  • An active ArcenPay API key
  • For production: a Redis-compatible nonce store for replay protection

Basic setup

Production setup

For production, use a Redis-backed nonce store for reliable replay protection:
Use RedisNonceStore for multi-instance production deployments. For local development, omit the option — the middleware falls back to an in-memory store.

Middleware options

Payment flow

  1. Client sends a request to your protected endpoint.
  2. Middleware returns 402 Payment Required with payment details in the response body.
  3. Client’s agent SDK processes the payment on-chain.
  4. Client retries with payment signature headers.
  5. Middleware verifies the payment and forwards the request to your handler.
  6. Your handler receives req.x402.payment with the payment details.

Testing x402 locally with ArcenAgent

Set up a test endpoint and use the agent SDK to verify:
The agent automatically handles the 402 response, signs payment, and retries transparently.

Next steps

Last modified on October 4, 2026