"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)

I’m trying to implement ChatGPT bot into my personal website and running into an issue. I am trying to connect the API key to my codebase without hardcoding it and it’s giving me a few issues. One is a certificate issue, another is an API not connecting issue. I have a .env with API_KEY=myAPIkey Below is the main code:

import { config } from “dotenv”
config()

import OpenAI from ‘openai’;

const openai = new OpenAI({
apiKey: process.env.API_KEY // This is also the default, can be omitted
});

const chatCompletion = await openai.chat.completions.create({
model: “gpt-3.5-turbo”,
messages: [{“role”: “user”, “content”: “Hello!”}],
}).then(res => {
console.log(res)
})
console.log(process.env.API_KEY)

I’m also getting the following: “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)

Did you solve it? or still not working? Im having the same problem and I already checked that the api is corect and is being pass in the right way

Just checking you have both updated your openai libraries to the latest version

This is the latest node.js example on git

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"]
});

async function main() {
  const chatCompletion = await openai.chat.completions.create({
    messages: [{ role: 'user', content: 'Say this is a test' }],
    model: 'gpt-3.5-turbo',
  });
}

main();

Take a look if process.env.API_KEY indeed returns the key by printing it out with console.log(process.env.API_KEY).

1 Like