Files
firecrawl/apps/api/src/search/serper.ts
T

33 lines
764 B
TypeScript
Raw Normal View History

2024-04-23 16:45:06 -07:00
import axios from "axios";
import dotenv from "dotenv";
2024-04-24 10:11:01 -07:00
import { SearchResult } from "../../src/lib/entities";
2024-04-23 16:45:06 -07:00
dotenv.config();
2024-04-24 10:11:01 -07:00
export async function serper_search(q, num_results): Promise<SearchResult[]> {
2024-04-23 16:45:06 -07:00
let data = JSON.stringify({
q: q,
2024-04-24 10:11:01 -07:00
num: num_results,
2024-04-23 16:45:06 -07:00
});
let config = {
method: "POST",
url: "https://google.serper.dev/search",
headers: {
"X-API-KEY": process.env.SERPER_API_KEY,
"Content-Type": "application/json",
},
data: data,
};
const response = await axios(config);
if (response && response.data && Array.isArray(response.data.organic)) {
2024-04-24 10:11:01 -07:00
return response.data.organic.map((a) => ({
url: a.link,
title: a.title,
description: a.snippet,
}));
}else{
2024-04-23 16:45:06 -07:00
return [];
}
}