Overview
x402x-fetch extends native fetch. When encountering HTTP 402 (Payment Required), it automatically parses the server’s payment requirements, creates a signature (Permit / EIP-3009 / Permit2), and retries the original request with the X-Payment header.
Key features
- Seamless integration: compatible with native fetch API
- Multiple authorization types: EIP-3009 / EIP-2612 Permit / Permit2
- Multi-chain support: common EVM chains built-in, custom chains supported
- Safety: max amount cap, signature validation
Install and dependencies
Quick start
One-liner wrapper
Full example
Parameters
walletClient: to signmaxValue: maximum allowed payment amount (highly recommended)paymentRequirementsSelector: custom selection for multi-option resources
Workflow
Typical use cases
- Access paid APIs/content
- Consistent UX across environments (Browser / Node)
API reference (condensed)
paymentType: eip3009 | permit | permit2
Multi-chain support
You can create a signer by chain name / viem Chain / custom config:Usage examples
GET
POST (with body)
Custom headers
Error handling and amount protection
Read payment response header
Best practices
- Set a reasonable
maxValuein production - Centralize logging and error classification for debugging
- In browsers, use a wallet connection (don’t expose private keys)
Resources
- Source:
https://github.com/WTFLabs-WTF/x402x/tree/main/typescript/packages/x402x-fetch - Issues:
https://github.com/WTFLabs-WTF/x402x/issues