Spaces:
Running
Running
SebastjanPrachovskij
commited on
Add support for SearchApi API (#1196)
Browse files* Add SearchApi integration
* Remove isURL
.env
CHANGED
@@ -24,6 +24,7 @@ YDC_API_KEY=#your docs.you.com api key here
|
|
24 |
SERPER_API_KEY=#your serper.dev api key here
|
25 |
SERPAPI_KEY=#your serpapi key here
|
26 |
SERPSTACK_API_KEY=#your serpstack api key here
|
|
|
27 |
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
|
28 |
SEARXNG_QUERY_URL=# where '<query>' will be replaced with query keywords see https://docs.searxng.org/dev/search_api.html eg https://searxng.yourdomain.com/search?q=<query>&engines=duckduckgo,google&format=json
|
29 |
|
|
|
24 |
SERPER_API_KEY=#your serper.dev api key here
|
25 |
SERPAPI_KEY=#your serpapi key here
|
26 |
SERPSTACK_API_KEY=#your serpstack api key here
|
27 |
+
SEARCHAPI_KEY=#your searchapi api key here
|
28 |
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
|
29 |
SEARXNG_QUERY_URL=# where '<query>' will be replaced with query keywords see https://docs.searxng.org/dev/search_api.html eg https://searxng.yourdomain.com/search?q=<query>&engines=duckduckgo,google&format=json
|
30 |
|
README.md
CHANGED
@@ -245,7 +245,7 @@ PUBLIC_APP_DISCLAIMER=
|
|
245 |
|
246 |
### Web Search config
|
247 |
|
248 |
-
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`.
|
249 |
|
250 |
You can also simply enable the local google websearch by setting `USE_LOCAL_WEBSEARCH=true` in your `.env.local` or specify a SearXNG instance by adding the query URL to `SEARXNG_QUERY_URL`.
|
251 |
|
|
|
245 |
|
246 |
### Web Search config
|
247 |
|
248 |
+
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/)) or `SEARCHAPI_KEY` ([searchapi.io](https://www.searchapi.io/)) to your `.env.local`.
|
249 |
|
250 |
You can also simply enable the local google websearch by setting `USE_LOCAL_WEBSEARCH=true` in your `.env.local` or specify a SearXNG instance by adding the query URL to `SEARXNG_QUERY_URL`.
|
251 |
|
docs/source/configuration/web-search.md
CHANGED
@@ -36,6 +36,7 @@ YDC_API_KEY=docs.you.com api key here
|
|
36 |
SERPER_API_KEY=serper.dev api key here
|
37 |
SERPAPI_KEY=serpapi key here
|
38 |
SERPSTACK_API_KEY=serpstack api key here
|
|
|
39 |
```
|
40 |
|
41 |
## Block/Allow List
|
|
|
36 |
SERPER_API_KEY=serper.dev api key here
|
37 |
SERPAPI_KEY=serpapi key here
|
38 |
SERPSTACK_API_KEY=serpstack api key here
|
39 |
+
SEARCHAPI_KEY=searchapi api key here
|
40 |
```
|
41 |
|
42 |
## Block/Allow List
|
src/lib/server/websearch/search/endpoints.ts
CHANGED
@@ -6,6 +6,7 @@ import searchSerpStack from "./endpoints/serpStack";
|
|
6 |
import searchYouApi from "./endpoints/youApi";
|
7 |
import searchWebLocal from "./endpoints/webLocal";
|
8 |
import searchSearxng from "./endpoints/searxng";
|
|
|
9 |
|
10 |
export function getWebSearchProvider() {
|
11 |
if (env.YDC_API_KEY) return WebSearchProvider.YOU;
|
@@ -21,7 +22,8 @@ export async function searchWeb(query: string): Promise<WebSearchSource[]> {
|
|
21 |
if (env.YDC_API_KEY) return searchYouApi(query);
|
22 |
if (env.SERPAPI_KEY) return searchSerpApi(query);
|
23 |
if (env.SERPSTACK_API_KEY) return searchSerpStack(query);
|
|
|
24 |
throw new Error(
|
25 |
-
"No configuration found for web search. Please set USE_LOCAL_WEBSEARCH, SEARXNG_QUERY_URL, SERPER_API_KEY, YDC_API_KEY, or
|
26 |
);
|
27 |
}
|
|
|
6 |
import searchYouApi from "./endpoints/youApi";
|
7 |
import searchWebLocal from "./endpoints/webLocal";
|
8 |
import searchSearxng from "./endpoints/searxng";
|
9 |
+
import searchSearchApi from "./endpoints/searchApi";
|
10 |
|
11 |
export function getWebSearchProvider() {
|
12 |
if (env.YDC_API_KEY) return WebSearchProvider.YOU;
|
|
|
22 |
if (env.YDC_API_KEY) return searchYouApi(query);
|
23 |
if (env.SERPAPI_KEY) return searchSerpApi(query);
|
24 |
if (env.SERPSTACK_API_KEY) return searchSerpStack(query);
|
25 |
+
if (env.SEARCHAPI_KEY) return searchSearchApi(query);
|
26 |
throw new Error(
|
27 |
+
"No configuration found for web search. Please set USE_LOCAL_WEBSEARCH, SEARXNG_QUERY_URL, SERPER_API_KEY, YDC_API_KEY, SERPSTACK_API_KEY, or SEARCHAPI_KEY in your environment variables."
|
28 |
);
|
29 |
}
|
src/lib/server/websearch/search/endpoints/searchApi.ts
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { env } from "$env/dynamic/private";
|
2 |
+
import type { WebSearchSource } from "$lib/types/WebSearch";
|
3 |
+
|
4 |
+
export default async function search(query: string): Promise<WebSearchSource[]> {
|
5 |
+
const response = await fetch(
|
6 |
+
`https://www.searchapi.io/api/v1/search?engine=google&hl=en&gl=us&q=${query}`,
|
7 |
+
{
|
8 |
+
method: "GET",
|
9 |
+
headers: {
|
10 |
+
Authorization: `Bearer ${env.SEARCHAPI_KEY}`,
|
11 |
+
"Content-type": "application/json",
|
12 |
+
},
|
13 |
+
}
|
14 |
+
);
|
15 |
+
|
16 |
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
17 |
+
const data = (await response.json()) as Record<string, any>;
|
18 |
+
|
19 |
+
if (!response.ok) {
|
20 |
+
throw new Error(
|
21 |
+
data["message"] ?? `SearchApi returned error code ${response.status} - ${response.statusText}`
|
22 |
+
);
|
23 |
+
}
|
24 |
+
|
25 |
+
return data["organic_results"] ?? [];
|
26 |
+
}
|
src/routes/+layout.server.ts
CHANGED
@@ -132,6 +132,7 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
|
|
132 |
env.SERPAPI_KEY ||
|
133 |
env.SERPER_API_KEY ||
|
134 |
env.SERPSTACK_API_KEY ||
|
|
|
135 |
env.YDC_API_KEY ||
|
136 |
env.USE_LOCAL_WEBSEARCH ||
|
137 |
env.SEARXNG_QUERY_URL
|
|
|
132 |
env.SERPAPI_KEY ||
|
133 |
env.SERPER_API_KEY ||
|
134 |
env.SERPSTACK_API_KEY ||
|
135 |
+
env.SEARCHAPI_KEY ||
|
136 |
env.YDC_API_KEY ||
|
137 |
env.USE_LOCAL_WEBSEARCH ||
|
138 |
env.SEARXNG_QUERY_URL
|