@three-ws/x402-server
The merchant side of x402 — turn any HTTP endpoint into a paid one in a few lines. Issue the 402, price the work, verify and settle the payment, take your fee.
Install ·
Quick start ·
API ·
How it works ·
Payment ·
three.ws
@three-ws/x402-server is the seller half of x402: the middleware and
primitives that make an endpoint demand payment. Wrap a route with paid()
and it answers an unpaid request with a 402 Payment Required challenge —
listing what it accepts (asset · amount · network · pay-to) — then, on the
retry that carries an X-PAYMENT header, it verifies the payment, runs your
handler, settles on-chain, and returns the result with an X-PAYMENT-RESPONSE
receipt. It speaks the two lanes the three.ws rails already run in production:
Solana (facilitator-settled SPL transferChecked) and EVM / Base
(gasless EIP-3009
transferWithAuthorization). It is the server twin of the buyer-side
@three-ws/x402-fetch
and @three-ws/x402-modal —
they pay; this charges.
Why
x402 revives HTTP 402 Payment Required as a real payment rail: a server
answers a request with a 402 whose body lists its accepts[], the client
pays, and re-sends the request with an X-PAYMENT header. The buyer side is a
solved problem — drop in a fetch wrapper or the modal and it pays. The seller
side is where everyone reinvents the same machinery: build the challenge
envelope in the exact v2 shape, advertise the right asset/fee-payer per chain,
parse the X-PAYMENT header, call a facilitator's /verify, run the work
only after verification, settle only after the work succeeds, emit the
receipt, and skim a platform fee out of the price without double-charging the
buyer.
This package is that machinery, done once, the way the three.ws merchant rails
do it:
- One wrapper, a paid route.
paid({ price, asset, payTo }) emits the 402
and gates your handler behind a verified payment.
- Two lanes, one API. Solana and Base/EVM accepts come from the same config;
the challenge advertises both and the buyer picks.
- Settle after the work, never before. Verification gates the handler;
settlement runs after it returns
200. A failed call moves no funds, so a
retry can't double-charge.
- Fee without surprise-billing. A platform fee is split out of the listed
price — the buyer's total is never marked up, and the fee ships inert (rate
0, no treasury) until you turn it on.
This is the same flow that powers paid MCP tools and hosted checkout SKUs on
three.ws; the package is its standalone, embeddable home.
Install
npm install @three-ws/x402-server
Node 18+ (uses the global fetch and Web Crypto). Framework-agnostic: works as
Express/Connect middleware, a Fastify hook, or a bare (req, res) handler on
Vercel / Node http. For the buyer side of a test, pair with
@three-ws/x402-fetch.
Quick start
The one-liner — paid()
Wrap a handler. Unpaid requests get a 402; paid ones run the handler:
import { paid } from '@three-ws/x402-server';
export default paid(
{ price: '10000', asset: 'usdc', payTo: { solana: 'THREEsynthetic1111…' } },
async (req, res) => {
res.json({ summary: await summarize(req.body.text) });
},
);
price is in atomic units of the asset — '10000' is $0.01 of 6-decimal
USDC. The first unpaid GET/POST returns the challenge; the buyer pays and
re-sends with X-PAYMENT; your handler runs once, settlement lands, and the
response carries the on-chain receipt.
The Solana lane also needs the facilitator's sponsor account: set
X402_FEE_PAYER_SOLANA in the environment (or pass feePayer), or the
challenge throws missing_fee_payer. The EVM lane needs neither.