Accept Payments
Protect your HTTP endpoints with x402 payments using middleware. The middleware enforces the x402 handshake, forwards payment requirements to a facilitator, inspects payment headers from clients, and handles settlement before allowing requests to proceed.Prerequisites
- Node.js and npm installed
- A SKALE Chain endpoint
- Understanding of x402 protocol
- A facilitator service (see Run a Facilitator)
Overview
When a client reaches a paywalled endpoint, the middleware:- Returns a
402 Payment Requiredresponse with payment requirements if the incoming request lacks a valid payment header - When a payment header is present, forwards it to the facilitator’s
/verifyand/settleendpoints - If settlement succeeds, the request continues; otherwise another 402 is returned
Environment Setup
Create a.env file with the following variables:
Implementation
- Coinbase SDK
- Faremeter SDK
Testing Your Server
Once your server is running, you can test it:402 Payment Required response with payment requirements that a client can use to make a payment.
Error Handling
- Invalid facilitator responses throw an exception (HTTP 500)
- When settlement fails, the middleware returns a 402 status with error details
- Missing configuration returns a 503 Service Unavailable
