← Back to blog

Google Search API in Node.js: Complete Tutorial (2026)

Google Search API in Node.js tutorial: built-in fetch (Node 18+), retry helper, pagination, sample output. Pay-as-you-go from $0.75/1k.

1 min read

Query Google from Node.js and get structured JSON, using serp.cheap. Node 18+ has fetch built in — no dependencies needed.

To sanity-check your key first, one curl is enough:

curl -s https://api.serp.cheap/v1/search \
  -H "X-API-Key: $SERP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"q":"best electric cars 2026","gl":"us"}'

Minimal example

Save as search.mjs (top-level await needs ESM) and run with SERP_KEY=... node search.mjs:

const res = await fetch("https://api.serp.cheap/v1/search", {
  method: "POST",
  headers: { "X-API-Key": process.env.SERP_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({ q: "best electric cars 2026", gl: "us", page: 1 }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const { organic, stats } = await res.json();
organic.forEach(r => console.log(`#${r.position} ${r.title} — ${r.link}`));
console.log("cost (credits):", stats.cost, "cached:", stats.cached);

Output looks like:

#1 The 10 Best Electric Cars of 2026 — https://www.example.com/best-electric-cars-2026
#2 Best EVs for 2026, Tested and Ranked — https://www.example.org/ev-rankings
#3 Electric Car Buying Guide — https://buyersguide.example.net/electric
cost (credits): 6 cached: false

Run the same query again and you’ll see cost (credits): 3 cached: true — cached responses are billed at half the fresh rate.

A small helper with retry

async function search(q, { gl = "us", page = 1, tbs, retries = 4 } = {}) {
  const body = JSON.stringify({ q, gl, page, ...(tbs && { tbs }) });
  for (let attempt = 0; ; attempt++) {
    const res = await fetch("https://api.serp.cheap/v1/search", {
      method: "POST",
      headers: { "X-API-Key": process.env.SERP_KEY, "Content-Type": "application/json" },
      body,
    });
    if (res.ok) return res.json();
    if (![429, 500, 502, 503, 504].includes(res.status) || attempt >= retries) {
      throw new Error(`HTTP ${res.status}`);
    }
    await new Promise(r => setTimeout(r, 1000 * 2 ** attempt)); // backoff
  }
}

const data = await search("serp api nodejs", { gl: "us" });

Paginate

Reuse the search() helper from above — no inline fetch duplication. page is 1-indexed:

async function pages(q, n = 3) {
  const results = [];
  for (let p = 1; p <= n; p++) results.push(...(await search(q, { page: p })).organic);
  return results;
}

Each page is a separate billed request. Keep your API key server-side — never ship it to the browser.

Next

Try serp.cheap

Everything above runs on Node 18+ with zero dependencies. 100 free searches on signup — no card required; pay-as-you-go from $0.75/1k after that, and credits stay valid for 12 months.

Frequently asked questions

How do I call a Google Search API in Node.js?+

Use the built-in fetch (Node 18+) to POST your query. With serp.cheap: fetch('https://api.serp.cheap/v1/search', { method: 'POST', headers: { 'X-API-Key': KEY, 'Content-Type': 'application/json' }, body: JSON.stringify({ q, gl: 'us' }) }).

Do I need an SDK?+

You don't have to — a SERP API is a plain HTTP endpoint, so native fetch (or axios) is enough. But serp.cheap also ships an official JavaScript/TypeScript SDK (npm install @serpcheap/sdk) with typed calls, retries and pagination, plus NestJS and Next.js integrations.

Get your API key

Cheapest direct, pay by card or crypto, no KYC. Or use a marketplace.