gedx402 · docs

Web research agent in 30 lines

Search Google, read the top two pages, and get a cited answer. Your agent pays each call in USDC on Base with x402: no API keys, no account, no subscription.

Each run makes 4 paid calls, priced per request:

StepRoutePrice
1. Google results (Serper)POST https://search.gedx402.com/v1/serper/searchflat, see hero
2–3. Page text, one URL per callPOST https://search.gedx402.com/v1/exa/contents-liteflat per page
4. Cited answerPOST https://chat.gedx402.com/v1/chat/completionspriced from the body (input size + max_tokens)

Live per-call prices are in llms.txt and GET /v1/models. Every route also quotes for free: an unpaid POST returns 402 with a payment-required header holding the exact amount for that body. @x402/fetch pays that amount and retries.

Prefer Exa neural search? Swap step 1 for POST https://search.gedx402.com/v1/exa/search with { query, num_results } and read results[].url (hero). It's priced the same as the Google route.

Run it

npm i @x402/fetch @x402/core @x402/evm viem
PRIVATE_KEY=0x... node web-research-agent.mjs "What is the x402 payment protocol?"

PRIVATE_KEY is a Base wallet holding a little USDC. Use a dedicated low-balance wallet; one dollar covers dozens of runs.

The code

// Web research agent in 30 lines: Google SERP -> read top pages -> cited answer, paid per call in USDC on Base (x402).
// npm i @x402/fetch @x402/core @x402/evm viem   then   PRIVATE_KEY=0x... node web-research-agent.mjs "your question"
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(privateKeyToAccount(process.env.PRIVATE_KEY)));
const pay = wrapFetchWithPayment(fetch, client); // answers each 402 with a signed USDC payment, then retries
const post = async (url, body) => {
  const res = await pay(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
  if (!res.ok) throw new Error(`${url} -> HTTP ${res.status}: ${await res.text()}`);
  return res.json();
};

const question = process.argv[2] ?? "What is the x402 payment protocol?";
// 1. Search Google (Serper). Or Exa neural search, same price: post(".../v1/exa/search", { query: question, num_results: 5 }) -> results[].url
const serp = await post("https://search.gedx402.com/v1/serper/search", { q: question, num: 5 });
const links = (serp.organic ?? []).slice(0, 2).map((r) => r.link);
// 2. Read the top 2 pages as text (Exa contents-lite, one URL per call)
const pages = [];
for (const url of links) {
  const page = await post("https://search.gedx402.com/v1/exa/contents-lite", { url, max_characters: 3000 }).catch((e) => console.error(e.message));
  if (page?.results?.[0]?.text) pages.push(`SOURCE ${url}\n${page.results[0].text}`);
}
// 3. Answer with citations (OpenAI-compatible chat)
const content = `Answer "${question}" in 5 sentences, citing source URLs.\n\n${pages.join("\n\n")}`;
const chat = await post("https://chat.gedx402.com/v1/chat/completions", { messages: [{ role: "user", content }], max_tokens: 400 });
console.log(chat.choices[0].message.content);

Notes