Skip to main content

Overview

The x402 server components let you monetize your API endpoints. Clients pay with USDC per request, and payments are settled gaslessly through Chipi’s paymaster. Two main components:
  • X402Facilitator — Verifies payment signatures and settles via paymaster
  • x402Middleware — Express middleware that handles the full 402 flow (also compatible with Express-like frameworks such as Hono)

Express / Node.js

Quick Setup

Middleware Behavior

  1. No X-PAYMENT header → Returns 402 with PAYMENT-REQUIRED header
  2. Malformed header → Returns 400
  3. Invalid payment → Returns 402 with error reason
  4. Valid payment → Settles via paymaster, attaches req.x402, calls next()

Access Payment Info

After successful payment, req.x402 contains:

Verify-Only Mode

To verify payments without auto-settling (for custom settlement logic):

Custom Nonce Store

By default, the facilitator uses an in-memory nonce store (resets on server restart). For production, plug in a persistent store:

Redis Example

PostgreSQL Example

The x402_nonces table must enforce uniqueness on the nonce column:
For cleanup, run periodically: DELETE FROM x402_nonces WHERE created_at < NOW() - INTERVAL '24 hours'

Python — FastAPI

Python — Flask

Direct Facilitator Usage

For custom server frameworks or advanced use cases:

CORS Configuration

The middleware automatically skips OPTIONS (preflight) requests. Make sure your CORS setup includes the X-PAYMENT header: