Implementation:Openai Openai node AzureOpenAI
| Knowledge Sources | |
|---|---|
| Domains | SDK, Azure |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The AzureOpenAI class extends the base OpenAI client to provide seamless integration with Azure OpenAI Service endpoints.
Description
AzureOpenAI is a specialized client class that adapts the standard OpenAI SDK for use with Microsoft Azure's hosted OpenAI services. It extends the base OpenAI class and overrides authentication and request building logic to conform to Azure's API conventions, including support for Azure-specific endpoint formatting, API versioning via the api-version query parameter, and deployment-based URL routing.
The constructor accepts Azure-specific configuration through the AzureClientOptions interface, which adds apiVersion, endpoint, deployment, and azureADTokenProvider on top of the standard ClientOptions. It enforces mutual exclusivity between apiKey and azureADTokenProvider, and between baseURL and endpoint. Environment variables (AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, OPENAI_API_VERSION) serve as fallback defaults.
The class overrides buildRequest to automatically inject /deployments/{model} into API paths for supported endpoints (completions, chat completions, embeddings, audio, images, and batches). It also overrides authHeaders to use the api-key header instead of the standard Authorization: Bearer header when a string API key is provided.
Usage
Use AzureOpenAI whenever you need to call OpenAI models hosted on Azure rather than the OpenAI platform directly. This is the primary entry point for Azure-hosted deployments, supporting both API key and Microsoft Entra (Azure AD) token-based authentication.
Code Reference
Source Location
- Repository: openai-node
- File: src/azure.ts
Signature
export interface AzureClientOptions extends ClientOptions {
apiVersion?: string | undefined;
endpoint?: string | undefined;
deployment?: string | undefined;
apiKey?: string | undefined;
azureADTokenProvider?: (() => Promise<string>) | undefined;
}
export class AzureOpenAI extends OpenAI {
deploymentName: string | undefined;
apiVersion: string;
constructor(opts?: AzureClientOptions);
override async buildRequest(
options: FinalRequestOptions,
props?: { retryCount?: number },
): Promise<{ req: RequestInit & { headers: Headers }; url: string; timeout: number }>;
protected override async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined>;
}
Import
import { AzureOpenAI } from 'openai';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| apiVersion | string |
Yes | Azure API version string. Defaults to process.env['OPENAI_API_VERSION'].
|
| endpoint | string |
Conditional | Azure resource endpoint URL, e.g. https://example-resource.azure.openai.com/. Required if baseURL is not set. Defaults to process.env['AZURE_OPENAI_ENDPOINT'].
|
| deployment | string |
No | Model deployment name. When set, routes requests through /deployments/{deployment}.
|
| apiKey | string |
Conditional | Azure API key. Mutually exclusive with azureADTokenProvider. Defaults to process.env['AZURE_OPENAI_API_KEY'].
|
| azureADTokenProvider | () => Promise<string> |
Conditional | Async function returning a Microsoft Entra access token. Mutually exclusive with apiKey.
|
| baseURL | string |
No | Custom base URL. Mutually exclusive with endpoint. Defaults to process.env['OPENAI_BASE_URL'].
|
Outputs
| Name | Type | Description |
|---|---|---|
| (instance) | AzureOpenAI |
An initialized Azure OpenAI client that exposes the same resource methods as the base OpenAI client (chat, completions, embeddings, etc.).
|
| deploymentName | undefined | The configured deployment name, if any. |
| apiVersion | string |
The resolved Azure API version string. |
Usage Examples
import { AzureOpenAI } from 'openai';
// Using API key authentication
const client = new AzureOpenAI({
apiKey: 'my-azure-api-key',
endpoint: 'https://my-resource.openai.azure.com/',
apiVersion: '2024-06-01',
deployment: 'gpt-4o',
});
const completion = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }],
});
// Using Azure AD token provider
const adClient = new AzureOpenAI({
azureADTokenProvider: async () => getAccessToken(),
endpoint: 'https://my-resource.openai.azure.com/',
apiVersion: '2024-06-01',
});