"to load resource: the server responded with a status of 401 ()"

Today I decided to use the chatGpt API for a mini project in Js, everything was “Ok”, until I went to use console.log() to see if it was reaching the server, I came across the 401 error. is giving this problem is line 14 and in my line 14 has the following code:

await fetch("https://api.openai.com/v1/completions",

await fetch("https://api.openai.com/v1/completions", {

    method: "POST",

    headers:{
       Accept:"application/json", "Content-Type": "applications/json",
       Authorization: "Bearer" + KEY,
    },

    body: JSON.stringify({
        model: "text-davinci-003",
        prompt: pergunta,
        max_tokens: 2048,
        temperature: 2,
    })
    
   });

how can i solve this error ?

You likely need a space after Bearer

    headers:{
       Accept:"application/json", "Content-Type": "applications/json",
       Authorization: "Bearer " + KEY,
    },

It didn’t fix the last error, now it gives the 400 error.When I access the completions file, it says " You didn’t provide an API key…"

Should be applications/json.

Are you sure you are setting KEY correctly with a proper value?

yes i’m bruv

i create a const to put the key . The key are 100% correct bruv

Let me try to recode everything.
and will say something.

Also be sure to look at the full error response, not just the status code. It will generally have a message indicating exactly what is wrong.