File size: 646 Bytes
d92a0cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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);
  }