#API Key error in Google Apps Script in only one .gs file

1 messages · Page 1 of 1 (latest)

timid mountain
#

Hey all, I have a series of scripts in a Google Apps Script project attached to a Google Sheet file. They used to all work but at some point one of the scripts started throwing this error:

    "error": {
        "message": "Incorrect API key provided: ${OPENAI*****KEY}. You can find your API key at https://platform.openai.com/accoun... (use muteHttpExceptions option to examine full response)```
The code to interact with the API is the same in all the scripts:
```function callOpenAI(systemMessage, records) {
  return new Promise((resolve, reject) => {
    try {
      const openaiUrl = "https://api.openai.com/v1/chat/completions";
      const request = {
        model: 'gpt-4',
        messages: [systemMessage, { role: "user", content: JSON.stringify(records) }],
        temperature: 0.1,
        max_tokens: 600,
        top_p: 1.0,
        frequency_penalty: 0.0,
        presence_penalty: 0.0
      };
      const scriptProperties = PropertiesService.getScriptProperties();
      const openAIKey = scriptProperties.getProperty('OPENAI_API_KEY');
      const options = {
        method: 'POST',
        payload: JSON.stringify(request),
        contentType: 'application/json',
        headers: {
          'Authorization': `Bearer ${openAIKey}`,
        },
      };
      Logger.log(options.headers);
      const response = UrlFetchApp.fetch(openaiUrl, options);
      const data = JSON.parse(response.getContentText());
      resolve(data.choices[0].message.content.trim());
    } catch (error) {
      Logger.log('Error in callOpenAI:' + error);
      reject(error);
    }
  });
}```

Any ideas? None of the logs in there will run in the broken script either, whereas they all run in the working version.