React Hook for x402 Payments
Joaquim VergesWe just shipped the new useFetchWithPayment() React hook in thirdweb v5.113.0, making it easier than ever to integrate x402 payment flows into your React applications with built-in UI for wallet connection, funding, and payment error handling.
0:00
/0:38
The useFetchWithPayment() hook provides a complete payment experience out of the box:
- Automatic Payment Handling - Detects 402 responses and creates payment authorization headers automatically
- Wallet Connection Flow - Prompts users to connect their wallet with a beautiful modal when needed
- Wallet Funding Integration - Shows a BuyWidget to help users top up their wallet when they have insufficient funds
- Error Handling UI - Displays modals with retry options when payments fail
- Response Parsing - Automatically parses responses as JSON by default (configurable)
- Fully Customizable - Customize themes, wallet options, funding widget, and more
- React Query Integration - Built on `@tanstack/react-query` for state management
The hook is available for both web and React Native:
- Web: Full UI support with modals for connection, funding, and errors
- React Native: Core functionality without modal UI (coming soon)
- Documentation: x402 Client Documentation
- Playground: Try it live
- TypeScript Reference: API Reference
Install the thirdweb SDK if you haven't already:
npm install thirdwebThen import and use the hook in your React components as shown above.
Happy Building! 🛠️