ESC
Tutorials > AI API Calls
Go into the /app/api/ai folder – any file named route.js is your API endpoint! ✨ 🤖 To have your back in the wild world of AI APIs, we've got a special custom retry logic in place! 🔄 This handles the ups and downs of AI API outages – just for you. 👊
Inside your newly created route.ts file, you can implement an example AI API call like below enjoying the perks of our retry logic doing its magic. ✨
/app/api/ai/gpt/route.ts
1
2 try {
3 // Custom retry for outages in AI APIs
4 axiosRetry(axios, {
5 retryDelay: (retryCount) => {
6 return retryCount * 1;
7 },
8 retries: 15,
9 });
10
11 const res = await axios.post(url, body, options);
12
13 const answer = res.data.choices[0].message.content;
14 const usage = res?.data?.usage;
15
16 console.log(">>> " + answer);
17 console.log(
18 "TOKENS USED: " +
19 usage?.total_tokens +
20 " (prompt: " +
21 usage?.prompt_tokens +
22 " / response: " +
23 usage?.completion_tokens +
24 ")"
25 );
26 console.log("
27");
28
29 return answer;
30 } catch (e) {
31 console.error("GPT Error: " + e?.response?.status, e?.response?.data);
32 return null;
33 }
34