serp-chat / src /pages /api /search.js
matt HOFFNER
just search
149f602
raw
history blame
646 Bytes
// pages/api/search.js
export default async function handler(req, res) {
const { searchTerm, startIndex = "1" } = req.query;
if (!searchTerm) {
res.status(400).json({ error: 'Search term is required' });
return;
}
const response = await fetch(
`https://www.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CONTEXT_KEY}&q=${searchTerm}&start=${startIndex}`
);
if (!response.ok) {
res.status(response.status).json({ error: 'Failed to fetch search results' });
return;
}
const data = await response.json();
res.status(200).json(data);
}