SerpApi for JavaScript/TypeScript
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
Ensure you're running at least Node.js v16.14.
npm install serpapi
import { getJson } from "serpapi";
const response = await getJson("google", {
api_key: API_KEY,
q: "coffee",
location: "Austin, Texas",
});
console.log(response);
Deno
Import directly from deno.land. Usage is otherwise the same as above.
import { getJson } from "https://deno.land/x/serpapi/mod.ts";
Features
- TypeScript types such as supported parameters and function argument types.
- Works out-of-the-box with Node.js and Deno.
- Promises and async/await support.
- (Planned) Pagination support.
- (Planned) More examples.
- (Planned) More error classes.
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
that doesn't require an API key,
accepts an optional api_key
and timeout
that will take precedence over the
values defined in config
.
import { config, getJson } from "serpapi";
config.api_key = API_KEY;
config.timeout = 60000;
await getJson("google", { q: "coffee" });
await getJson("google", { api_key: API_KEY_2, q: "coffee" });
Functions
Table of Contents
getJson
Get a JSON response based on search parameters.
- Accepts an optional callback.
Parameters
engine
string
engine nameparameters
object
search query parameters for the enginecallback
fn? optional callback
Examples
const json = await getJson("google", { api_key: API_KEY, q: "coffee" });
getJson("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
engine
string
engine nameparameters
object
search query parameters for the enginecallback
fn? optional callback
Examples
const html = await getHtml("google", { api_key: API_KEY, q: "coffee" });
getHtml("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 keyparameters.timeout
number?
timeout in milliseconds
-
callback
fn? optional callback
Examples
const response = await getJson("google", {
api_key: API_KEY,
async: true,
q: "coffee",
});
const { id } = response.search_metadata;
await delay(1000);
const json = await getJsonBySearchId(id, { api_key: API_KEY });
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 keyparameters.timeout
number?
timeout in milliseconds
-
callback
fn? optional callback
Examples
const response = await getJson("google", {
api_key: API_KEY,
async: true,
q: "coffee",
});
const { id } = response.search_metadata;
await delay(1000);
const html = await getHtmlBySearchId(id, { api_key: API_KEY });
getHtmlBySearchId(id, { api_key: API_KEY }, console.log);
getAccount
Get account information of an API key. https://serpapi.com/account-api
Parameters
Examples
const info = await getAccount({ api_key: API_KEY });
getAccount({ api_key: API_KEY }, console.log);
getLocations
Get supported locations. Does not require an API key.
https://serpapi.com/locations-api
Parameters
Examples
const locations = await getLocations({ limit: 3 });
getLocations({ limit: 3 }, console.log);