Payments
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 wrapFetchWithPayment
The wrapFetchWithPayment function wraps the native fetch API to automatically handle 402 Payment Required responses.
fetch- The fetch function to wrap (typicallyglobalThis.fetch)client- The thirdweb client used to access RPC infrastructurewallet- The wallet used to sign payment messagesmaxValue- (Optional) The maximum allowed payment amount in base units (defaults to 1 USDC = 1,000,000)
For full API documentation, see the TypeScript Reference.