|
| 1 | +import { CodeServer } from "@workspace/ui/components/code/code.server"; |
| 2 | +import { CircleDollarSignIcon, Code2Icon } from "lucide-react"; |
| 3 | +import { CodeExample, TabName } from "@/components/code/code-example"; |
| 4 | +import ThirdwebProvider from "@/components/thirdweb-provider"; |
| 5 | +import { PageLayout } from "../../../components/blocks/APIHeader"; |
| 6 | +import { createMetadata } from "../../../lib/metadata"; |
| 7 | +import { X402ClientPreview } from "./components/x402-client-preview"; |
| 8 | + |
| 9 | +const title = "x402 Payments"; |
| 10 | +const description = |
| 11 | + "Use the x402 payment protocol to pay for API calls using any web3 wallet."; |
| 12 | +const ogDescription = |
| 13 | + "Use the x402 payment protocol to pay for API calls using any web3 wallet."; |
| 14 | + |
| 15 | +export const metadata = createMetadata({ |
| 16 | + title, |
| 17 | + description: ogDescription, |
| 18 | + image: { |
| 19 | + icon: "payments", |
| 20 | + title, |
| 21 | + }, |
| 22 | +}); |
| 23 | + |
| 24 | +export default function Page() { |
| 25 | + return ( |
| 26 | + <ThirdwebProvider> |
| 27 | + <PageLayout |
| 28 | + icon={CircleDollarSignIcon} |
| 29 | + title={title} |
| 30 | + description={description} |
| 31 | + docsLink="https://portal.thirdweb.com/payments/x402?utm_source=playground" |
| 32 | + > |
| 33 | + <X402Example /> |
| 34 | + <div className="h-8" /> |
| 35 | + <ServerCodeExample /> |
| 36 | + </PageLayout> |
| 37 | + </ThirdwebProvider> |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +function ServerCodeExample() { |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <div className="mb-4"> |
| 45 | + <h2 className="font-semibold text-xl tracking-tight"> |
| 46 | + Next.js Server Code Example |
| 47 | + </h2> |
| 48 | + <p className="max-w-4xl text-muted-foreground text-balance text-sm md:text-base"> |
| 49 | + Use any x402 middleware + the thirdweb facilitator to settle |
| 50 | + transactions with our server wallet. |
| 51 | + </p> |
| 52 | + </div> |
| 53 | + <div className="overflow-hidden rounded-lg border bg-card"> |
| 54 | + <div className="flex grow flex-col border-b md:border-r md:border-b-0"> |
| 55 | + <TabName icon={Code2Icon} name="Server Code" /> |
| 56 | + <CodeServer |
| 57 | + className="h-full rounded-none border-none" |
| 58 | + code={`// src/middleware.ts |
| 59 | +
|
| 60 | +import { facilitator } from "thirdweb/x402"; |
| 61 | +import { createThirdwebClient } from "thirdweb"; |
| 62 | +import { paymentMiddleware } from "x402-next"; |
| 63 | +
|
| 64 | +const client = createThirdwebClient({ secretKey: "your-secret-key" }); |
| 65 | +
|
| 66 | +export const middleware = paymentMiddleware( |
| 67 | + "0xYourWalletAddress", |
| 68 | + { |
| 69 | + "/api/paid-endpoint": { |
| 70 | + price: "$0.01", |
| 71 | + network: "base-sepolia", |
| 72 | + config: { |
| 73 | + description: "Access to paid content", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + facilitator({ |
| 78 | + client, |
| 79 | + serverWalletAddress: "0xYourServerWalletAddress", |
| 80 | + }), |
| 81 | +); |
| 82 | +
|
| 83 | +// Configure which paths the middleware should run on |
| 84 | +export const config = { |
| 85 | + matcher: ["/api/paid-endpoint"], |
| 86 | +}; |
| 87 | +
|
| 88 | + `} |
| 89 | + lang="tsx" |
| 90 | + /> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +function X402Example() { |
| 98 | + return ( |
| 99 | + <CodeExample |
| 100 | + header={{ |
| 101 | + title: "Client Code Example", |
| 102 | + description: |
| 103 | + "Wrap your fetch requests with the `wrapFetchWithPayment` function to enable x402 payments.", |
| 104 | + }} |
| 105 | + code={`import { createThirdwebClient } from "thirdweb"; |
| 106 | +import { wrapFetchWithPayment } from "thirdweb/x402"; |
| 107 | +import { useActiveWallet } from "thirdweb/react"; |
| 108 | +
|
| 109 | +const client = createThirdwebClient({ clientId: "your-client-id" }); |
| 110 | +
|
| 111 | +export default function Page() { |
| 112 | + const wallet = useActiveWallet(); |
| 113 | +
|
| 114 | + const onClick = async () => { |
| 115 | + const fetchWithPay = wrapFetchWithPayment(fetch, client, wallet); |
| 116 | + const response = await fetchWithPay('/api/paid-endpoint'); |
| 117 | + } |
| 118 | +
|
| 119 | + return ( |
| 120 | + <Button onClick={onClick}>Pay Now</Button> |
| 121 | + ); |
| 122 | +}`} |
| 123 | + lang="tsx" |
| 124 | + preview={<X402ClientPreview />} |
| 125 | + /> |
| 126 | + ); |
| 127 | +} |
0 commit comments