1818### Node.js
1919
2020- Supports Node.js 7.10.1 and newer.
21- - Refer to [ this example] ( examples/node/basic_js_node_7_up ) for help.
21+ - Refer to [ this example] ( examples/node/js_node_7_up ) for help.
2222
2323``` bash
2424npm install serpapi
@@ -40,7 +40,7 @@ getJson({
4040
4141- If you prefer using the ` import ` syntax and top-level ` await ` , you need to use
4242 at least Node.js 14.8.0.
43- - Refer to [ this example] ( examples/node/basic_js_node_14_up ) for help.
43+ - Refer to [ this example] ( examples/node/js_node_14_up ) for help.
4444
4545You will need to add ` "type": "module" ` to your ` package.json ` :
4646
@@ -66,10 +66,17 @@ console.log(response);
6666
6767- Import directly from deno.land.
6868- Usage is otherwise the same as above.
69- - Refer to [ this example] ( examples/deno/basic_ts ) for help.
69+ - Refer to [ this example] ( examples/deno ) for help.
7070
7171``` ts
7272import { getJson } from " https://deno.land/x/serpapi/mod.ts" ;
73+ const response = await getJson ({
74+ engine: " google" ,
75+ api_key: API_KEY , // Get your API_KEY from https://serpapi.com/manage-api-key
76+ q: " coffee" ,
77+ location: " Austin, Texas" ,
78+ });
79+ console .log (response );
7380```
7481
7582## Features
@@ -80,8 +87,6 @@ import { getJson } from "https://deno.land/x/serpapi/mod.ts";
8087- Promises and async/await support.
8188- Callbacks support.
8289- [ Examples in JavaScript/TypeScript on Node.js/Deno using ESM/CommonJS, and more] ( https://github.com/serpapi/serpapi-javascript/tree/master/examples ) .
83- - [ Pagination support] ( #pagination ) .
84- - (Planned) More error classes.
8590
8691## Configuration
8792
@@ -105,30 +110,12 @@ await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY
105110
106111## Pagination
107112
108- Search engines handle pagination in several different ways. Some rely on an
109- "offset" value to return results starting from a specific index, while some
110- others rely on the typical notion of a "page". These are often combined with a
111- "size" value to define how many results are returned in a search.
113+ Built-in pagination is not supported. Please refer to our pagination examples
114+ for a manual approach:
112115
113- This module helps you handle pagination easily. After receiving search results
114- from ` getJson ` , simply call the ` next() ` method on the returned object to
115- retrieve the next page of results. If there is no ` next() ` method, then either
116- pagination is not supported for the search engine or there are no more pages to
117- be retrieved.
118-
119- ``` js
120- const page1 = await getJson ({ engine: " google" , q: " coffee" , start: 15 });
121- const page2 = await page1 .next ? .();
122- ` ` `
123-
124- You may pass in the engine's supported pagination parameters as per normal. In
125- the above example, the first page contains the 15th to the 24th result while the
126- second page contains the 25th to the 34th result.
127-
128- Note that if you set ` no_cache` to ` true ` , all subsequent ` next ()` calls will
129- not return cached results.
130-
131- Refer to the [` getJson` definition below](#getjson) for more examples.
116+ - [ Pagination example (Node.js >= 7)] ( examples/node/js_node_7_up/pagination_example.js )
117+ - [ Pagination example (Node.js >= 14)] ( examples/node/js_node_14_up/pagination_example.js )
118+ - [ Pagination example (Deno)] ( examples/deno/pagination_example.ts )
132119
133120## Functions
134121
@@ -159,10 +146,6 @@ Refer to the [`getJson` definition below](#getjson) for more examples.
159146
160147Get a JSON response based on search parameters.
161148
162- - Accepts an optional callback.
163- - Get the next page of results by calling the ` .next ()` method on the returned
164- response object.
165-
166149#### Parameters
167150
168151- ` parameters `
@@ -180,43 +163,6 @@ const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
180163getJson ({ engine: " google" , api_key: API_KEY , q: " coffee" }, console .log );
181164```
182165
183- ` ` ` javascript
184- // pagination (async/await)
185- const page1 = await getJson ({ engine: " google" , q: " coffee" , start: 15 });
186- const page2 = await page1 .next ? .();
187- ` ` `
188-
189- ` ` ` javascript
190- // pagination (callback)
191- getJson ({ engine: " google" , q: " coffee" , start: 15 }, (page1 ) => {
192- page1 .next ? .((page2 ) => {
193- console .log (page2);
194- });
195- });
196- ` ` `
197-
198- ` ` ` javascript
199- // pagination loop (async/await)
200- const organicResults = [];
201- let page = await getJson ({ engine: " google" , api_key: API_KEY , q: " coffee" });
202- while (page) {
203- organicResults .push (... page .organic_results );
204- if (organicResults .length >= 30 ) break ;
205- page = await page .next ? .();
206- }
207- ` ` `
208-
209- ` ` ` javascript
210- // pagination loop (callback)
211- const organicResults = [];
212- getJson ({ engine: " google" , api_key: API_KEY , q: " coffee" }, (page ) => {
213- organicResults .push (... page .organic_results );
214- if (organicResults .length < 30 && page .next ) {
215- page .next ();
216- }
217- });
218- ` ` `
219-
220166### getHtml
221167
222168Get a HTML response based on search parameters.
0 commit comments