First of all, as @KenWhite suggested, fix the fundamentals. Use the try...catch statement properly as follows:

try {
  // Your code here
} catch (error) {
  console.error(error);
}

Problem

Note: OpenAI NodeJS SDK v4 was released on August 16, 2023, and is a complete rewrite of the SDK. See the v3 to v4 migration guide.

There are a few problems with the code you posted in your question. The solutions to these problems I provide below differ depending on whether you use OpenAI NodeJS SDK v3 or v4.

To check your OpenAI NodeJS SDK version, run the following command:

npm info openai version

Problem 1: Passing an invalid parameter to the API endpoint

You're trying to pass headers as a parameter to the API endpoint, which is not a valid parameter. Remove it.

Solution

You need to set the Bearer token as follows...

• If you have the OpenAI NodeJS SDK v3:

import { Configuration, OpenAIApi } from 'openai';

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

const openai = new OpenAIApi(configuration);

• If you have the OpenAI NodeJS SDK v4:

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

Problem 2: Using the wrong method name

You want to use the gpt-3.5-turbo model (i.e., Chat Completions API). Use the proper method name.

Solution

• If you have the OpenAI NodeJS SDK v3:

  • openai.createCompletion <-- Wrong ✘
  • openai.createChatCompletion <-- Correct (works with the Chat Completions API)

• If you have the OpenAI NodeJS SDK v4:

  • openai.completions.create <-- Wrong ✘
  • openai.chat.completions.create <-- Correct (works with the Chat Completions API)

Problem 3: Using the prompt parameter

You want to use the gpt-3.5-turbo model (i.e., Chat Completions API).

The Chat Completions API uses the messages parameter, while the Completions API uses the prompt parameter.

Solution

Use the messages parameter instead of the prompt parameter.


Final solution

• If you have the OpenAI NodeJS SDK v3, try this:

import { Configuration, OpenAIApi } from 'openai';

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

const openai = new OpenAIApi(configuration);

try {
  const chatCompletion = await openai.createChatCompletion({
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Hello world' }],
    max_tokens: 100,
    n: 1,
    stop: '\n',
    temperature: 1.17,
  });

  console.log(chatCompletion.data.choices[0].message);
} catch (error) {
  console.error(error);
}

• If you have the OpenAI NodeJS SDK v4, try this:

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

