# How to use a Google Search API in Node.js (with full code)

Source: https://serpwiz.com/blog/google-search-api-nodejs · Published 2026-12-06

This guide gets live Google results into Node.js with the built-in `fetch` (Node 18 or later), no packages needed. It uses the [SerpWiz Google Search API](https://serpwiz.com/api/google-search).

## 1. Get an API key

[Create a free account](https://serpwiz.com/signup) (1,000 free queries a month, no card), then create a key under **API keys** in the dashboard:

```bash
export SERPWIZ_API_KEY="sw_live_..."
```

## 2. Your first search

Save this as `search.mjs` and run `node search.mjs`:

```javascript
const params = new URLSearchParams({ q: "best crm for startups", gl: "us", hl: "en" });
const res = await fetch(`https://api.serpwiz.com/v1/google/search?${params}`, {
  headers: { "X-API-Key": process.env.SERPWIZ_API_KEY },
});
if (!res.ok) throw new Error((await res.json()).error.message);
const { results } = await res.json();
for (const r of results) console.log(r.position, r.title, r.url);
```

Each search returns one page of Google: up to 10 organic `results`, plus `ads`, `top_stories`, `knowledge_panel` and `related_keywords`.

## 3. A reusable function with retries

Failed requests are free, so retry them. `429`, `502` and `503` responses include a `Retry-After` header in seconds:

```javascript
const API = "https://api.serpwiz.com/v1";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

export async function search(q, { page = 1, gl = "us", hl = "en", attempts = 3 } = {}) {
  const params = new URLSearchParams({ q, page: String(page), gl, hl });
  for (let i = 0; i < attempts; i++) {
    const res = await fetch(`${API}/google/search?${params}`, {
      headers: { "X-API-Key": process.env.SERPWIZ_API_KEY },
    });
    if ([429, 502, 503].includes(res.status)) {
      await sleep(1000 * Number(res.headers.get("retry-after") || 1));
      continue;
    }
    if (!res.ok) throw new Error((await res.json()).error.message);
    return res.json();
  }
  throw new Error(`Gave up after ${attempts} attempts`);
}
```

## 4. Page through the top 100 and save it

```javascript
import { writeFile } from "node:fs/promises";

async function topResults(q, pages = 10) {
  const rows = [];
  for (let page = 1; page <= pages; page++) {
    const data = await search(q, { page });
    for (const r of data.results) rows.push({ rank: rows.length + 1, title: r.title, url: r.url });
    if (!data.next_page) break;
  }
  return rows;
}

const rows = await topResults("best crm for startups", 3);
await writeFile("results.json", JSON.stringify(rows, null, 2));
console.log(`Saved ${rows.length} results`);
```

## 5. Google Maps places

The same key works for the [Google Maps API](https://serpwiz.com/api/google-maps), up to 200 places per search:

```javascript
const mp = new URLSearchParams({ q: "dentists in Miami", num: "20" });
const maps = await fetch(`${API}/google/maps?${mp}`, {
  headers: { "X-API-Key": process.env.SERPWIZ_API_KEY },
}).then((r) => r.json());
for (const p of maps.results) console.log(p.title, p.phone, p.rating);
```

## Using it from an AI agent

Building with an LLM? Connect the hosted [MCP server](https://serpwiz.com/docs/mcp) instead and your agent can search Google with no code, or see [web search for AI agents](https://serpwiz.com/blog/web-search-api-for-ai-agents).

## What it costs

Each search is 1 query: $0.15 per 1,000 on monthly plans, $0.1125 with a year prepaid, and failed requests are free. Also available: the [Python](https://serpwiz.com/blog/google-search-api-python) and [PHP](https://serpwiz.com/blog/google-search-api-php) versions of this guide.
