Hi! Im writing a website code where i can write in a question and get an answer from the api, i did try it in a folder consisting of a html file, javascript file and a css code. i will provide the code in pictures. Everything works fine until i press submit on the website, and it just changes the url but there is no output and no errors in the console. I just dont know what i should change. I did run it through chat gpt but it says it should be working.
#Api not returning any output in a html code
15 messages · Page 1 of 1 (latest)
just wanted to post an update: i changed a bit of the code and now i have an output but its an error and the error message in the console: {
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://beta.openai.com.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
You shouldn't post your api key (visible in the screenshot) for others to see! People can see it and use it, and you'll be paying for it. I'd reccomend deleting the screenshot immediately (and replacing it with one with the key blurred or crossed out)
Can you clarify what you mean by "it just changes the url"
like its weird, the url changes to whatever the question is
i did update the code
async function getAnswer(event) {
event.preventDefault();
// Get the question
let question = document.getElementById("question").value;
let api_key = "apikey";
// Send the question to the OpenAI API
try {
const response = await fetch('https://api.openai.com/v1/engines/davinci/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': Bearer ${api_key}
},
body: JSON.stringify({
prompt: question,
max_tokens: 2048,
temperature: 0.5,
stop: null
})
});
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status}`);
}
// Get the JSON response
const data = await response.json();
// Extract the answer from the response
let answer = data.choices[0].text;
// Display the answer
let answerEl = document.getElementById("answer");
answerEl.innerHTML = answer;
} catch (err) {
console.log(err);
let answerEl = document.getElementById("answer");
answerEl.innerHTML = "An error occured, please try again later.";
}
}
this is the js code
i mean i could upload it via filezilla so anyone could take a look at it
if that helps
but it simply puts out an error in the output and then in the console, its this error: {
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://beta.openai.com.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}