Azure OpenAI
Use Azure-hosted OpenAI and Foundry models with Anvia.
Azure OpenAI in Microsoft Foundry Models exposes OpenAI-compatible chat completions, completions, embeddings, image generation, and audio endpoints at https://{resource}.openai.azure.com/openai/deployments/{deployment}. In Anvia, configure OpenAIClient with that baseUrl, then pass your deployment name to completionModel(...).
Create the Client
import { AgentBuilder } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
const client = new OpenAIClient({
baseUrl:
"https://{your-resource-name}.openai.azure.com/openai/deployments/{your-deployment-name}",
apiKey: process.env.AZURE_OPENAI_API_KEY,
defaultQuery: { "api-version": "2024-10-21" },
});
const model = client.completionModel("{your-deployment-name}");
const agent = new AgentBuilder("support", model)
.instructions("Answer support questions clearly.")
.build();
const response = await agent.prompt("Hello!").send();
console.log(response.output);baseUrl makes Anvia use the OpenAI-compatible chat completion adapter. The model id is your Azure deployment name, not an Anvia-specific alias. Azure requires the api-version query parameter on every request — pass it via defaultQuery on the client.
Microsoft Entra ID Auth
Azure OpenAI also supports Microsoft Entra ID bearer tokens instead of API keys. Issue a token from your Entra app registration or managed identity, then forward it as the bearer token.
import { DefaultAzureCredential } from "@azure/identity";
const credential = new DefaultAzureCredential();
const tokenResponse = await credential.getToken(
"https://cognitiveservices.azure.com/.default",
);
const client = new OpenAIClient({
baseUrl:
"https://{your-resource-name}.openai.azure.com/openai/deployments/{your-deployment-name}",
apiKey: tokenResponse?.token ?? "",
defaultHeaders: { Authorization: `Bearer ${tokenResponse?.token}` },
defaultQuery: { "api-version": "2024-10-21" },
});The api-key header is only used for key-based auth. When you switch to Entra ID, the Authorization: Bearer header takes over and api-key should be omitted.
Get the Model List
Azure OpenAI's model list endpoint reflects deployments in your resource, not the full OpenAI catalog. Because the client was created with baseUrl, listModels() calls Azure's /models endpoint.
const models = await client.listModels();
console.table(
models.data.map((model) => ({
id: model.id,
name: model.name,
contextLength: model.contextLength,
})),
);Use a deployment id from the response directly with completionModel(...).
Notes
- Azure requires the
api-versionquery parameter. Pin a GA version such as2024-10-21for production. Preview versions are released on a monthly cadence and should be used with care. - API key auth uses the
api-keyrequest header (notAuthorization). Microsoft Entra ID auth usesAuthorization: Bearer {token}. - Azure-specific features not part of OpenAI include Azure Search and Azure Cosmos DB data sources in chat completions, content filter results on responses, and system-assigned or user-assigned managed identities.
- The endpoint URL embeds the deployment name in the path. There is no separate
modelparameter in the request body — the deployment determines which model is used. api-versionlifecycle: a new preview inference API is also available. See the API lifecycle guide for the deprecation timeline of older versions.- Model capabilities still depend on the deployed model. Test the specific deployment for tool calling, structured output, streaming, and multimodal support before enabling those features.
For current Azure OpenAI details, see the Azure OpenAI REST API reference and the Azure OpenAI quickstart.
