NodeJS Azure OpenAI Integration Issues - Configuration Not Working

I’m having trouble switching from regular OpenAI to Azure OpenAI in my Node.js project. Everything worked fine before but now I keep getting authentication errors.

My original working code:

const { Configuration, OpenAIApi } = require('openai');

const myApiKey = process.env.OPENAI_SECRET;
const config = new Configuration({
  apiKey: myApiKey,
});
const client = new OpenAIApi(config);

async function generateResponse(systemPrompt, userInput, modelName) {
  try {
    const result = await client.createChatCompletion({
      model: modelName,
      messages: [
        {
          role: 'system',
          content: systemPrompt
        },
        {
          role: 'user', 
          content: userInput
        }
      ]
    });
    const answer = result.data.choices[0].message.content;
    return answer;
  }
}

Updated Azure version that fails:

const { Configuration, OpenAIApi } = require('openai');

const config = new Configuration({
  apiKey: process.env.AZURE_OPENAI_KEY,
  api_type: "azure",
  api_base: "my_azure_endpoint",
  api_version: "my_api_version",
});
const client = new OpenAIApi(config);

async function generateResponse(systemPrompt, userInput, modelName) {
  try {
    const result = await client.ChatCompletion.create({
      engine: "myDeployment",
      messages: [
        {
          role: 'system',
          content: systemPrompt
        },
        {
          role: 'user',
          content: userInput
        }
      ],
    });
    console.log(result);
    const answer = result.data.choices[0].message;
    console.log(answer);
    return answer;
  }
}

I keep getting 401 unauthorized errors even though my credentials work perfectly in Python. Has anyone successfully configured Azure OpenAI with Node.js? What am I missing in the setup?

had this exact prob last week! you’re mixing openai library versions. createChatCompletion doesn’t exist in newer versions, and azure config syntax changed.

two options: downgrade to [email protected] or upgrade to v4 (completely different syntax tho). also check your azure endpoint url - needs to end with openai.azure.com, not just the base url.

Your method call syntax is wrong. You’re using client.ChatCompletion.create() but it should be client.createChatCompletion() like your working code. For Azure config, ditch the constructor parameters and use environment variables instead. Set these: OPENAI_API_TYPE=azure, OPENAI_API_BASE=your_endpoint, and OPENAI_API_VERSION=2023-05-15. The openai library picks these up automatically from env vars - works way better than passing them to the constructor. Also double-check your endpoint URL format. Should look like https://yourresource.openai.azure.com/ with the trailing slash.

Double-check your API key - Azure uses resource keys, not regular OpenAI account keys. Make sure your engine parameter matches your exact deployment name in Azure portal (not the model name). If you’re still having issues, try switching to @azure/openai instead of the regular openai package - works way better for Azure.

Your Azure config is missing key pieces. How you pass api_type, api_base, and api_version depends on your openai package version. For older versions, set these as environment variables or pass them through the config constructor. I hit the same auth issues. Azure OpenAI needs the full resource endpoint URL with https://. Also check that your API version matches what Azure actually supports - I was using an old version string that failed silently. Double-check your deployment name in the engine parameter too. Azure deployment names are case-sensitive and must match exactly what’s in your Azure portal. That 401 error usually comes from bad endpoint formatting, not a wrong API key.