x402
Client Side
Make requests to any x402-compatible backend and automatically handle payment flows when APIs return a 402 Payment Required response.
The client library wraps the native fetch API and handles:
- Initial request to the API
- Detection of
402 Payment Requiredresponses - Parsing payment requirements from the response
- Creating and signing payment authorization
- Retrying the request with payment credentials
Using useFetchWithPayment
The useFetchWithPayment hook is a React-specific wrapper that automatically handles 402 Payment Required responses with built-in UI for payment errors, insufficient funds, and wallet connection.
- Automatic Payment Handling: Detects 402 responses and creates payment headers automatically
- Wallet Connection: Prompts users to connect their wallet if not connected
- Funding: Integrates BuyWidget to help users top up their wallet directly when needed
- Built-in error handling UI: Shows error modals with retry and fund wallet options
- Response Parsing: Automatically parses responses as JSON by default
client- The thirdweb client used to access RPC infrastructureoptions- (Optional) Configuration object:maxValue- Maximum allowed payment amount in base unitsparseAs- How to parse the response: "json" (default), "text", or "raw"theme- Theme for the payment error modal: "dark" (default) or "light"uiEnabled- Whether to show the UI for connection, funding or payment retries (defaults to true)fundWalletOptions- Customize the BuyWidget for topping upconnectOptions- Customize the ConnectModal for wallet connection
For full API documentation, see the TypeScript Reference.