npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serpapi

v2.1.0

Published

Scrape and parse search engine results using SerpApi.

Downloads

134,612

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);