|
| 1 | +import { Tabs, TabsList, TabsTrigger, TabsContent, OpenApiEndpoint } from "@doc"; |
| 2 | +import { TypeScriptIcon, EngineIcon } from "@/icons"; |
| 3 | + |
| 4 | +# Client Side |
| 5 | + |
| 6 | +Make requests to any x402-compatible backend by automatically handling payment flows when APIs return a `402 Payment Required` response. |
| 7 | + |
| 8 | +The client library wraps the native `fetch` API and handles: |
| 9 | +1. Initial request to the API |
| 10 | +2. Detection of `402 Payment Required` responses |
| 11 | +3. Parsing payment requirements from the response |
| 12 | +4. Creating and signing payment authorization |
| 13 | +5. Retrying the request with payment credentials |
| 14 | + |
| 15 | +<Tabs defaultValue="typescript"> |
| 16 | + <TabsList> |
| 17 | + <TabsTrigger value="typescript" className="flex items-center [&>p]:mb-0"> |
| 18 | + <TypeScriptIcon className="size-4 mr-1.5" /> |
| 19 | + TypeScript |
| 20 | + </TabsTrigger> |
| 21 | + <TabsTrigger value="http" className="flex items-center [&>p]:mb-0"> |
| 22 | + <EngineIcon className="size-4 mr-1.5" /> |
| 23 | + HTTP API |
| 24 | + </TabsTrigger> |
| 25 | + </TabsList> |
| 26 | + |
| 27 | + <TabsContent value="typescript"> |
| 28 | + ## Using `wrapFetchWithPayment` |
| 29 | + |
| 30 | + The `wrapFetchWithPayment` function wraps the native fetch API to automatically handle 402 Payment Required responses. |
| 31 | + |
| 32 | + ```typescript |
| 33 | + import { wrapFetchWithPayment } from "thirdweb/x402"; |
| 34 | + import { createThirdwebClient } from "thirdweb"; |
| 35 | + import { createWallet } from "thirdweb/wallets"; |
| 36 | + |
| 37 | + const client = createThirdwebClient({ clientId: "your-client-id" }); |
| 38 | + const wallet = createWallet("io.metamask"); // or any other wallet |
| 39 | + await wallet.connect({ client }) |
| 40 | + |
| 41 | + // Wrap fetch with payment handling |
| 42 | + // maxValue is the maximum payment amount in base units (defaults to 1 USDC = 1_000_000) |
| 43 | + const fetchWithPay = wrapFetchWithPayment( |
| 44 | + fetch, |
| 45 | + client, |
| 46 | + wallet, |
| 47 | + BigInt(1 * 10 ** 6) // max 1 USDC |
| 48 | + ); |
| 49 | + |
| 50 | + // Make a request that may require payment |
| 51 | + const response = await fetchWithPay('https://api.example.com/paid-endpoint'); |
| 52 | + const data = await response.json(); |
| 53 | + ``` |
| 54 | + |
| 55 | + ### Parameters |
| 56 | + |
| 57 | + - `fetch` - The fetch function to wrap (typically `globalThis.fetch`) |
| 58 | + - `client` - The thirdweb client used to access RPC infrastructure |
| 59 | + - `wallet` - The wallet used to sign payment messages |
| 60 | + - `maxValue` - (Optional) The maximum allowed payment amount in base units (defaults to 1 USDC = 1,000,000) |
| 61 | + |
| 62 | + ### Reference |
| 63 | + |
| 64 | + For full API documentation, see the [TypeScript Reference](/references/typescript/v5/x402/wrapFetchWithPayment). |
| 65 | + |
| 66 | + </TabsContent> |
| 67 | + |
| 68 | + <TabsContent value="http"> |
| 69 | + ## Prepare Payment |
| 70 | + |
| 71 | + Create a signed payment header from the authenticated user to include in your API request: |
| 72 | + |
| 73 | + ### Making the Paid Request |
| 74 | + |
| 75 | + After preparing the payment, include it in your API request: |
| 76 | + |
| 77 | + ```bash |
| 78 | + # First, make the initial request to get payment requirements |
| 79 | + curl -X GET https://api.example.com/paid-endpoint |
| 80 | + |
| 81 | + # Response will be 402 Payment Required with payment details |
| 82 | + # { |
| 83 | + # "x402Version": 1, |
| 84 | + # "accepts": [...payment requirements...], |
| 85 | + # "error": "Payment required" |
| 86 | + # } |
| 87 | + |
| 88 | + # Select one of the payment methods, and sign the payment authorization using the API |
| 89 | + curl -X POST https://api.thirdweb.com/v1/payments/x402/prepare \ |
| 90 | + -H "Content-Type: application/json" \ |
| 91 | + -H "Authorization: Bearer <user-access-token>" \ |
| 92 | + -d '{ |
| 93 | + "from": "0x1234...", |
| 94 | + "paymentRequirements": { ... } |
| 95 | + }' |
| 96 | + |
| 97 | + # Response will contain the signed payment header |
| 98 | + # { |
| 99 | + # "paymentPayload": { ... }, |
| 100 | + # "paymentHeader": "..." // base64 encoded payment header |
| 101 | + # } |
| 102 | + |
| 103 | + # Finally, make the request with the payment header |
| 104 | + curl -X GET https://api.example.com/paid-endpoint \ |
| 105 | + -H "X-PAYMENT: <base64-encoded-payment>" |
| 106 | + ``` |
| 107 | + |
| 108 | + ## API Reference |
| 109 | + |
| 110 | + <OpenApiEndpoint path="/v1/payments/x402/prepare" method="POST" /> |
| 111 | + |
| 112 | + </TabsContent> |
| 113 | +</Tabs> |
0 commit comments