Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

serpapi

serpapi136.6kMIT2.1.0TypeScript support: included

Scrape and parse search engine results using SerpApi.

serpapi, serp api, scrape, google, search, api, query, json, html, image, automated, localization, news, seo, walmart, yahoo, yandex, scholar, bing, baidu, ebay, youtube, apple, store, app, homedepot, naver, duckduckgo

readme

SerpApi for JavaScript/TypeScript

npm version Deno version Build status License SerpApi Libraries

Scrape and parse search engine results using SerpApi. Get search results from Google, Bing, Baidu, Yandex, Yahoo, Home Depot, eBay and more.

🪧 Coming from google-search-results-nodejs?
Check out the migration document to find out how to upgrade.

Quick start

Node.js

  • Supports Node.js 7.10.1 and newer.
  • Refer to this example for help.
npm install serpapi
const { getJson } = require("serpapi");
getJson({
  engine: "google",
  api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
  q: "coffee",
  location: "Austin, Texas",
}, (json) => {
  console.log(json["organic_results"]);
});

Node.js with ES Modules (ESM) and top-level await

  • If you prefer using the import syntax and top-level await, you need to use at least Node.js 14.8.0.
  • Refer to this example for help.

You will need to add "type": "module" to your package.json:

{
  "type": "module",
  // rest of package.json
}
import { getJson } from "serpapi";
const response = await getJson({
  engine: "google",
  api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
  q: "coffee",
  location: "Austin, Texas",
});
console.log(response);

Deno

  • Import directly from deno.land.
  • Usage is otherwise the same as above.
  • Refer to this example for help.
import { getJson } from "https://deno.land/x/serpapi/mod.ts";
const response = await getJson({
  engine: "google",
  api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
  q: "coffee",
  location: "Austin, Texas",
});
console.log(response);

Features

Configuration

You can declare a global api_key and timeout value by modifying the config object. timeout is defined in milliseconds and defaults to 60 seconds.

All functions, other than getLocations, accepts an optional api_key and timeout that will take precedence over the values defined in config.

getLocations doesn't require an API key.

import { config, getJson } from "serpapi";

config.api_key = API_KEY;
config.timeout = 60000;

await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in the config
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used

Pagination

Built-in pagination is not supported. Please refer to our pagination examples for a manual approach:

Functions

Table of Contents

getJson

Get a JSON response based on search parameters.

Parameters

  • parameters object search query parameters for the engine
  • callback fn? optional callback

Examples

// single call (async/await)
const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });

// single call (callback)
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);

getHtml

Get a HTML response based on search parameters.

  • Accepts an optional callback.
  • Responds with a JSON string if the search request hasn't completed.

Parameters

  • parameters object search query parameters for the engine
  • callback fn? optional callback

Examples

// async/await
const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });

// callback
getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);

getJsonBySearchId

Get a JSON response given a search ID.

  • This search ID can be obtained from the search_metadata.id key in the response.
  • Typically used together with the async parameter.
  • Accepts an optional callback.

Parameters

  • searchId string search ID
  • parameters object (optional, default {})

    • parameters.api_key string? API key
    • parameters.timeout number? timeout in milliseconds
  • callback fn? optional callback

Examples

const response = await getJson({
  engine: "google",
  api_key: API_KEY,
  async: true,
  q: "coffee",
});
const { id } = response.search_metadata;
await delay(1000); // wait for the request to be processed.

// async/await
const json = await getJsonBySearchId(id, { api_key: API_KEY });

// callback
getJsonBySearchId(id, { api_key: API_KEY }, console.log);

getHtmlBySearchId

Get a HTML response given a search ID.

  • This search ID can be obtained from the search_metadata.id key in the response.
  • Typically used together with the async parameter.
  • Accepts an optional callback.
  • Responds with a JSON if the search request hasn't completed.

Parameters

  • searchId string search ID
  • parameters object (optional, default {})

    • parameters.api_key string? API key
    • parameters.timeout number? timeout in milliseconds
  • callback fn? optional callback

Examples

const response = await getJson({
  engine: "google",
  api_key: API_KEY,
  async: true,
  q: "coffee",
});
const { id } = response.search_metadata;
await delay(1000); // wait for the request to be processed.

// async/await
const html = await getHtmlBySearchId(id, { api_key: API_KEY });

