JavaScript developer guide

Google Search API in JavaScript & Node.js

Use the Google Search API from JavaScript and Node.js with authentication, targeting, pagination, JSON parsing, timeouts, and robust error handling.

A complete Node.js request

Node 18 and later include fetch, so the example has no runtime dependencies. Keep the API key in an environment variable and never ship it in browser-side JavaScript.
search-google.mjs
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30_000);

try {
  const response = await fetch("https://api.prismcrawl.com/v1/google/search", {
    method: "POST",
    signal: controller.signal,
    headers: {
      "x-api-key": process.env.PRISMCRAWL_API_KEY,
      "content-type": "application/json",
    },
    body: JSON.stringify({
      query: "best coffee shops",
      gl: "us",
      hl: "en-US",
      location: "Austin,Texas,United States",
      device: "mobile",
    }),
  });

  if (!response.ok) {
    throw new Error(`PrismCrawl returned ${response.status}: ${await response.text()}`);
  }

  const payload = await response.json();
  for (const result of payload.data.content.results ?? []) {
    console.log(result.rank, result.title, result.url);
  }
} finally {
  clearTimeout(timeout);
}

Or use the official Node.js SDK

The prismcrawl package on npm wraps every endpoint with TypeScript types, automatic retries, and async page iterators for paginated endpoints. It has no runtime dependencies, ships ESM and CommonJS builds, and reads PRISMCRAWL_API_KEY from the environment. Source and examples are on GitHub. For a complete project, see the TypeScript rank and AI Overview citation tracker.
search-google-sdk.mjs
// npm install prismcrawl
import PrismCrawl, { APIStatusError } from "prismcrawl";

const client = new PrismCrawl(); // retries 429s, 5xx, and connection errors

try {
  const response = await client.google.search(
    { query: "best coffee shops", gl: "us", location: "Austin,Texas,United States" },
    { timeout: 30_000 },
  );

  if (response.data.format === "json") {
    for (const result of response.data.content.results) {
      console.log(result.rank, result.title, result.url);
    }
  }
} catch (error) {
  if (error instanceof APIStatusError) {
    console.error(error.status, error.code, error.message, error.requestId);
  }
  throw error;
}

Production integration checklist

  • Call PrismCrawl from a trusted server—not the browser
  • Use an AbortController timeout
  • Handle non-2xx responses before parsing success data
  • Retry transient 429/5xx responses with capped backoff
  • Log request_id for debugging and support
  • Read has_next_page before requesting another page

Pagination and response parsing

paginate.mjs
async function searchPage(query, start = 0) {
  const response = await fetch("https://api.prismcrawl.com/v1/google/search", {
    method: "POST",
    headers: {
      "x-api-key": process.env.PRISMCRAWL_API_KEY,
      "content-type": "application/json",
    },
    body: JSON.stringify({ query, start, gl: "us", device: "desktop" }),
  });
  if (!response.ok) throw new Error(await response.text());
  return (await response.json()).data.content;
}

for (let start = 0; ; start += 10) {
  const data = await searchPage("site:example.com product", start);
  data.results?.forEach(({ rank, url }) => console.log(rank, url));
  if (!data.has_next_page) break;
}

Test before you integrate

Build the request in the SERP API playground, inspect every field in the API reference, compare the Python implementation, return to the Google Search API overview, and review the complete SERP API feature and pricing guide.

Make your first live search today.

Create an account, get 100 free credits, and test live Google or Bing results. No credit card or subscription required.