JavaScript Library
Complete guide to @transx402/client — subpath imports, createBrowserClient, createPaywall, agents, and settlement modes for IDRX x402.
Overview
@transx402/client is a browser and Node.js library for IDRX x402 payments. Import clients from subpaths — the package root exports types and errors only.
For full-stack integrations (recommended), pair it with @transx402/server: the client signs; your backend calls POST /facilitate.
| Settlement mode | Who calls /facilitate | Default for | API key |
|---|---|---|---|
server (canonical) | Merchant backend (@transx402/server) | fetch() | Server env only — omit in the browser |
direct | Browser / agent | pay() / createPaywall() | Publishable ipk_pub_sandbox_ / ipk_pub_live_ (register Allowed Origins first) |
Installation
Requires Node.js 20.19+. For zero-build HTML, see CDN Usage. WordPress sites should use the WordPress Plugin, not this npm package, in the browser.
| Import path | Role |
|---|---|
@transx402/client | Types, FacilitationError, helpers — no createBrowserClient |
@transx402/client/browser | Path 4: createBrowserClient, createPaywall, CDN TransX402 |
@transx402/client/agent | Path 3: createAgentClient (Node, private key) |
Canonical: createBrowserClient + fetch()
TypeScript forbids apiKey when environment is set and settlement is "server". Wire the proxy with tx402.config.handleRequest.
You may set facilitatorUrl to override the preset host; the config section still follows environment. Chain / RPC / IDRX / Permit2 come only from GET /config.
client.fetch(url, init?)
Drop-in replacement for fetch(). Automatically handles 402 responses.
Internal flow (settlement: "server", default):
- Sends a normal
fetch()request - If response is 402, parses payment requirements
- Connects wallet (if not connected)
- Signs x402 payment payload (Exact EVM + Permit2 witness)
- Retries the original request with
PAYMENT-SIGNATURE/X-PAYMENT - Your merchant API settles via
@transx402/server→POST /facilitate - Returns the final response
Internal flow (settlement: "direct"): steps 1–4, then the client calls POST /facilitate itself before the retry. Used by pay() and the paywall.
Direct settlement: pay()
Zero-backend payments. Requires a publishable API key.
Paywall: createPaywall()
Imperative overlay — not a React component. Always uses direct settlement.
CDN equivalent: TransX402.paywall({ ... }) from the browser bundle.
The overlay includes price in IDR, Pay with IDRX, wallet connect, Permit2 approve, signature, and a transaction link.
Theme
Agent: createAgentClient
Path 3 — Node private key. Sponsored Permit2 approve via signTransaction. Do not use this in MetaMask (browsers cannot eth_signTransaction).
For settlement: "direct", pass a publishable apiKey. Relative configProxyPath works in the browser only.
Other browser methods
Configuration options
| Option | Type | Notes |
|---|---|---|
environment | "local" | "camp" | "base" | Selects config section + preset host |
facilitatorUrl | string | Optional host override; section still follows environment |
configProxyPath | string | Same-origin proxy base for GET …/config (server settlement). Default /api/transx402 |
settlement | "server" | "direct" | Default "server" for fetch() |
apiKey | string | Required for direct / pay() / paywall; omitted for server fetch() |
onPaymentStart | function | Payment started |
onPaymentSuccess | function | Payment succeeded |
onPaymentError | function | Payment failed |
onWalletConnect | function | Wallet connected |
onApprovalRequired | function | Optional manual requestApproval() hook |
Do not pass token or network — those are not client options. Config comes from GET /config.
Errors
Also: WalletConnectionError, Permit2Error. After server settlement, a failed gate may return HTTP 402/500 with { code, error, details } — rehydrate FacilitationError from the body if you need the same UX.
Compatibility
Browser
- Chrome 90+, Firefox 90+, Safari 15+, Edge 90+
- Mobile: Chrome Android, Safari iOS
- Wallet: MetaMask / EIP-1193 (
window.ethereum) only
Node.js
- Node.js 20.19+ (built-in
fetch) - Agents:
@transx402/client/agentwith a private key