Getting an error while deploying expressjs and open ai api

getting an error while hosting it not when using it locally

Hey, welcome to the forum.

Probably not a good idea to share pieces of your API key as it’s against Terms of Service.

What error are you getting?


error 401 Unauthorized token
it works locally but as i deploy on the server it just dont work then it also dont work locally and i have to create a new api key

Can you post more of your code?

What model are you trying to access?

yes i am trying to get text-davinci-003 and dalle


import { Configuration, OpenAIApi } from "openai";
import config from "config";

export const createProduct = async (req, res) => {
  const configuration = new Configuration({
    apiKey: config.get("OPENAI_SECRET"),
  const openai = new OpenAIApi(configuration);
  const { number, theme, ingredients, dish } = req.body;
  const imageSize = `512x512`;
  const textPrompt = `create an image on the following ${dish}`;
  try {
    const response = await openai.createImage({
      prompt: imagePrompt,
      n: 2,
      size: imageSize,

    const description = await openai.createCompletion({
      model: "text-davinci-003",
      prompt: textPrompt,
      temperature: 0.8,
      max_tokens: 60,
      top_p: 1,
      frequency_penalty: 0.5,
      presence_penalty: 0,

    const imageUrl = response;
    const data = {

      success: true,
      data: data,
  } catch (error) {
    if (error) {

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

dont tell me thats it is an error with api key i created and it works on the first try locally but after deploying it to or using vercel(NEXTjs) still the same error

Does it not work beyond the first try locally?

Error Code 401 - Invalid Authentication

This error message indicates that your authentication credentials are invalid. This could happen for several reasons, such as:

  • You are using a revoked API key.

  • You are using a different API key than one under the requesting organization.

  • You are using an API key that does not have the required permissions for the endpoint you are calling.

To resolve this error, please follow these steps:

  • Check that you are using the correct API key and organization ID in your request header. You can find your API key and organization ID in your account settings here.

  • If you are unsure whether your API key is valid, you can generate a new one here. Make sure to replace your old API key with the new one in your requests and follow our best practices.

i told you its not an api key error maybe an error with the nodejs wrapper for openai

Just trying to help you brainstorm a solution…

That is probably related to the problem…

It’s the official library, yes?

  const configuration = new Configuration({
    apiKey: config.get("OPENAI_SECRET"),


const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,

Maybe it can’t get the key from the env on your server?

Good luck!

even doing it directly like:

const configuration = new Configuration({

wont do and havent i showned the error where you said exposing you secret key is against terms

The key works locally. Then doesn’t when you deploy. Does it still work locally after that point or is it “burned”… ?

Have you tried reaching out to support? I’m not seeing anything. Sorry.

it works until i send an request to the deployed server then the api key neither works locally nor hosted

Send a request “to the deployed server” or “from deployed server”?

Sounds like the key is getting burned… Have you contacted support?

why the key gets burned on deploying not locally even i used it for 3 days? it worked for 3 days when i was testing locally but when i deployed it to vercel,( then after sending an request from my react app i get an error and then when i test the same key locally it doesnt work but when i create a new key it works locally and the cycle repeats

Well, this hints that it’s not your code and something to do with how you’re deploying it.

Have you hooked up a credit card and everything to your account?

Playground and everything else working okay?

i used the free credits but then i created another account. it worked with the first account but the credits were used so i created another one and from here i am getting tjis error

Fixed the issue actually i was commitng the open ai secret in the github repo

@amrani.mahaveer223 I have the same issue. I haven’t deployed my project from github. I used Heroku CLI and frontend I deployed in Netlify. still same problem that you had. I am not sharing my code because it is similar to yours.

Check your gmail i had this error because i was pushing the open api key to github