-
Notifications
You must be signed in to change notification settings - Fork 629
[SDK] Add useFetchWithPayment React hook for x402 payment handling #8444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joaquim-verges
merged 1 commit into
main
from
_SDK_Add_useFetchWithPayment_React_hook_for_x402_payment_handling
Nov 24, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| "thirdweb": minor | ||
| --- | ||
|
|
||
| # New `useFetchWithPayment()` React Hook | ||
|
|
||
| Added a new React hook that wraps the native fetch API to automatically handle 402 Payment Required responses using the x402 payment protocol. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Automatic Payment Handling**: Automatically detects 402 responses, creates payment headers, and retries requests | ||
| - **Built-in UI**: Shows an error modal with retry and fund wallet options when payment fails | ||
| - **Sign In Flow**: Prompts users to connect their wallet if not connected, then automatically retries the payment | ||
| - **Insufficient Funds Flow**: Integrates BuyWidget to help users top up their wallet directly in the modal | ||
| - **Customizable**: Supports theming, custom payment selectors, BuyWidget customization, and ConnectModal customization | ||
| - **Opt-out Modal**: Can disable the modal to handle errors manually | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| The hook automatically parses JSON responses by default. | ||
|
|
||
| ```tsx | ||
| import { useFetchWithPayment } from "thirdweb/react"; | ||
| import { createThirdwebClient } from "thirdweb"; | ||
|
|
||
| const client = createThirdwebClient({ clientId: "your-client-id" }); | ||
|
|
||
| function MyComponent() { | ||
| const { fetchWithPayment, isPending } = useFetchWithPayment(client); | ||
|
|
||
| const handleApiCall = async () => { | ||
| // Response is automatically parsed as JSON by default | ||
| const data = await fetchWithPayment( | ||
| "https://api.example.com/paid-endpoint" | ||
| ); | ||
| console.log(data); | ||
| }; | ||
|
|
||
| return ( | ||
| <button onClick={handleApiCall} disabled={isPending}> | ||
| {isPending ? "Loading..." : "Make Paid API Call"} | ||
| </button> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Customize Response Parsing | ||
|
|
||
| By default, responses are parsed as JSON. You can customize this with the `parseAs` option: | ||
|
|
||
| ```tsx | ||
| // Parse as text instead of JSON | ||
| const { fetchWithPayment } = useFetchWithPayment(client, { | ||
| parseAs: "text", | ||
| }); | ||
|
|
||
| // Or get the raw Response object | ||
| const { fetchWithPayment } = useFetchWithPayment(client, { | ||
| parseAs: "raw", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Customize Theme & Payment Options | ||
|
|
||
| ```tsx | ||
| const { fetchWithPayment } = useFetchWithPayment(client, { | ||
| maxValue: 5000000n, // 5 USDC in base units | ||
| theme: "light", | ||
| paymentRequirementsSelector: (requirements) => { | ||
| // Custom logic to select preferred payment method | ||
| return requirements[0]; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Customize Fund Wallet Widget | ||
|
|
||
| ```tsx | ||
| const { fetchWithPayment } = useFetchWithPayment(client, { | ||
| fundWalletOptions: { | ||
| title: "Add Funds", | ||
| description: "You need more tokens to complete this payment", | ||
| buttonLabel: "Get Tokens", | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Customize Connect Modal | ||
|
|
||
| ```tsx | ||
| const { fetchWithPayment } = useFetchWithPayment(client, { | ||
| connectOptions: { | ||
| wallets: [inAppWallet(), createWallet("io.metamask")], | ||
| title: "Sign in to continue", | ||
| showThirdwebBranding: false, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Disable Modal (Handle Errors Manually) | ||
|
|
||
| ```tsx | ||
| const { fetchWithPayment, error } = useFetchWithPayment(client, { | ||
| showErrorModal: false, | ||
| }); | ||
|
|
||
| // Handle the error manually | ||
| if (error) { | ||
| console.error("Payment failed:", error); | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify the API endpoint path change.
The endpoint path changed from
/v1/contracts/{chainId}/{address}/writeto/v1/contracts/write, removing the path parameters. Please confirm this reflects the actual API structure, as it's a significant change to the documented endpoint.🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 1042
🏁 Script executed:
Length of output: 414
🏁 Script executed:
Length of output: 1611