try {
  const chatCompletion = await openai.chat.completions.create({
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Hello world' }],
    max_tokens: 100,
    n: 1,
    stop: '\n',
    temperature: 1.17,
  });

  console.log(chatCompletion.choices[0].message);
} catch (error) {
  console.error(error);
}
Answer from Rok Benko on Stack Overflow
🌐
OpenAI Help Center
help.openai.com › en › articles › 6882433-incorrect-api-key-provided
Incorrect API key provided | OpenAI Help Center
Check your API key at https://platform.openai.com/api-keys and verify it with the API key shown in the error message. Sometimes, the error message may include an old or incorrect API key that you no longer use.
🌐
NoteGPT
notegpt.io › blog › openai-api-key-not-working
OpenAI API Key Not Working – Best Solution
If your OpenAI API key is not working, the best solution is to verify the key's validity, ensure correct permissions and connectivity, and reach out to OpenAI support for further assistance.
🌐
OpenAI Developer Community
community.openai.com › api › bugs
API KEY is not working, Everyone please give some solution for this - Bugs - OpenAI Developer Community
April 20, 2024 - Error: One of the parameters is invalid, your OpenAI account needs your attention or OpenAI is currently unavailable. Check autoresponder.ai/gpt-err for help. This message is repeatedly giving auto responder, its secret key is not working. It is repeatedly showing error
🌐
OpenAI Developer Community
community.openai.com › api
Open AI error Key not found - API - OpenAI Developer Community
Hi, I am new to openai and trying to run the example code to run a bot. import os import openai openai.api_key = os.getenv(“APIKEY”) response = openai.Completion.create( engine=“text-davinci-001”, prompt=“Marv is …
Published   February 23, 2022
🌐
OpenAI Developer Community
community.openai.com › api
Issues with invalid api key - API - OpenAI Developer Community
February 20, 2024 - I am trying to make a chatbot for a digital marketing agency. I inserted the right API key and even the document uploaded on replit is visible on the OpenAI platform. But the code says invalid API key please help me with…
🌐
OpenAI Developer Community
community.openai.com › api
API key created but not showing in /api-keys gui - API - OpenAI Developer Community
November 23, 2023 - Hi all, I’ve created 3 API keys of which I am successfully using one, so that confirms that they exist. However they are not being listed in https://platform.openai.com/api-keys. The page continues to say “You currently do not have any API keys” Without them being listed I am unable to manage them such as ‘delete’ Steps to reproduce: create new key newly created key not shown in gui but useable in api I have billing setup and am being charged as expected I have tried the following: clea...
Top answer
1 of 1
2

First of all, as @KenWhite suggested, fix the fundamentals. Use the try...catch statement properly as follows:

try {
  // Your code here
} catch (error) {
  console.error(error);
}

Problem

Note: OpenAI NodeJS SDK v4 was released on August 16, 2023, and is a complete rewrite of the SDK. See the v3 to v4 migration guide.

There are a few problems with the code you posted in your question. The solutions to these problems I provide below differ depending on whether you use OpenAI NodeJS SDK v3 or v4.

To check your OpenAI NodeJS SDK version, run the following command:

npm info openai version

Problem 1: Passing an invalid parameter to the API endpoint

You're trying to pass headers as a parameter to the API endpoint, which is not a valid parameter. Remove it.

Solution

You need to set the Bearer token as follows...

• If you have the OpenAI NodeJS SDK v3:

import { Configuration, OpenAIApi } from 'openai';

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

const openai = new OpenAIApi(configuration);

• If you have the OpenAI NodeJS SDK v4:

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

Problem 2: Using the wrong method name

You want to use the gpt-3.5-turbo model (i.e., Chat Completions API). Use the proper method name.

Solution

• If you have the OpenAI NodeJS SDK v3:

  • openai.createCompletion <-- Wrong ✘
  • openai.createChatCompletion <-- Correct (works with the Chat Completions API)

• If you have the OpenAI NodeJS SDK v4:

  • openai.completions.create <-- Wrong ✘
  • openai.chat.completions.create <-- Correct (works with the Chat Completions API)

Problem 3: Using the prompt parameter

You want to use the gpt-3.5-turbo model (i.e., Chat Completions API).

The Chat Completions API uses the messages parameter, while the Completions API uses the prompt parameter.

Solution

Use the messages parameter instead of the prompt parameter.


Final solution

• If you have the OpenAI NodeJS SDK v3, try this:

import { Configuration, OpenAIApi } from 'openai';

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

const openai = new OpenAIApi(configuration);

try {
  const chatCompletion = await openai.createChatCompletion({
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Hello world' }],
    max_tokens: 100,
    n: 1,
    stop: '\n',
    temperature: 1.17,
  });

  console.log(chatCompletion.data.choices[0].message);
} catch (error) {
  console.error(error);
}

• If you have the OpenAI NodeJS SDK v4, try this:

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

try {
  const chatCompletion = await openai.chat.completions.create({
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Hello world' }],
    max_tokens: 100,
    n: 1,
    stop: '\n',
    temperature: 1.17,
  });

  console.log(chatCompletion.choices[0].message);
} catch (error) {
  console.error(error);
}
🌐
Reddit
reddit.com › r/openai › api key not working
r/OpenAI on Reddit: API Key not working
June 8, 2024 -

Sorry if this is the incorrect sub to post this....

I'm not versed at all in open ai

I'm trying to integrate Open Ai in to Google Docs. I went to open ai site and got an API Key but when I put it in the doc I get a message saying, "Error: Your OpenAI API free trial is expired or inactive. Setting up an OpenAI paid account should help accelerate the activation."

I did put $10 of credit into my open ai account but I'm still receiving the same message. Do I need to signup for a monthly account? Is there a reason why the API Keys are not working even though there are credits on my account?

Find elsewhere
🌐
OpenAI Developer Community
community.openai.com › api
No API KEY provided - API - OpenAI Developer Community
February 18, 2022 - I am trying to fine tune a model. I am in the following line: openai api fine_tunes.create -t -m When I try to run it, I get the following message: No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface.
🌐
Stack Overflow
stackoverflow.com › questions › 75941852 › openai-api-key-works-this-way-but-not-that-way-very-confused
python - OpenAI API Key Works This Way, But Not That Way? Very Confused? - Stack Overflow
It could also be that your API somehow changed after testing the first method, try making sure that your API key is one you were actually given. ... Sign up to request clarification or add additional context in comments.
🌐
OpenAI Developer Community
community.openai.com › api
The API key not working, How to fix this? - API - OpenAI Developer Community
June 3, 2023 - Hye Team, When I generate ther API key for the Humanwritter tools, then a message will be appear that your API key is revoke or expire. Why facing this issue? Thanks
🌐
Wolfram Community
community.wolfram.com › groups › - › m › t › 2989628
OpenAI API Key does not work - Online Technical Discussion Groups—Wolfram Community
Wolfram Community forum discussion about OpenAI API Key does not work. Stay on top of important topics and build connections by joining Wolfram Community groups relevant to your interests.
🌐
The AISURF
theaisurf.com › home › fix openai api key issues in 2026
Fix OpenAI API Key Issues in 2026
January 29, 2025 - The application is still using ... · Roll back recent changes If the error appeared right after a deploy, revert the last change and test again....
🌐
OpenAI Developer Community
community.openai.com › api
OpenAI Error: No API key provided - API - OpenAI Developer Community
December 3, 2022 - Error: No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the ...
🌐
OpenAI Developer Community
community.openai.com › api › bugs
No API keys are generated in my projects - Bugs - OpenAI Developer Community
July 31, 2024 - When attempting to generate a key for a new project I get a infinite loader. No pop ups or errors after 10 mins. I have also created a new project instead of the default project and attempted again with the same results. Attempted in Incogneto mode to remove likelyhood of old session cookies Deleted project and created new project and attempted a new token.
🌐
OpenAI Developer Community
community.openai.com › api
Not working with any API-Key? - API - OpenAI Developer Community
October 23, 2024 - Hello - i am using a simple assistant code - see attached For some reason this code is not working with any API-Key. (i have API-keys from different customers - with one it is working without problems - but with the other API-key it seems that no result is provided - but without any error-message or anything) This is the output with the API-Key which is NOT working: File "D:\DEV\Fiverr2024\TRY\larssegelke\checkHTML2.py", line 62, in resultAnswer = results.data[0].content[0].t...
🌐
GitHub
github.com › microsoft › visual-chatgpt › issues › 53
OpenAI API key: dont work on free account · Issue #53 · ...
December 8, 2022 - Not an issue but to let people like myself not waste 4h of their time downloading 60GiBs of data just to get to the point where, the API key you thought you had on your free account, does not work: Entering new AgentExecutor chain... Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: You exceeded your current quota, please check your plan and billing details..
Published   Mar 10, 2023
🌐
OpenAI
platform.openai.com › docs › guides › error-codes
Error codes | OpenAI API
You are using an API key that does not have the required permissions for the endpoint you are calling.