I’m trying to write this script for automation in Airtable but the console says there’s an error when making the API request to ChatGPT. Can someone help me?
let apiKey = "api key"; // Replace with your ChatGPT API key
let table = base.getTable("Teste");
let query = await table.selectRecordsAsync();
for (let record of query.records) {
let greeting = record.getCellValueAsString("Obrigado");
if (greeting) {
let response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: "gpt-4",
messages: [
{ role: 'user', content: greeting }
]
})
});
if (!response.ok) {
console.error("Error making request to the ChatGPT API");
return;
}
let data = await response.json();
let reply = data.choices[0].message.content;
await table.updateRecordAsync(record, {
"Imagine": reply
});
}
}