How it works
Setup
Create a paying fetch
| Option | Type | Description |
|---|---|---|
privateKey | string | Hex private key for the payer wallet. Must hold USDC. |
network | string | 'base' (mainnet) or 'base-sepolia' (testnet) |
fetchFn | function | Optional custom fetch implementation to wrap |
Make requests
UsefetchWithPayment exactly like fetch. When the server returns 402, payment is handled transparently:
Testnet
Usebase-sepolia for development and testing:
Wrapping a custom fetch
If you have a custom fetch (e.g., with auth headers or logging), pass it asfetchFn:
Advanced re-exports
The x402 subpackage re-exports lower-level primitives for custom integrations:| Export | From | Description |
|---|---|---|
wrapFetchWithPayment | @x402/fetch | Lower-level fetch wrapper |
wrapFetchWithPaymentFromConfig | @x402/fetch | Config-driven fetch wrapper |
decodePaymentResponseHeader | @x402/fetch | Parse payment response headers |
ExactEvmScheme | @x402/evm | EVM payment scheme |
toClientEvmSigner | @x402/evm | Convert to client-side EVM signer |