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 ?
401
{
error: {
code: ‘invalid_api_key’,
message: ‘Incorrect API key provided: undefined. You can find your API key at https://beta.openai.com .’,
param: null,
type: ‘invalid_request_error’
}
}
2 Likes
gc
January 4, 2023, 4:48pm
2
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:
{{OPEN_API_KEY}}
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({
prompt,
n: 1,
size: imageSize,
});
const imageUrl = response.data.data[0].url;
res.status(200).json({
success: true,
data: imageUrl,
});
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
res.status(400).json({
success: false,
error: 'The image could not be generated',
});
}
};
module.exports = { generateImage };
My .env:
PORT=5002
OPENAI_API_KEY= “xx”
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!
2 Likes
hey @iigormata i am having same issue… i have tried printing and generating a new one. but no go.
1 Like
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({
prompt,
n: 1,
size: imageSize,
});
console.log(response.data.data[0].url);
const imageUrl = response.data.data[0].url;
res.status(200).json({
success: true,
data: imageUrl,
});
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
res.status(400).json({
success: false,
error: “The image could not be generated”,
});
}
};
module.exports = { generateImage }; USe this code and give a prompt in postman and you get image url
my error is
app.js:4
|fetchData|@|app.js:4|
|(anonymous)|@|app.js:22|
{error: {…}}
error:
1. code: “invalid_api_key”
2. message: “”
3. param: null
4. type: “invalid_request_error”
what should I do?
If your using .env files you generally need to use the dot env npm package.
idib19
May 27, 2023, 6:21am
14
Hi guys! I hope everybody is doing well here .
I recently got the same problem as most of you : invalid api key .
The only thing that helped me was setting up my GitHub repository to private instead of public. It was as simple as that ::
If your project is in production phase and the GitHub repository is on public, openAi will automatically detects the exposed key and marks it as invalid.
So, if you’re in a similar situation, I highly recommend changing the visibility of your GitHub repository and generating a fresh API key. It should resolve the “invalid API key” problem once and for all.
Thank you, and happy coding!
1 Like
The correct solution is never add API Keys to Git. They should be stored as environment variables or at least in git-ignored config files.
1 Like