import { wrapFetchWithPayment } from "@x402/fetch"; import { x402Client, x402HTTPClient } from "@x402/core/client"; import { ExactEvmScheme } from "@x402/evm/exact/client"; import { privateKeyToAccount } from "viem/accounts"; const privateKey = process.env.EVM_PRIVATE_KEY as `0x${string}`; if (!privateKey) { throw new Error("Set EVM_PRIVATE_KEY to a Base wallet funded with USDC"); } const signer = privateKeyToAccount(privateKey); const client = new x402Client(); client.register("eip155:*", new ExactEvmScheme(signer)); const fetchWithPayment = wrapFetchWithPayment(fetch, client); const httpClient = new x402HTTPClient(client); const response = await fetchWithPayment("https://api.fetch-sentry.online/fetch?url=https%3A%2F%2Fexample.com", { method: "GET" }); const data = await response.json(); console.log("Paid response:", JSON.stringify(data, null, 2)); if (response.ok) { const receipt = httpClient.getPaymentSettleResponse((name) => response.headers.get(name)); console.log("Payment settled:", receipt); }