Server Library
@transx402/server — merchant backend SDK for canonical x402 settlement (402 responses, processGate, config proxy).
Overview
@transx402/server is a zero-dependency Node package for merchant backends. Use it with @transx402/client in settlement: "server" mode (the default for fetch()).
Canonical flow:
- Client hits your API → return 402 from
payments.processGate/payments.buildRequired - Client wallet signs → retries with
PAYMENT-SIGNATURE - Your Route Handler calls
tx402.payments.processGate→POST /facilitatewith serverX-API-Key - On success, return paid content
TransX402 uses a combined POST /facilitate (verify + settle in one call). Keep ipk_sandbox_ / ipk_live_ keys on the server.
For paywall / static sites without a merchant settle endpoint, use client settlement: "direct" instead.
Requires Node.js 20.19+. There are no Express / Hono / Fastify middleware packages — pass Web Standard Request / Headers (or a Node http header record) yourself. Next.js App Router is the primary example path.
Install
Source: github.com/campinvestment/transx402-server
You must pass environment and/or facilitatorUrl to the constructor. The key family must match (ipk_sandbox_ ≠ environment: "base").
Quick start (Route Handler)
priceIdr is whole IDR (converted ×100 to IDRX base units). payTo must match the dashboard merchant wallet.
Browser / agent:
Config proxy (server settlement)
Browsers load chain params via GET /config. Without a merchant-domain CORS allowlist on the hosted facilitator, that cross-origin call fails. Expose a same-origin proxy and point the browser client at it.
Default proxy base path: /api/transx402 (client appends /config).
Your payment Route Handler still talks to the facilitator directly for 402 bodies and POST /facilitate. Only the browser uses the proxy.
API
Client
| Member | Purpose |
|---|---|
new TransX402(apiKey, options) | Configure once (environment and/or facilitatorUrl) |
tx402.payments.processGate | No header → 402; header → facilitate |
tx402.payments.facilitate | Decode header → POST /facilitate (throws FacilitationError) |
tx402.payments.verify | Post-settlement GET /payments/:txHash |
tx402.payments.buildRequired | Build x402 v2 402 JSON from facilitator /config |
tx402.config.handleRequest | Web Standard handler for merchant config proxy routes |
tx402.config.fetch | Upstream GET /config |
processGate result:
Free helpers
| Export | Purpose |
|---|---|
hasPaymentHeader / getPaymentHeader | Read PAYMENT-SIGNATURE / X-PAYMENT |
decodePaymentSignature | Base64 JSON decode of payment payload |
toIdrxBaseUnits | Whole IDR → IDRX base units (×100) |
detectApiKeyFamily | ipk_sandbox_ / ipk_live_ → section |
browserFacilitatorProxyBase / DEFAULT_FACILITATOR_CONFIG_PROXY_BASE | Same-origin base path ("/api/transx402") |
FACILITATOR_PRESETS | Named facilitator hosts |
FacilitationError | Error class from facilitate failures |
Prefer PAYMENT-SIGNATURE; X-PAYMENT is a fallback.
Migration from 0.2.x
These are no longer public: resolveServerConfig, processPaymentGate, facilitatePayment, verifyPayment, buildPaymentRequired, fetchFacilitatorConfig, handleFacilitatorConfigRequest.
Related
- JavaScript Library —
@transx402/client - API Reference —
GET /config,POST /facilitate,GET /payments/:txHash - Quickstart — copy-paste Next.js recipe