Announcing our new library for JavaScript and TypeScript

We’re excited to release our new and improved library for JavaScript and TypeScript, serpapi!

SerpApi has grown to be so much more than just supporting Google Search. We support a wide range of search engines including Bing, Baidu, Yandex, Yahoo, Home Depot, eBay and many more. We’ve built this new library to not only reflect our continual commitment to creating the API for search engines, but to further improve your experience with SerpApi.

GitHub  |  npm  |  deno.land

Highlights

  • TypeScript types. Easily see what search parameters are supported for each search engine.
  • Streamlined API functions. No instantiation of classes, simply import and call the functions to get the results you want quickly.
  • First-class Promises support. Simplify your code by using async/await to retrieve search engine results with ease.
  • Straightforward pagination. Spend less time figuring out how pagination works, just call .next() to retrieve the next page of results.
  • Works with Node.js and Deno. Take your pick, we’ve published the library on npm and deno.land.

How to use it

Install the library using your preferred package manager:

npm install serpapi

Import getJson, specify the target engine and pass in your SerpApi API key. Get your API key from https://serpapi.com/manage-api-key.

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

For Deno users, import directly from deno.land instead:

import { getJson } from "https://deno.land/x/serpapi/mod.ts";

How is this different from
google-search-results-nodejs?

Other than the naming to better express support for many other search engines, we’ve re-worked it in several ways. These are taken from the migration document to help you upgrade and take advantage of the latest features.

Use functions rather than classes

Our previous approach required importing and instantiating specific search engine classes. This made it difficult to maintain while we added support for more engines. To simplify things, this module exposes functions (getJson, getHtml) that accept an engine parameter instead.

// ❌ Previous way.
import { GoogleSearch } from "google-search-results-nodejs";
const engine = new GoogleSearch(API_KEY);
engine.json(...);

// ✅ New way, import and use functions directly.
import { getJson } from "serpapi";
getJson("google", { api_key: API_KEY, ... })

search_archive method has been replaced

We’ve replaced the search_archive method with getJsonBySearchId and getHtmlBySearchId to better express their purpose. These functions accept a searchId and return JSON/HTML.

// ❌ Previous way, only supported JSON output.
engine.search_archive(searchId, console.log);

// ✅ New way, use `getJsonBySearchId`.
getJsonBySearchId(searchId, { api_key: API_KEY }, console.log);

// ✅ New way, use `getHtmlBySearchId` if you want the HTML result.
getHtmlBySearchId(searchId, { api_key: API_KEY }, console.log);

account and location methods have been renamed

The account and location methods are now getAccount and getLocations. Previously, you had to call these methods from a search engine object. This was misleading since these functions are not tied to any specific engine.

// ❌ Previous way, was part of the engine class.
engine.account(console.log);
engine.location("Austin", 5, console.log);

// ✅ New way, functions not tied to a class.
import { getAccount, getLocations } from "serpapi";
getAccount({ api_key: API_KEY }, console.log);
getLocations({ q: "Austin" }, console.log);

New features

In addition to the changes above, we’ve exposed TypeScript types, added support for Promises, added support for pagination, added support for global config and exposed error classes. The library can be imported as an ES Module (ESM) or CommonJS module via npm or deno.land.

Behind the scenes, this module is written fully in TypeScript using Deno with a comprehensive test suite and an automated deployment pipeline.

Still curious?

Check out the GitHub repository to take a deeper dive.