Skip to content

Commit f02ffb2

Browse files
gururisensarrazin
andauthored
Add support for SerpStack API (#669)
* Add support for SerpStack API * update const to SERPSTACK_API_KEY * update readme * lint --------- Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com>
1 parent 9b99c58 commit f02ffb2

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ HF_ACCESS_TOKEN=#LEGACY! Use HF_TOKEN instead
1616
YDC_API_KEY=#your docs.you.com api key here
1717
SERPER_API_KEY=#your serper.dev api key here
1818
SERPAPI_KEY=#your serpapi key here
19+
SERPSTACK_API_KEY=#your serpstack api key here
1920
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
2021

2122
WEBSEARCH_ALLOWLIST=`[]` # if it's defined, allow websites from only this list.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ PUBLIC_APP_DISCLAIMER=
122122

123123
### Web Search config
124124

125-
You can enable the web search through an API by adding `YDC_API_KEY` ([docs.you.com](https://docs.you.com)) or `SERPER_API_KEY` ([serper.dev](https://serper.dev/)) or `SERPAPI_KEY` ([serpapi.com](https://serpapi.com/)) to your `.env.local`.
125+
You can enable the web search through an API by adding `YDC_API_KEY` ([docs.you.com](https://docs.you.com)) or `SERPER_API_KEY` ([serper.dev](https://serper.dev/)) or `SERPAPI_KEY` ([serpapi.com](https://serpapi.com/)) or `SERPSTACK_API_KEY` ([serpstack.com](https://serpstack.com/)) to your `.env.local`.
126126

127127
You can also simply enable the local websearch by setting `USE_LOCAL_WEBSEARCH=true` in your `.env.local`.
128128

src/lib/server/websearch/searchWeb.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { YouWebSearch } from "../../types/WebSearch";
22
import { WebSearchProvider } from "../../types/WebSearch";
3-
import { SERPAPI_KEY, SERPER_API_KEY, USE_LOCAL_WEBSEARCH, YDC_API_KEY } from "$env/static/private";
3+
import {
4+
SERPAPI_KEY,
5+
SERPER_API_KEY,
6+
SERPSTACK_API_KEY,
7+
USE_LOCAL_WEBSEARCH,
8+
YDC_API_KEY,
9+
} from "$env/static/private";
410
import { getJson } from "serpapi";
511
import type { GoogleParameters } from "serpapi";
612
import { searchWebLocal } from "./searchWebLocal";
@@ -24,6 +30,9 @@ export async function searchWeb(query: string) {
2430
if (SERPAPI_KEY) {
2531
return await searchWebSerpApi(query);
2632
}
33+
if (SERPSTACK_API_KEY) {
34+
return await searchSerpStack(query);
35+
}
2736
throw new Error("No You.com or Serper.dev or SerpAPI key found");
2837
}
2938

@@ -100,3 +109,36 @@ export async function searchWebYouApi(query: string) {
100109
organic_results: formattedResultsWithSnippets,
101110
};
102111
}
112+
113+
export async function searchSerpStack(query: string) {
114+
const response = await fetch(
115+
`http://api.serpstack.com/search?access_key=${SERPSTACK_API_KEY}&query=${query}&hl=en&gl=us`,
116+
{
117+
method: "GET",
118+
headers: {
119+
"Content-type": "application/json; charset=UTF-8",
120+
},
121+
}
122+
);
123+
124+
const data = (await response.json()) as Record<string, any>;
125+
126+
if (!response.ok) {
127+
throw new Error(
128+
data["error"] ??
129+
`SerpStack API returned error code ${response.status} - ${response.statusText}`
130+
);
131+
}
132+
133+
const resultsWithSnippets = data["organic_results"].map(
134+
({ title, url, snippet }: { title: string; url: string; snippet: string | undefined }) => ({
135+
title,
136+
link: url,
137+
text: snippet || "",
138+
})
139+
);
140+
141+
return {
142+
organic_results: resultsWithSnippets ?? [],
143+
};
144+
}

src/routes/+layout.server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DEFAULT_SETTINGS } from "$lib/types/Settings";
88
import {
99
SERPAPI_KEY,
1010
SERPER_API_KEY,
11+
SERPSTACK_API_KEY,
1112
MESSAGES_BEFORE_LOGIN,
1213
YDC_API_KEY,
1314
USE_LOCAL_WEBSEARCH,
@@ -77,7 +78,13 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
7778
}))
7879
.toArray(),
7980
settings: {
80-
searchEnabled: !!(SERPAPI_KEY || SERPER_API_KEY || YDC_API_KEY || USE_LOCAL_WEBSEARCH),
81+
searchEnabled: !!(
82+
SERPAPI_KEY ||
83+
SERPER_API_KEY ||
84+
SERPSTACK_API_KEY ||
85+
YDC_API_KEY ||
86+
USE_LOCAL_WEBSEARCH
87+
),
8188
ethicsModalAccepted: !!settings?.ethicsModalAcceptedAt,
8289
ethicsModalAcceptedAt: settings?.ethicsModalAcceptedAt ?? null,
8390
activeModel: settings?.activeModel ?? DEFAULT_SETTINGS.activeModel,

0 commit comments

Comments
 (0)