@dexterai/x402
@dexterai/x402 is Dexter's full-stack x402 SDK.
It is the package you use when you want to:
- protect an API endpoint with x402 payments
- make x402 payments from Node.js scripts (Solana or EVM)
- make x402 payments from browser apps
- add payment-aware React UI flows
- use Access Pass instead of signing every request
- implement dynamic or token-based pricing
- receive and display sponsored recommendations (Ads for Agents)
When To Use It
Use @dexterai/x402 when you are building:
- a paid API
- a wallet-connected browser app
- a React application that needs payment state
- a client that should handle
402 Payment Requiredautomatically - an endpoint that needs dynamic or usage-based pricing
- a publisher that wants to earn from sponsored recommendations
Do not use this package when your main goal is:
- OpenClaw-native agent payments: use
@dexterai/clawdexter - local stdio discovery/payment for editor or CLI agents: use
@dexterai/opendexter
Install
Optional peer dependencies (install only what you need):
What It Exports
The published package exports five entry points:
@dexterai/x402/client@dexterai/x402/server@dexterai/x402/react@dexterai/x402/adapters@dexterai/x402/utils
Which Surface To Use
client
Use @dexterai/x402/client when you need payment-aware fetch behavior.
| Export | Purpose |
|---|---|
wrapFetch | One-liner for Node.js scripts — supports walletPrivateKey (Solana) and evmPrivateKey (EVM) |
createX402Client | Full client for browser or wallet-connected environments |
createKeypairWallet | Create a Solana wallet from a private key |
createEvmKeypairWallet | Create an EVM wallet from a private key (requires viem) |
getPaymentReceipt | Extract typed payment receipt from a response |
getSponsoredRecommendations | Extract sponsored recommendations from a payment response |
getSponsoredAccessInfo | Extract full sponsored-access extension data |
fireImpressionBeacon | Confirm recommendation delivery to the ad network |
KEYPAIR_SYMBOL | Symbol key for safe access to the underlying Solana Keypair |
X402Error | Typed error class with machine-readable error codes |
server
Use @dexterai/x402/server when you are protecting endpoints or building seller infrastructure.
| Export | Purpose |
|---|---|
x402Middleware | One-liner Express middleware with onSettlement and onVerifyFailed callbacks |
x402AccessPass | Time-limited access pass middleware |
createX402Server | Low-level server for manual 402 flows |
createDynamicPricing | Price by payload size with HMAC-signed quotes |
createTokenPricing | Price by token count (async, requires tiktoken) |
stripePayTo | Stripe machine payments provider (Base only) |
x402BrowserSupport | HTML paywall for browser 402 responses |
escapeHtml | HTML escape function for rendering payment data safely |
FacilitatorClient | Direct facilitator API client with retry and caching |
SponsoredRecommendation | Typed recommendation interface (re-exported from @dexterai/x402-ads-types) |
SPONSORED_ACCESS_EXTENSION_KEY | Extension key constant ("sponsored-access") |
react
Use @dexterai/x402/react when you want hook-based payment state in a React app.
| Export | Purpose |
|---|---|
useX402Payment | Main hook — returns sponsoredRecommendations, balances, transaction state |
useAccessPass | Dedicated hook for access pass lifecycle (tier discovery, purchase, caching) |
getSponsoredRecommendations | Also available from react entry point |
fireImpressionBeacon | Also available from react entry point |
adapters
Use @dexterai/x402/adapters for lower-level chain adapter control.
createSolanaAdapter/createEvmAdapterisKnownUSDC— check if an asset address is a known USDC contract (any chain)USDC_ADDRESSES— registry of USDC addresses per EVM chain- Chain constants:
BASE_MAINNET,POLYGON,ARBITRUM_ONE,OPTIMISM,AVALANCHE,BSC_MAINNET,SKALE_BASE
utils
Use @dexterai/x402/utils for amount conversion and network detection.
toAtomicUnits/fromAtomicUnits— convert between human-readable and atomic amountsisSolanaNetwork/isEvmNetwork— check network type from CAIP-2 identifiersgetChainFamily/getChainName/getExplorerUrl— network utilitiesencodeBase64Json/decodeBase64Json— unicode-safe base64 encoding
Minimal Paths
Fastest buyer path: Node.js (Solana)
Fastest buyer path: Node.js (EVM)
Fastest browser path
Fastest seller path
Supported Networks
The SDK supports all networks available on the Dexter facilitator:
- Solana Mainnet
- Base Mainnet
- Polygon
- Arbitrum
- Optimism
- Avalanche
- BNB Smart Chain
- SKALE
For exact current chain support and facilitator behavior, see Supported Networks.
What The SDK Covers
- automatic 402 handling with pre-payment inspection (
onPaymentRequired) - client fetch wrappers (Solana + EVM)
- browser wallet flows
- React hooks with sponsored recommendation delivery
- Express middleware with settlement/verification callbacks
- Access Pass (JWT-based time-limited access)
- dynamic pricing (HMAC-signed, time-bounded quotes)
- token pricing (async, tiktoken-based)
- Stripe machine payments (Base)
- sponsored access (Ads for Agents)
Common Failure Modes
Wrong wallet context
The client needs a compatible wallet for a chain the endpoint accepts. Use isSolanaNetwork() or isEvmNetwork() to check.
Missing funds
The payment flow cannot complete if the configured wallet is not funded with USDC on the accepted chain. Balance checks now throw on RPC errors instead of silently returning zero — the client skips the pre-check and lets the chain reject if the RPC is unavailable.
Choosing the wrong surface
| Goal | Use |
|---|---|
| Node.js script | wrapFetch |
| Browser app | createX402Client |
| React UI | useX402Payment |
| Protected endpoint | x402Middleware |
| Repeated reads | x402AccessPass |
| Budget controls | onPaymentRequired callback |