I am building a webapp which can generate photo by inputting keywords, after I created the file xxx.env with my api key
it show the below messge in my terminal, any idea bros ?

error: {
code: ‘invalid_api_key’,
message: ‘Incorrect API key provided: undefined. You can find your API key at’,
param: null,
type: ‘invalid_request_error’

I struggled with this also until I realised that I wasn’t properly formatting the authoristion bearer heading.

In my case I was using Replit and the entry should have been:


I’d left out the brackets and got the same message you’re getting.

So, check the format of your authorization statement.

1 Like

Yeah, you need to send the key in the header.

What language are you using?

I’m having the same issue, and I’m using Node.js. Key in the header even for generating images (DALL-e2)? @PaulBellow can you help?

1 Like

@iigormata Are you using the openai library for node.js? ( $npm install openai)

If you are, here is a code snippet.

const { Configuration, OpenAIApi } = require(“openai”);

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
const openai = new OpenAIApi(configuration);

const response = await openai.createCompletion({ model: “text-davinci-003”, prompt: “Say this is a test”, temperature: 0, max_tokens: 7, });

This example is for completion, but there are probably methods in the library for images too

If you are not using the library to help, do you want to post your code example with the API key blanked out, and we can have a look

1 Like

@raymonddavey I have that for indeed library for images but I still get error “401” mentioned on this post. I’m using this code:

const { Configuration, OpenAIApi } = require(‘openai’);

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
const openai = new OpenAIApi(configuration);

const generateImage = async (req, res) => {
const { prompt, size } = req.body;

const imageSize =
size === ‘small’ ? ‘256x256’ : size === ‘medium’ ? ‘512x512’ : ‘1024x1024’;

try {
const response = await openai.createImage({
n: 1,
size: imageSize,

const imageUrl =[0].url;

  success: true,
  data: imageUrl,

} catch (error) {
if (error.response) {
} else {

  success: false,
  error: 'The image could not be generated',


module.exports = { generateImage };

My .env:


I’ve tried several KEYs, still doesn’t work.

You code looks OK to me. Things I would try:

Swap the createImage call for a really simple createCompletion call (you could just copy my example)

I read in a couple of posts that people have been having issues with calling DALL-E in the last 24 hours.

If the creatcompletion works, then I would assume it is a DALL-E specific issue.(endpoint down, issues with how I am calling Dall-e specifically etc)

If it doesn’t work, then I would print process.env.OPENAI_API_KEY to my console to be certain that node is seeing the correct environmental settings

I would also check I have the latest version of the openai library (just incase it is calling old endpoints behind the scenes)

I would also print the prompt and size to the screen for the Dall-E version to make sure it is what I expected

1 Like

This would be my guess too…ie it’s not getting the key correctly…

@raymonddavey and @PaulBellow Thank youuu, it worked when I added the print process.env.OPEN_API_KEY and added a new generated API key. Thank you a lot!


hey @iigormata i am having same issue… i have tried printing and generating a new one. but no go.

1 Like