GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - serpapi/serpapi-javascript: Scrape and parse search engine results using SerpApi.

BranchesTags
Open more actions menu

Latest commit

 

History

231 Commits
231 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
docs
docs
 
 
examples
examples
 
 
scripts
scripts
 
 
smoke_tests
smoke_tests
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
deno.json
deno.json
 
 
mod.ts
mod.ts
 
 
version.ts
version.ts
 
 

Repository files navigation

SerpApi for JavaScript/TypeScript

npm version Build status License SerpApi Libraries

Scrape and parse search engine results using SerpApi. Retrieve structured JSON, token-efficient Markdown for AI agents, or raw HTML 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
# or if you prefer yarn
yarn add 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

  • TypeScript support.
  • Works out-of-the-box with Node.js and Deno.
  • Promises and async/await support.
  • Callbacks support.
  • JSON, HTML, and token-efficient Markdown response formats.
  • Extract API support to get any public web page as structured JSON or Markdown.
  • Examples in JavaScript/TypeScript on Node.js/Deno using ESM/CommonJS, and more.

Markdown output for AI agents

Use getMd to get token-efficient Markdown optimized for LLMs and AI agents:

import { getMd } from "serpapi";

const markdown = await getMd({
  engine: "google",
  api_key: API_KEY,
  q: "coffee",
});

Archived results are also available as Markdown with getMdBySearchId.

Learn more about SerpApi Markdown output.

Extract API

Use extract to get any public web page as structured JSON with the Extract API. url is required, and engine is always set to extract.

import { config, extract } from "serpapi";

config.api_key = API_KEY;

const page = await extract({ url: "https://example.com/" });
console.log(page.page_information);
console.log(page.markdown);

Set mode to map to list a website's URLs, or to crawl to extract every page visited. Crawl mode consumes one search credit per extracted page.

// List URLs from the page links and sitemaps
const { urls } = await extract({
  url: "https://books.toscrape.com/",
  mode: "map",
});

// Extract up to 3 pages, following links 1 level deep
const { pages } = await extract({
  url: "https://books.toscrape.com/",
  mode: "crawl",
  max_pages: 3,
  max_depth: 1,
});

To extract a page found in the results, pass its URL to extract again. Settings such as device or include_tags are not carried over, so pass them again if needed:

const page = await extract({ url: "https://books.toscrape.com/" });
const link = page.inline_links?.[0]?.link;
if (link) {
  const linkedPage = await extract({ url: link });
  console.log(linkedPage.page_information);
}

To get only the Markdown, use getMd with the extract engine:

const markdown = await getMd({
  engine: "extract",
  url: "https://example.com/",
});

Note: Search pages covered by a dedicated SerpApi engine (such as Google, Bing, eBay, or Walmart) are redirected to that engine, and the response follows that engine's JSON structure.

As in the other functions, timeout sets how long this library waits for SerpApi to respond. To set the Extract API timeout parameter (how long SerpApi waits for the page), use fetch_timeout. It also works with getJson, getHtml, and getMd. Keep timeout higher than fetch_timeout, since the request to SerpApi includes fetching the page:

const page = await extract({
  url: "https://example.com/",
  fetch_timeout: 10000, // sent to the Extract API as `timeout`
  timeout: 60000, // request timeout of this library
});

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

Using a proxy

Note: SerpApi handles proxies on its end — you do not need to supply your own proxy to avoid blocks or CAPTCHAs. This option is only for users who need to route all outbound requests through a corporate or network proxy.

You can route requests through your own proxy by passing requestOptions with an HttpsProxyAgent instance. This can be done either globally through the config object or per-request in the parameters.

First, install the required package:

npm install https-proxy-agent
# or if you prefer yarn
yarn add https-proxy-agent

Then use it in your code:

import { config, getJson } from "serpapi";
import { HttpsProxyAgent } from "https-proxy-agent";

// Global configuration
config.requestOptions = {
  agent: new HttpsProxyAgent("http://proxy-server:port"),
};

// Or per-request configuration
await getJson({
  engine: "google",
  q: "coffee",
  requestOptions: {
    agent: new HttpsProxyAgent("http://proxy-server:port"),
  },
});

Pagination

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

  • Pagination example (Node.js >= 7)
  • Pagination example (Node.js >= 14)
  • Pagination example (Deno)

Functions

Table of contents

  • getJson
    • Parameters
    • Examples
  • getHtml
    • Parameters
    • Examples
  • getMd
    • Parameters
    • Examples
  • getJsonBySearchId
    • Parameters
    • Examples
  • getHtmlBySearchId
    • Parameters
    • Examples
  • getMdBySearchId
    • Parameters
    • Examples
  • getAccount
    • Parameters
    • Examples
  • getLocations
    • Parameters
    • Examples
  • uploadImage
    • Parameters
    • Examples
  • extract
    • Parameters
    • Examples

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

getMd

Get a Markdown response based on search parameters.

Parameters

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

Examples

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

// callback
getMd({ 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);

getMdBySearchId

Get a Markdown 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 markdown = await getMd({
  engine: "google",
  api_key: API_KEY,
  q: "coffee",
});
const idMatch = markdown.match(/^  id:\s*(.+)$/m);
if (!idMatch) throw new Error("Search ID missing from Markdown frontmatter");
const searchId = idMatch[1].trim();

// async/await
const archivedMarkdown = await getMdBySearchId(searchId, { api_key: API_KEY });

// callback
getMdBySearchId(searchId, { 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);

uploadImage

Upload an image for use with supported engines.

Parameters

  • parameters object
    • parameters.image (Uint8Array | ArrayBuffer | string) binary image contents or file path
    • parameters.api_key string? API key
    • parameters.timeout number? timeout in milliseconds
  • callback fn? optional callback

Examples

const result = await uploadImage({
  api_key: API_KEY,
  image: "image.png",
});
console.log(result.image_id);

extract

Get structured JSON for any public web page using the Extract API. https://serpapi.com/extract-api

  • The engine parameter is always set to extract.
  • Accepts an optional callback.

Parameters

  • parameters object Extract API parameters
    • parameters.url string absolute http or https URL of the page to extract
    • parameters.mode string? scrape (default), map, or crawl
    • parameters.api_key string? API key
    • parameters.timeout number? timeout in milliseconds for the request to SerpApi
    • parameters.fetch_timeout number? Extract API timeout parameter, in milliseconds
  • callback fn? optional callback

Examples

// async/await
const json = await extract({ api_key: API_KEY, url: "https://example.com/" });

// callback
extract({ api_key: API_KEY, url: "https://example.com/" }, console.log);

// crawl a website
const { pages } = await extract({
  api_key: API_KEY,
  url: "https://books.toscrape.com/",
  mode: "crawl",
  max_pages: 3,
  max_depth: 1,
});

About

Scrape and parse search engine results using SerpApi.

serpapi.com

Topics

denojavascriptjsonnodejsscrapingserp-apiserpapitypescriptweb-scraping-nodejs

Resources

Readme
MIT license

Contributing

Contributing
Activity
Custom properties

Stars

101 stars

Watchers

15 watching

Forks

33 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.