JavaScript Library
Complete guide to @transx402/client — installation, configuration, paywall component, and API reference for IDRX payment integration.
Overview
@transx402/client is a browser and Node.js library for IDRX x402 payments. It wraps the standard x402 client libraries and adds IDRX-specific defaults for token/network.
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 |
direct | Browser / agent | pay() / paywall | Publishable ipk_pub_sandbox_ / ipk_pub_live_ (register allowed origins in Dashboard first) |
Installation
NPM (for bundled apps)
CDN (for WordPress, static sites, any HTML page)
For zero-build integrations, see the dedicated CDN Usage guide. The short version:
The CDN script exposes a global TransX402 object with the same API described below. CDN / paywall flows use direct settlement.
Initialization
TransX402.create(options)
Create a TransX402 client instance:
The chain (sandbox/production) is determined from your API key prefix — both use https://api.transx402.com. You don't need to set the facilitator URL manually for hosted integrations.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | required | API key from the TransX402 dashboard |
environment | string | — | "local" | "camp" | "base" (XOR with facilitatorUrl) |
facilitatorUrl | string | — | Custom facilitator URL (XOR with environment) |
settlement | string | "server" for fetch() | "server" | "direct" — who calls /facilitate |
onPaymentStart | function | — | Callback when payment starts |
onPaymentSuccess | function | — | Callback when payment succeeds |
onPaymentError | function | — | Callback when payment fails |
onWalletConnect | function | — | Callback when wallet connects |
onApprovalRequired | function | — | Optional callback for manual requestApproval() flows |
Core API
client.fetch(url, options)
Drop-in replacement for fetch(). Automatically handles 402 responses.
Internal flow (settlement: "server", default for fetch()):
- 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"):
1–4 as above, then the client calls POST /facilitate itself before the retry. Used by pay() and the paywall.
client.connectWallet()
Explicitly connect a wallet. Supports:
- MetaMask / browser extension wallets (EIP-1193)
- WalletConnect v2
- Coinbase Wallet
client.checkApproval() (optional)
Check if the connected wallet has approved Permit2 for IDRX.
client.requestApproval() (optional)
Manual helper to request Permit2 approval. Most integrations should rely on the standard x402 flow and sponsorship extensions instead.
client.pay(paymentRequirements)
Manually trigger a payment without wrapping fetch.
Paywall Component
A ready-made paywall overlay for content gating.
Vanilla JavaScript
React
Paywall Display
The default paywall overlay includes:
- Price display in IDR (e.g., "Rp 5,000")
- "Pay with IDRX" button
- Wallet connection flow (if not connected)
- Signature confirmation step
- Payment confirmation with transaction link
- Customizable via CSS variables
Customization
CSS Variables
Event Hooks
Multi-Currency Support
The library is designed for IDRX first but supports additional tokens:
Token configuration (addresses, decimals, methods) is fetched from the facilitator's /tokens endpoint, so adding new tokens requires no client library updates.
Bundle Size Targets
| Variant | Target |
|---|---|
| Core (fetch wrapper only) | < 15 KB gzipped |
| With paywall UI | < 30 KB gzipped |
| With wallet connectors | < 50 KB gzipped |
| CDN full bundle | < 60 KB gzipped |
Compatibility
Browser
- Chrome 90+
- Firefox 90+
- Safari 15+
- Edge 90+
- Mobile: Chrome Android, Safari iOS
Node.js
- Node.js 18+ (Fetch API required)
- Can be used server-side for programmatic payments (AI agents, backends)