-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-search.js
More file actions
39 lines (32 loc) · 1.45 KB
/
Copy pathbasic-search.js
File metadata and controls
39 lines (32 loc) · 1.45 KB
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
33
34
35
36
37
38
39
// Serpent API — Basic Google search example (Node.js, no dependencies)
// Docs: https://apiserpent.com/docs
// Sign up: https://apiserpent.com/login (10 free searches, no card)
//
// Requires Node 18+ (built-in fetch). Run:
// SERPENT_API_KEY="sk_live_..." node basic-search.js
const API = "https://api.apiserpent.com";
const KEY = process.env.SERPENT_API_KEY || "sk_live_demo_key_replace_me";
async function search(query, engine = "google", country = "us") {
const url = new URL(`${API}/api/search/quick`);
url.searchParams.set("q", query);
url.searchParams.set("engine", engine);
url.searchParams.set("country", country);
const r = await fetch(url, { headers: { "X-API-Key": KEY } });
if (!r.ok) throw new Error(`HTTP ${r.status}: ${await r.text()}`);
return r.json();
}
const data = await search("cheapest google serp api");
console.log(`\nTop 5 results for: ${data.query}\n`);
for (const hit of data.results.organic.slice(0, 5)) {
console.log(`${hit.position}. ${hit.title}`);
console.log(` ${hit.url}`);
console.log(` ${(hit.snippet || "").slice(0, 140)}\n`);
}
if (data.results.featuredSnippet) {
console.log(`Featured snippet: ${data.results.featuredSnippet.text.slice(0, 200)}`);
console.log(` → ${data.results.featuredSnippet.sourceUrl}\n`);
}
if (data.results.aiOverview) {
console.log(`AI Overview: ${data.results.aiOverview.text.slice(0, 200)}`);
console.log(` Sources: ${(data.results.aiOverview.sources || []).length}`);
}