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. |
|---|
- Supports Node.js 7.10.1 and newer.
- Refer to this example for help.
npm install serpapi
# or if you prefer yarn
yarn add serpapiconst { 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"]); });
- If you prefer using the
importsyntax and top-levelawait, 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);
- 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);
- 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.
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.
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 });
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
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-agentThen 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"), }, });
Built-in pagination is not supported. Please refer to our pagination examples for a manual approach:
- getJson
- getHtml
- getMd
- getJsonBySearchId
- getHtmlBySearchId
- getMdBySearchId
- getAccount
- getLocations
- uploadImage
- extract
Get a JSON response based on search parameters.
parametersobject search query parameters for the enginecallbackfn? optional callback
// 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);
Get a HTML response based on search parameters.
- Accepts an optional callback.
- Responds with a JSON string if the search request hasn't completed.
parametersobject search query parameters for the enginecallbackfn? optional callback
// 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);
Get a Markdown response based on search parameters.
parametersobject search query parameters for the enginecallbackfn? optional callback
// 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);
Get a JSON response given a search ID.
- This search ID can be obtained from the
search_metadata.idkey in the response. - Typically used together with the
asyncparameter. - Accepts an optional callback.
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);
Get a HTML response given a search ID.
- This search ID can be obtained from the
search_metadata.idkey in the response. - Typically used together with the
asyncparameter. - Accepts an optional callback.
- Responds with a JSON if the search request hasn't completed.
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);
Get a Markdown response given a search ID.
- This search ID can be obtained from the
search_metadata.idkey in the response. - Typically used together with the
asyncparameter. - Accepts an optional callback.
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);
Get account information of an API key. https://serpapi.com/account-api
-
parametersobject (optional, default{}) -
callbackfn? optional callback
// async/await const info = await getAccount({ api_key: API_KEY }); // callback getAccount({ api_key: API_KEY }, console.log);
Get supported locations. Does not require an API key. https://serpapi.com/locations-api
-
parametersobject (optional, default{}) -
callbackfn? optional callback
// async/await const locations = await getLocations({ limit: 3 }); // callback getLocations({ limit: 3 }, console.log);
Upload an image for use with supported engines.
parametersobjectparameters.image(Uint8Array | ArrayBuffer | string) binary image contents or file pathparameters.api_keystring? API keyparameters.timeoutnumber? timeout in milliseconds
callbackfn? optional callback
const result = await uploadImage({ api_key: API_KEY, image: "image.png", }); console.log(result.image_id);
Get structured JSON for any public web page using the Extract API. https://serpapi.com/extract-api
- The
engineparameter is always set toextract. - Accepts an optional callback.
parametersobject Extract API parametersparameters.urlstring absolutehttporhttpsURL of the page to extractparameters.modestring?scrape(default),map, orcrawlparameters.api_keystring? API keyparameters.timeoutnumber? timeout in milliseconds for the request to SerpApiparameters.fetch_timeoutnumber? Extract APItimeoutparameter, in milliseconds
callbackfn? optional callback
// 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, });