clawpay-x402
v0.1.0
Published
ClawPay × x402 client + server SDK — China-localized payment schemes (clawpay-cny, clawpay-dcep, clawpay-cross) on top of the x402 HTTP 402 protocol.
Downloads
135
Maintainers
Readme
clawpay-x402 — JavaScript SDK
ClawPay × x402 client + server SDK for the
clawpay-cny/clawpay-dcep/clawpay-crossschemes. Implements the x402 HTTP 402 protocol with China-localized settlement.Spec: https://erc8004.cn/specs/clawpay-x402/ · License: CC0 · ESM only · Zero deps
Install
npm i clawpay-x402
# or pnpm add / yarn add / bun addClient — pay an x402 endpoint in 3 lines
import { x402Pay } from "clawpay-x402/client";
const r = await x402Pay("https://erc8004.cn/api/x402/demo", { from: "clawpay:my-agent" });
console.log(r.body, r.receipt);What x402Pay does (per spec):
GETthe URL.- If
200, returns the body (no payment needed). - If
402, parsesaccepts[], picks the first scheme matchingprefer(default order:clawpay-cny→clawpay-dcep→clawpay-cross→exact), builds aPaymentPayload, base64-encodes it asX-PAYMENT, retries. - Returns
{ status, body, paid, paymentRequirements, receipt, response }.
Real settlement integration — pass onBuildPayload to inject a real transactionId from your payment processor:
const r = await x402Pay(url, {
from: "clawpay:user_42",
onBuildPayload: async (req) => {
// call WeChat Pay / Alipay / DCEP API → get a real txn id
const txn = await wechatPay.charge({ amount: req.maxAmountRequired, payee: req.payTo });
return { transactionId: txn.id, rail: "wechat" };
},
});For the exact (USDC on Base) scheme, this SDK throws — use Coinbase's official x402 SDK or filter exact out via prefer.
Server — protect any endpoint in 1 line (express/connect/node http)
import express from "express";
import { x402Protect } from "clawpay-x402/server";
const app = express();
app.get("/api/translate",
x402Protect({
accepts: [
{ scheme: "clawpay-cny", network: "clawpay-mainnet",
maxAmountRequired: "150", asset: "CNY", payTo: "agent:31981",
extra: { facilitator: "https://erc8004.cn/api/x402/facilitator", humanPrice: "¥1.50" } },
],
verify: async (payload, req) => {
// call ClawPay facilitator /verify, or your own WeChat receipt lookup
return await myFacilitator.verify(payload);
},
}),
(req, res) => res.json({ output: "Hello, world", paidWith: req.x402.payload.scheme })
);The middleware:
- emits
402+accepts[]when noX-PAYMENTheader present - decodes & shape-validates the header (rejects malformed, wrong scheme, under-amount, stale
signedAt) - calls your
verifyhook for real settlement check - on success, sets
X-PAYMENT-RESPONSE(base64 receipt) and callsnext() - attaches
req.x402 = { payload, paymentRequirements }
For framework-agnostic use, the lower-level building blocks are exported too:
import { build402Body, decodePayment, verifyShape, encodeReceipt } from "clawpay-x402/server";See examples/server.mjs for a vanilla node:http integration.
Test
node --test test/ # unit tests (no network)
LIVE=1 node --test test/live.test.js # hits the real erc8004.cn demoWhat this SDK is and isn't
| ✅ | ❌ | |---|---| | HTTP layer: 402 ↔ X-PAYMENT ↔ X-PAYMENT-RESPONSE | Real WeChat/Alipay/DCEP settlement (call your processor) | | Shape & freshness validation | Real fraud check (delegate to facilitator) | | Spec-compliant PaymentPayload encoding | On-chain USDC signing (use Coinbase x402 SDK) | | Client + server in one tiny ESM package | Crypto custody (illegal for CN domestic entities anyway) |
Why CC0?
We want this scheme adopted, not licensed. Fork it, rebrand it, productize it. Tag us if you ship.
