Claude3 API help requested (javascript)
Hey Team I really need some coding help. Specifically does anyone know how to use fetchURL to call the Anthropic API ?? I have this code (below) that I use in a Google Apps/Ads Script for OpenAI & would love to be able to offer users the option of using Claude3 (& Gemini) instead of just OpenAI The 'app' such that it is, is a Google Sheet that has a tab for settings & the output (the script runs inside a google ads account - but doesn't actually pull data from that account - it pulls it from another sheet in the settings) I'm really just using google ads as my environment to run the code! I wanted to show marketers how easy it can be to put these AI building blocks together! @Brandon Hancock actually maybe that's a YT idea for you??? To create API calls from inside a google apps script, so people can call these models using spreadsheets (which they're more familiar with)?? Any help very happily received! Thanks Mike PS - posting screenshot of settings tab as it's the closest thing to an 'app' I have! ----- Code: (existing OpenAI API call inside a function) function generateTextOpenAI(prompt, data, apiKey, model) { Logger.log('Generating report with OpenAI'); let url = 'https://api.openai.com/v1/chat/completions'; let messages = [ { "role": "user", "content": prompt } ]; let payload = { "model": model, "messages": messages }; let httpOptions = { "method": "POST", "muteHttpExceptions": true, "contentType": "application/json", "headers": { "Authorization": 'Bearer ' + apiKey }, 'payload': JSON.stringify(payload) }; let response = UrlFetchApp.fetch(url, httpOptions); let startTime = Date.now(); while (response.getResponseCode() !== 200 && Date.now() - startTime < 30000) { Utilities.sleep(5000); response = UrlFetchApp.fetch(url, httpOptions); Logger.log('Time elapsed: ' + (Date.now() - startTime) / 1000 + ' seconds');