Skip to content

Releases: thirdweb-dev/js

[email protected]

26 Nov 22:09
fda2284

Choose a tag to compare

Patch Changes

[email protected]

26 Nov 01:11
3ac8cba

Choose a tag to compare

Minor Changes

  • #8457 35aaf24 Thanks @joaquim-verges! - Add "upto" payment scheme option for x402 verify and settle

    const paymentArgs = {
      resourceUrl: "https://api.example.com/premium-content",
      method: "GET",
      paymentData,
      payTo: "0x1234567890123456789012345678901234567890",
      network: arbitrum,
      scheme: "upto", // enables dynamic pricing
      price: "$0.10", // max payable amount
      facilitator: thirdwebFacilitator,
    };
    
    // First verify the payment is valid for the max amount
    const verifyResult = await verifyPayment(paymentArgs);
    
    if (verifyResult.status !== 200) {
      return Response.json(verifyResult.responseBody, {
        status: verifyResult.status,
        headers: verifyResult.responseHeaders,
      });
    }
    
    // Do the expensive work that requires payment
    const { tokensUsed } = await doExpensiveWork();
    const pricePerTokenUsed = 0.00001;
    
    // Now settle the payment based on actual usage
    const settleResult = await settlePayment({
      ...paymentArgs,
      price: tokensUsed * pricePerTokenUsed, // adjust final price based on usage
    });

@thirdweb-dev/[email protected]

26 Nov 22:09
fda2284

Choose a tag to compare

@thirdweb-dev/[email protected]

26 Nov 01:11
3ac8cba

Choose a tag to compare

@thirdweb-dev/[email protected]

26 Nov 22:09
fda2284

Choose a tag to compare

@thirdweb-dev/[email protected]

26 Nov 01:11
3ac8cba

Choose a tag to compare

Patch Changes

[email protected]

24 Nov 23:24
ad2d175

Choose a tag to compare

Minor Changes

  • #8444 9809d5c Thanks @joaquim-verges! - # 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.

    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:

    // 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

    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

    const { fetchWithPayment } = useFetchWithPayment(client, {
      fundWalletOptions: {
        title: "Add Funds",
        description: "You need more tokens to complete this payment",
        buttonLabel: "Get Tokens",
      },
    });

    Customize Connect Modal

    const { fetchWithPayment } = useFetchWithPayment(client, {
      connectOptions: {
        wallets: [inAppWallet(), createWallet("io.metamask")],
        title: "Sign in to continue",
        showThirdwebBranding: false,
      },
    });

    Disable Modal (Handle Errors Manually)

    const { fetchWithPayment, error } = useFetchWithPayment(client, {
      showErrorModal: false,
    });
    
    // Handle the error manually
    if (error) {
      console.error("Payment failed:", error);
    }

Patch Changes

@thirdweb-dev/[email protected]

24 Nov 23:24
ad2d175

Choose a tag to compare

@thirdweb-dev/[email protected]

24 Nov 23:24
ad2d175

Choose a tag to compare

Patch Changes

[email protected]

20 Nov 02:05
a3c76d5

Choose a tag to compare

Patch Changes