/ app / src / pages / api / summary.ts
summary.ts
 1  export default async function handler(req, res) {
 2    const { socials, question } = req.body;
 3    let urls = '';
 4    socials.slice(0, 5).forEach(social => {
 5      urls += social.url + ', ';
 6    });
 7  
 8    console.log('URLs:', urls);
 9  
10    try {
11      const response = await fetch('https://api.workflows.tryleap.ai/v1/runs', {
12        method: 'POST',
13        headers: new Headers({
14          'Content-Type': 'application/json',
15          'X-Api-Key': process.env.NEXT_PUBLIC_LEAP_API_KEY || '',
16        }),
17        body: JSON.stringify({
18          workflow_id: "wkf_0CzZBKTedkvVrT",
19          webhook_url: "https://google.com",
20          input: {
21            url: urls,
22            question: question
23          }
24        })
25      });
26  
27      const responseData = await response.json();
28      const workflow_id = responseData.id;
29  
30      console.log('Workflow ID:', workflow_id);
31   
32  
33      let finishedRunning= false;
34      while (!finishedRunning) {
35        const response = await fetch(`https://api.workflows.tryleap.ai/v1/runs/${workflow_id}`, {
36          method: 'GET',
37          headers: new Headers({
38            'X-Api-Key': process.env.LEAP_API_KEY || '',
39          })
40        });
41        const responseData = await response.json();
42        if (responseData.status === 'completed') {
43          finishedRunning = true;
44  
45          const output = responseData.output;
46          console.log('Output:', output);
47          res.status(200).json({ linkedinSummary: output });
48          return;
49        }
50        await new Promise(r => setTimeout(r, 1000));
51      }
52    } catch (error: any) {
53      console.error('Error:', error);
54      res.status(500).json({ error: error.toString() });
55    }
56  }