Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5edf1e8

Browse filesBrowse files
committed
[Examples] Add basic Deno TS example
1 parent 58ba285 commit 5edf1e8
Copy full SHA for 5edf1e8

4 files changed

+50-1Lines changed: 50 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.vscode/settings.json‎

Copy file name to clipboardExpand all lines: .vscode/settings.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"version.ts",
55
"src",
66
"tests",
7-
"scripts"
7+
"scripts",
8+
"examples/deno"
89
],
910
"deno.inlayHints.enumMemberValues.enabled": false,
1011
"deno.inlayHints.functionLikeReturnTypes.enabled": false,
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY
Collapse file

‎examples/deno/basic_ts/README.md‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Deno basic TypeScript example
2+
3+
## Usage
4+
5+
1. Setup environment variables
6+
7+
- Duplicate `.env.example` and name it `.env`.
8+
- Replace `YOUR_API_KEY` with your SerpApi API key.
9+
10+
2. Run the example
11+
12+
```
13+
deno run example.ts
14+
```
15+
16+
## Notes
17+
18+
- Imports rely on `mod.ts` found in the root folder.
19+
- To import the published module from deno.land, update the import to the
20+
following:
21+
```ts
22+
import {
23+
config,
24+
getJson,
25+
GoogleParameters,
26+
} from "https://deno.land/x/serpapi/mod.ts";
27+
```
Collapse file

‎examples/deno/basic_ts/example.ts‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2+
import { config, getJson, GoogleParameters } from "../../../mod.ts";
3+
4+
const { API_KEY: apiKey } = loadSync();
5+
const params = {
6+
q: "Coffee",
7+
api_key: apiKey,
8+
} satisfies GoogleParameters;
9+
10+
// Show result as JSON (async/await)
11+
const response1 = await getJson("google", params);
12+
console.log(response1["organic_results"]);
13+
14+
// Show result as JSON (callback)
15+
getJson("google", params, (json) => console.log(json["organic_results"]));
16+
17+
// Use global config
18+
config.api_key = apiKey;
19+
const response2 = await getJson("google", { q: "Coffee" });
20+
console.log(response2["organic_results"]);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.