/** * Paid GET /x402/brands using @x402/fetch (v2.31.x) and @x402/evm. * * Start with the free sample (no key) if you only want to inspect the schema: * curl -sS https://api.aestheticdata.io/sample * * Never commit a private key. Export EVM_PRIVATE_KEY in your shell, and keep a * tight spend cap: this route costs 0.001 USDC per successful paid call. * * Network eip155:8453 is Base mainnet. */ import { wrapFetchWithPaymentFromConfig } from "@x402/fetch"; import { ExactEvmScheme } from "@x402/evm"; import { privateKeyToAccount } from "viem/accounts"; const SAMPLE_URL = "https://api.aestheticdata.io/sample"; const PAID_URL = "https://api.aestheticdata.io/x402/brands"; async function main(): Promise { const sample = await fetch(SAMPLE_URL); if (!sample.ok) { throw new Error(`Free sample failed: HTTP ${sample.status}`); } console.log("Free sample HTTP", sample.status); const key = process.env.EVM_PRIVATE_KEY as `0x${string}` | undefined; if (!key) { console.log("EVM_PRIVATE_KEY is not set; stopping before any paid request."); console.log("An unpaid GET to the paid route returns HTTP 402 with PAYMENT-REQUIRED."); const unpaid = await fetch(PAID_URL); console.log("Unpaid brands HTTP", unpaid.status); return; } const account = privateKeyToAccount(key); const fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, { schemes: [ { network: "eip155:8453", client: new ExactEvmScheme(account), }, ], }); const paid = await fetchWithPayment(PAID_URL, { method: "GET" }); const body = await paid.json(); console.log("Paid brands HTTP", paid.status); if (paid.headers.get("PAYMENT-RESPONSE")) { console.log("PAYMENT-RESPONSE header is present."); } console.log("Top-level keys:", Object.keys(body)); } main().catch((err) => { console.error(err); process.exitCode = 1; });