Consulta Google desde Node.js y obtén JSON estructurado, usando serp.cheap. Node 18+ trae fetch integrado, sin necesidad de dependencias.
Ejemplo mínimo
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 pequeño helper con reintentos
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" });
Paginar
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;
}
Cada página es una petición facturada por separado. Mantén tu API key en el servidor: nunca la envíes al navegador.
Siguiente
- Google Search API para agentes de IA (LangChain/RAG)
- Sin código con n8n / Make / Zapier
- Otros lenguajes: Python · PHP · Go