// Usage in a Next.js API route
import { verifyPayment, facilitator } from "thirdweb/x402";
import { createThirdwebClient } from "thirdweb";
import { arbitrumSepolia } from "thirdweb/chains";
const client = createThirdwebClient({
secretKey: process.env.THIRDWEB_SECRET_KEY,
});
const thirdwebFacilitator = facilitator({
client,
serverWalletAddress: "0x1234567890123456789012345678901234567890",
});
export async function GET(request: Request) {
const paymentData = request.headers.get("x-payment");
const paymentArgs = {
resourceUrl: "https://api.example.com/premium-content",
method: "GET",
paymentData,
payTo: "0x1234567890123456789012345678901234567890",
network: arbitrumSepolia, // or any other chain
price: "$0.10", // or { amount: "100000", asset: { address: "0x...", decimals: 6 } }
facilitator: thirdwebFacilitator,
routeConfig: {
description: "Access to premium API content",
mimeType: "application/json",
},
};
// verify the payment
const result = await verifyPayment(paymentArgs);
if (result.status === 200) {
// Payment verified, but not settled yet
// you can do the work that requires payment first
const result = await doSomething();
// then settle the payment
const settleResult = await settlePayment(paymentArgs);
// then return the result
return Response.json(result);
} else {
// verification failed, return payment required
return Response.json(result.responseBody, {
status: result.status,
headers: result.responseHeaders,
});
}
}