Skip to content

Commit

Permalink
Add support for SearchApi API (#1196)
Browse files Browse the repository at this point in the history
* Add SearchApi integration

* Remove isURL
  • Loading branch information
SebastjanPrachovskij authored May 30, 2024
1 parent 674cc15 commit 7f44ba0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ YDC_API_KEY=#your docs.you.com api key here
SERPER_API_KEY=#your serper.dev api key here
SERPAPI_KEY=#your serpapi key here
SERPSTACK_API_KEY=#your serpstack api key here
SEARCHAPI_KEY=#your searchapi api key here
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ PUBLIC_APP_DISCLAIMER=

### Web Search config

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`.
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`.

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`.

Expand Down
1 change: 1 addition & 0 deletions docs/source/configuration/web-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ YDC_API_KEY=docs.you.com api key here
SERPER_API_KEY=serper.dev api key here
SERPAPI_KEY=serpapi key here
SERPSTACK_API_KEY=serpstack api key here
SEARCHAPI_KEY=searchapi api key here
```

## Block/Allow List
Expand Down
4 changes: 3 additions & 1 deletion src/lib/server/websearch/search/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import searchSerpStack from "./endpoints/serpStack";
import searchYouApi from "./endpoints/youApi";
import searchWebLocal from "./endpoints/webLocal";
import searchSearxng from "./endpoints/searxng";
import searchSearchApi from "./endpoints/searchApi";

export function getWebSearchProvider() {
if (env.YDC_API_KEY) return WebSearchProvider.YOU;
Expand All @@ -21,7 +22,8 @@ export async function searchWeb(query: string): Promise<WebSearchSource[]> {
if (env.YDC_API_KEY) return searchYouApi(query);
if (env.SERPAPI_KEY) return searchSerpApi(query);
if (env.SERPSTACK_API_KEY) return searchSerpStack(query);
if (env.SEARCHAPI_KEY) return searchSearchApi(query);
throw new Error(
"No configuration found for web search. Please set USE_LOCAL_WEBSEARCH, SEARXNG_QUERY_URL, SERPER_API_KEY, YDC_API_KEY, or SERPSTACK_API_KEY in your environment variables."
"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."
);
}
26 changes: 26 additions & 0 deletions src/lib/server/websearch/search/endpoints/searchApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { env } from "$env/dynamic/private";
import type { WebSearchSource } from "$lib/types/WebSearch";

export default async function search(query: string): Promise<WebSearchSource[]> {
const response = await fetch(
`https://www.searchapi.io/api/v1/search?engine=google&hl=en&gl=us&q=${query}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${env.SEARCHAPI_KEY}`,
"Content-type": "application/json",
},
}
);

/* eslint-disable @typescript-eslint/no-explicit-any */
const data = (await response.json()) as Record<string, any>;

if (!response.ok) {
throw new Error(
data["message"] ?? `SearchApi returned error code ${response.status} - ${response.statusText}`
);
}

return data["organic_results"] ?? [];
}
1 change: 1 addition & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
env.SERPAPI_KEY ||
env.SERPER_API_KEY ||
env.SERPSTACK_API_KEY ||
env.SEARCHAPI_KEY ||
env.YDC_API_KEY ||
env.USE_LOCAL_WEBSEARCH ||
env.SEARXNG_QUERY_URL
Expand Down

0 comments on commit 7f44ba0

Please sign in to comment.