Installation
API Reference
PaymentPreference
Chain and token selection hooks.PaymentPreference.chain(preferredChains)
Prefer specific networks when multiple options are available.preferredChains- Array of network keys (string or string[])
ethereum- Ethereum Mainnetbase- Base Mainnetbase-sepolia- Base Sepolia Testnetskale-base- SKALE Baseskale-base-sepolia- SKALE Base Sepoliaethereum-sepolia- Ethereum Sepolia Testnet
PaymentPreference.token(preferredTokens)
Prefer specific tokens when multiple options are available.preferredTokens- Array of token symbols (string or string[])
PaymentPreference.cheapest()
Select the option with the lowest amount within the selected network and asset.Logger
Payment event logging.Logger.console(options?)
Log payment events to the console.| Option | Type | Default | Description |
|---|---|---|---|
prefix | string | "[x402]" | Log prefix |
enabled | boolean | true | Enable logging |
Utilities
combineHooks(…hooks)
Combine multiple hook arrays into a single array.Types
PaymentRequiredContext
Context passed when payment is required.PaymentPayloadContext
Context passed before signing payment.ClientHookErrorContext
Context passed when a hook errors.ClientHook
Hook interface.Usage
Selection Behavior
- Hook selection narrows progressively in array order
- Chain preference runs first, then token preference can narrow within selected chain
- Cheapest refines within the selected network and asset
- If no hook selects, clients fall back to the first compatible
accepts[]option fromPAYMENT-REQUIRED
Notes
- This package is optional. Core clients work without it.
- Hooks are fail-closed by default: if a hook throws, payment flow throws.