// callback
getHtmlBySearchId(id, { api_key: API_KEY }, console.log);

getAccount

Get account information of an API key. https://serpapi.com/account-api

Parameters

  • parameters object (optional, default {})

    • parameters.api_key string? API key
    • parameters.timeout number? timeout in milliseconds
  • callback fn? optional callback

Examples

// async/await
const info = await getAccount({ api_key: API_KEY });

// callback
getAccount({ api_key: API_KEY }, console.log);

getLocations

Get supported locations. Does not require an API key. https://serpapi.com/locations-api

Parameters

  • parameters object (optional, default {})

    • parameters.q string? query for a location
    • parameters.limit number? limit on number of locations returned
    • parameters.timeout number? timeout in milliseconds
  • callback fn? optional callback

Examples

// async/await
const locations = await getLocations({ limit: 3 });

// callback
getLocations({ limit: 3 }, console.log);

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

Added

  • Expose EngineParameters type.
  • Expose InvalidArgumentError error.

Changed

  • Remove settings.json, update CONTRIBUTING.md.
  • Make types more lenient so newly supported engines/parameters whose types have not yet been updated do not throw warnings.

Fixed

Removed

  • Remove all types for engine parameters and responses. SerpApi's documentation should be the only source of truth for valid engines and their parameters.
  • Remove pagination support.

1.1.1 - 2023-02-15

Added

Changed

  • Exclude Deno and DOMException from distribution shim.
  • Refine retrieval of Node/Deno version.

1.1.0 - 2023-02-02

Added

  • [Google Shopping] Add new API.

1.0.0 - 2023-01-19

Added

  • Add pagination support for getJson.
  • Export error classes.
  • Add examples.
  • [Apple Reviews] Add more sort options.
  • [Google Finance] Add new API.
  • [Google Maps Photo Meta] Add new API.
  • [Google Maps] Add place_id parameter.
  • [Google] Add kgmid and si parameters.

Changed

  • [Google Trends] Refine tz parameter documentation.
  • [Google] Refine start parameter documentation.

0.0.13 - 2023-01-13

Fixed

  • Ensure engine parameter type exports actually appear in the npm module.

0.0.12 - 2023-01-09

Changed

  • Minor refactoring of validators.

Fixed

  • Ensure engine parameter type exports are not exported in the JS ESM module.

0.0.11 - 2023-01-05

Added

  • Add module version to the source parameter.

Changed

  • Update deploy flow to be triggered by a change in version.ts.
  • Update development dependencies.

0.0.10 - 2023-01-03

Added

  • [Google About This Result] Add google_domain parameter.
  • Ensure cov_profile is cleared when generating test coverage.
  • Add CONTRIBUTING.md.
  • Update VSCode workspace settings to format TypeScript and Markdown using deno.
  • Add descriptions for base parameters.

Fixed

  • Ensure engine parameter types include SerpApi types (e.g. api_key, device).

0.0.9 - 2022-12-27

Added

  • [Google Lens] Add types.
  • Update build GitHub Action workflow to test the building of npm files.

Changed

  • Update tests.
    • Run getLocations tests regardless of whether API key is defined.
    • Use metered query for some tests.
    • Add missing case for getHtmlBySearchId.

0.0.8 - 2022-12-12

Added

  • README badges.
  • [Google Jobs] Add ltype parameter.

Fixed

  • Fix issue where setting api_key in config was not working.

0.0.7 - 2022-12-08

Fixed

Use absolute link for the migration document link as Deno does not handle relative links correctly on their site.

0.0.6 - 2022-12-08

Fixed

Ensure the migration document link works on the Deno module page.

0.0.5 - 2022-12-08

Changed

Enabled publishing to npm.

0.0.4 - 2022-12-08

Fixed

Ensure Deno imports mod.ts from the root directory rather than src/.

0.0.3 - 2022-12-08

Added

Initial stable release.

The API signatures of all functions are as follows:

  • getJson(engine, parameters, callback?)
  • getHtml(engine, parameters, callback?)
  • getJsonBySearchId(searchId, parameters?, callback?)
  • getHtmlBySearchId(searchId, parameters?, callback?)
  • getAccount(parameters?, callback?)
  • getLocations(parameters?, callback?)