This repo demonstrates the use of Azure OpenAI GPT models with Responses API and Chat Completions API in JavaScript.
Note
From August 2025, the new naming convention allows you to use the latest API version implicitly. Specifics of the Azure OpenAI models API lifecycle can be found on this AI Foundry documentation page.
- Part 1: Configuring Solution Environment
- Part 2: Client Initialisation
- Part 3: Test of Responses API
- Part 4: Test of Chat Completions API
- Part 5: Test of Conversations API
To run the provided JavaScript demos, you'll need to set up your Azure AI Foundry resource and install required packages.
Ensure you have an Azure AI Foundry resource with a deployed Azure OpenAI model (e.g., gpt-4.1-mini).
The demos utilise Microsoft Entra ID (former Azure AD) for secure authentication. This is done via the DefaultAzureCredential from the @azure/identity package.
Before running the demos, ensure you are logged in:
az loginConfigure the following environment variables. Note the use of the new /openai/v1/ base path suffix to support versionless routing:
| Environment Variable | Description |
|---|---|
| AZURE_OPENAI_API_BASE | The endpoint URL (e.g., https://<AOAI_RESOURCE>.openai.azure.com/openai/v1/). |
| AZURE_OPENAI_API_DEPLOY | The name of your model deployment. |
Install required JS libraries:
npm install openai @azure/identityTo align with the latest Azure OpenAI API developments, these demos implemented new formats:
- Standard OpenAI Class: We use the base
OpenAIclass. - No Explicit API Version: By pointing to the
/openai/v1/path in thebaseURL, the system uses the latest stable API version. - Integrated Token Provider: Authentication is handled by passing the bearer token provider directly into the
apiKeyparameter.
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
const { OpenAI } = require("openai");
// Create token provider for Microsoft Entra ID authentication
const tokenProvider = getBearerTokenProvider(
new DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
);
// Simplified initialization
const client = new OpenAI({
baseURL: process.env.AZURE_OPENAI_API_BASE,
apiKey: tokenProvider,
});The Responses API provides a flexible way to interact with advanced models. The demo-responses.js file uses the client.responses.create method, designed for high-performance interactions where conversation logic managed by the backend.
The following snippet shows a multi-turn conversation where the backend manages history via previous_response_id.
// Define conversation turns
const questions = [
"What is the capital of France?",
"What is its population?",
"Name 3 famous landmarks there."
];
let previousResponseId = null;
for (let i = 0; i < questions.length; i++) {
const requestParams = {
model: process.env.AZURE_OPENAI_API_DEPLOY,
input: questions[i],
};
// Add system instructions only on first turn
if (i === 0) {
requestParams.instructions = "You are a helpful assistant. Keep responses brief.";
}
// Chain to previous response if exists
if (previousResponseId) {
requestParams.previous_response_id = previousResponseId;
}
const response = await client.responses.create(requestParams);
console.log("AI Agent:", response.output_text);
// Store response ID for next turn
previousResponseId = response.id;
}node demo-responses.jsIf setup successfully, you should expect to get a response looking like this:
============================================================
RESPONSES API - Multi-turn Conversation Demo
(History managed on backend via previous_response_id)
============================================================
[Turn 1] User: What is the capital of France?
AI Agent: The capital of France is Paris.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[Turn 2] User: What is its population?
AI Agent: As of the most recent data, the population of Paris is approximately 2.1 million people within the city proper.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[Turn 3] User: Name 3 famous landmarks there.
AI Agent: Three famous landmarks in Paris are:
1. Eiffel Tower
2. Louvre Museum
3. Notre-Dame Cathedral
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββThe Chat Completions API is a legacy conversation-based approach. The demo-chat-completions.js file uses the client.chat.completions.create method, and maintains conversation context across multiple exchanges.
The following snippet shows a multi-turn conversation where history is managed manually via the messages array.
// Define conversation turns
const questions = [
"What is the capital of France?",
"What is its population?",
"Name 3 famous landmarks there."
];
// Initialize messages with system prompt
const messages = [
{ role: "system", content: "You are a helpful assistant. Keep responses brief." }
];
for (let i = 0; i < questions.length; i++) {
// Add user message to history
messages.push({ role: "user", content: questions[i] });
const response = await client.chat.completions.create({
model: process.env.AZURE_OPENAI_API_DEPLOY,
messages: messages,
max_tokens: 150,
});
const answer = response.choices[0].message.content;
// Add assistant response to history for next turn
messages.push({ role: "assistant", content: answer });
console.log("AI Agent:", answer);
}node demo-chat-completions.jsIf setup successfully, you should expect to get a response looking like this:
============================================================
CHAT COMPLETIONS API - Multi-turn Conversation Demo
(History managed manually via messages array)
============================================================
[Turn 1] User: What is the capital of France?
AI Agent: The capital of France is Paris.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[Turn 2] User: What is its population?
AI Agent: As of 2024, the population of Paris is approximately 2.1 million people.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[Turn 3] User: Name 3 famous landmarks there.
AI Agent: Three famous landmarks in Paris are:
1. Eiffel Tower
2. Louvre Museum
3. Notre-Dame Cathedral
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββWarning
At the time of writing in January 2026, Conversations API was not supported in Azure OpenAI yet. If you are interested in this capability, please upvote it here.