← Torna al blog

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

Usa una Google Search API in Node.js / JavaScript — richieste con fetch, paginazione async, retry e un helper tipizzato. Esempi copia-incolla con serp.cheap.

1 min di lettura

Interroga Google da Node.js e ottieni JSON strutturato, usando serp.cheap. Node 18+ ha fetch integrato — nessuna dipendenza necessaria.

Esempio minimo

const res = await fetch("https://api.serp.cheap/v1/search", {
  method: "POST",
  headers: { "X-API-Key": "YOUR_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);

Un piccolo helper con 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" });

Paginazione

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;
}

Ogni pagina è una richiesta fatturata separatamente. Mantieni la tua chiave API lato server — non inviarla mai al browser.

Prossimi passi

Frequently asked questions

Come chiamo una Google Search API in Node.js?+

Usa il fetch integrato (Node 18+) per inviare la query in POST. Con 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' }) }).

Mi serve un SDK?+

Non è obbligatorio — una SERP API è un semplice endpoint HTTP, quindi il fetch nativo (o axios) è sufficiente. Ma serp.cheap offre anche un SDK ufficiale JavaScript/TypeScript (npm install @serpcheap/sdk) con chiamate tipizzate, retry e paginazione, oltre alle integrazioni per NestJS e Next.js.

Ottieni la tua chiave API

Più economico diretto, paga con carta o crypto, senza KYC. O tramite un marketplace.