Hey everyone! I’m having trouble with the OpenAI API. I’m trying to send a request but keep getting a 400 error. Here’s what I’ve tried:
let aiCall = new XMLHttpRequest();
const data = {
'model': 'gpt-3.5-turbo',
'prompt': 'Hello, world!'
};
aiCall.open('POST', 'https://api.openai.com/v1/completions');
aiCall.setRequestHeader('Content-Type', 'application/json');
aiCall.setRequestHeader('Authorization', 'Bearer ' + apiKey);
aiCall.send(JSON.stringify(data));
At first, I forgot to actually send the request (oops!). After fixing that, I still got an error saying I didn’t provide an API key. But I’m pretty sure I did! Any ideas what I’m doing wrong?
Update: I figured it out! The problem was how I was sending the payload. Changing aiCall.send()
to aiCall.send(JSON.stringify(data))
did the trick. Hope this helps anyone else who runs into this!