-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbasic_example.js
More file actions
32 lines (26 loc) · 1017 Bytes
/
basic_example.js
File metadata and controls
32 lines (26 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Example works for Node.js 14 and newer.
* - Uses ESM imports which is supported from Node.js 13.2.0.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility
* - Uses top-level await which is supported from Node.js 14.8.0.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility
*/
import * as Dotenv from "dotenv";
import process from "process";
import { config, getJson } from "serpapi";
Dotenv.config();
const apiKey = process.env.API_KEY;
const params = {
engine: "google",
q: "Coffee",
api_key: apiKey,
};
// Show result as JSON (async/await)
const response1 = await getJson(params);
console.log(response1["organic_results"]);
// Show result as JSON (callback)
getJson(params, (json) => console.log(json["organic_results"]));
// Use global config
config.api_key = apiKey;
const response2 = await getJson({ engine: "google", q: "Coffee" });
console.log(response2["organic_results"]);