Nodejs returns 401 error (running locally) [Solved]

require('dotenv').config();

const { Client, GatewayIntents, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});

const got = require('got');

client.on('messageCreate', async (message) => {
  try {
    if (message.author.bot) return;

    const url = 'https://api.openai.com/v1/engines/davinci/completions';
    const prompt = `User: ${message.content}\n`;
    const params = {
      prompt: prompt,
      max_tokens: 100,
      temperature: 0.7,
    };
    const headers = {
      'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
      'Content-Type': 'application/json',
    };

    const response = await got.post(url, { json: params, headers: headers }).json();
    const reply = response.choices[0].text.trim();
    
    message.reply(reply);
  } catch (err) {
    console.error(err);
  }
});

client.login(process.env.DISCORD_TOKEN);
console.log('Discord bot is online.');

here is the new code

HTTPError: Response code 429 (Too Many Requests)
    at Request.<anonymous> (D:\ChatGBT-discord-bot\node_modules\got\dist\source\as-promise\index.js:118:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1685482297320,
    socket: 1685482297321,
    lookup: 1685482297345,
    connect: 1685482297379,
    secureConnect: 1685482297415,
    upload: 1685482297415,
    response: 1685482297578,
    end: 1685482297580,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 1,
      dns: 24,
      tcp: 34,
      tls: 36,
      request: 0,
      firstByte: 163,
      download: 2,
      total: 260
    }
  }
}

here is the new error

Well at least im not getting a Key error anymore but now im getting error code 429 i tried adding a limiter but that hasnt gotten me too far either. Any new suggestions?

Are you still getting that error? How busy is the Discord?

429 errors just happen. If you have a trial account you’ll hit more 429 errors then normal.

I having the same issue. I have tried with different API keys, but continue to get 401 error. Any help will be appreciated. Here is my code

const axios = require("axios");

require("dotenv").config();

const apiKey = process.env.OPENAI_API_KEY;


const prompt = 'Translate the following English text to French: "{Good Morning ! How are you doing today ?}"';

const data = {
  'prompt': prompt,
  'max_tokens': 60
};

axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', data, {
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  }
}).then(response => {
  console.log(response.data.choices[0].text);
}).catch(error => {
  console.error(error);
});

Got it ! Its the URL ! The correct URL should be

https://api.openai.com/v1/engines/text-davinci-003/completions

Lesson : DO NOT trust the code generated / suggested by ChatGPT !