Request failed status code 401

I’m in the process of finishing up a ChatGPT chat bot and I obtained a new API key for this project. However I’m getting the authentication failed when I try and send a POST request. It’s sends a request but I dont get a response. Let me provide a few more details below:

Error log message:
event - compiled successfully in 29 ms (44 modules)
[ { role: ‘user’, content: ‘HI ava’ } ]
error - Error: Request failed with status code 401
at createError (C:\Users\ericl\myProjects\openai\openai-chatgpt\node_modules\axios\lib\core\createError.js:16:15)
at settle (C:\Users\ericl\myProjects\openai\openai-chatgpt\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (C:\Users\ericl\myProjects\openai\openai-chatgpt\node_modules\axios\lib\adapters\http.js:322:11)
at IncomingMessage.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: ‘XSRF-TOKEN’,
xsrfHeaderName: ‘X-XSRF-TOKEN’,
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: ‘application/json, text/plain, /’,
‘Content-Type’: ‘application/json’,
‘User-Agent’: ‘OpenAI/NodeJS/3.2.1’,
Authorization: ‘Bearer undefined’,
‘Content-Length’: 73
method: ‘post’,
data: ‘{“model”:“gpt-3.5-turbo”,“messages”:[{“role”:“user”,“content”:“HI ava”}]}’,
url: ‘

Code for the POST request:

const response = await fetch(“/api/openAIChat”, {
method: “POST”,
headers: {
“Content-Type”: “application/json”,
body: JSON.stringify({ messages: chatHistory }),

const completion = await openai.createChatCompletion({
model: “gpt-3.5-turbo”,
messages: req.body.messages,