# Azure OpenAI

[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/) provides Azure-hosted deployments of OpenAI models behind resource-specific endpoints. AISIX lets applications reach those deployments through a single OpenAI-compatible gateway endpoint.

This configuration is for Azure OpenAI deployments that should use AISIX authentication, model allowlists, rate limits, and usage accounting. AISIX can authenticate upstream with a resource API key or an Entra ID client credential.

An Azure OpenAI provider key serves chat completions, embeddings, image generation and editing, audio, realtime sessions, and files, batches, and fine-tuning on Azure's own routes. See [Supported Endpoints](#supported-endpoints).

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* One AISIX setup:

  <!-- -->

  * For AISIX Enterprise or AISIX Cloud, an environment with an attached gateway and a write-scoped admin token. For AISIX Enterprise, follow the [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md). To request AISIX Cloud access, [contact API7](https://api7.ai/contact).
  * For the open-source AISIX gateway, prepare either a local AISIX installation or the Docker setup from the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md). Configure the gateway to load a declarative resources file.

* An Azure OpenAI resource with a deployment.

* Either a resource API key or an Entra ID app registration with `tenant_id`, `client_id`, and `client_secret` granted access to the resource.

* The Azure OpenAI resource host and deployment name.

* `curl` and `jq`.

## Configure with AISIX Enterprise or AISIX Cloud[​](#configure-with-aisix-enterprise-or-aisix-cloud "Direct link to Configure with AISIX Enterprise or AISIX Cloud")

Export the control-plane connection details:

```
# AISIX_CP is the Admin API base URL; include /api and omit a trailing slash

# The local AISIX Enterprise quickstart uses http://localhost:8080/api

export AISIX_CP="YOUR_AISIX_CONTROL_PLANE_API_URL"

export AISIX_TOKEN="YOUR_ADMIN_TOKEN"

export ENV_ID="YOUR_ENVIRONMENT_ID"
```

Create the provider key with the Azure catalog provider. The control plane derives the `azure-openai` adapter, so do not send `adapter`:

```
# Replace with your values

export AZURE_OPENAI_API_KEY="YOUR_AZURE_API_KEY"



PROVIDER_KEY_ID=$(curl -sS -X POST "$AISIX_CP/provider_keys" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "display_name": "azure-prod",

    "provider": "azure",

    "api_key": "'"$AZURE_OPENAI_API_KEY"'",

    "api_base": "https://acme-west.openai.azure.com",

    "allowed_environments": ["'"$ENV_ID"'"]

  }' | jq -r '.provider_key.id')
```

The dashboard's Azure provider form accepts a resource API key. The AISIX Cloud Admin API also accepts the Entra ID credential JSON described in [Use Entra ID Authentication](#use-entra-id-authentication) as the `api_key` value. The API does not accept that credential under `config`.

The control plane checks an Entra ID credential when you create the provider key and when you rotate its credential, and refuses with `400` the values the gateway cannot use, naming the field. `tenant_id` and `client_id` must not contain `/`, `\`, `?`, `#`, `%`, a space, or an ASCII control character, must not contain `..`, and must not be made only of dots. A non-empty `authority_host` must be a string holding an `http://` or `https://` origin with no path, query, or fragment. A plain resource API key is not affected.

Create the model with the Azure deployment name as `model_name`:

```
MODEL_ID=$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/models" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "display_name": "gpt-4o-azure",

    "model_name": "gpt4o-prod",

    "provider_key_id": "'"${PROVIDER_KEY_ID}"'"

  }' | jq -r '.model.id')
```

Create a caller API key that can access the model:

```
AZURE_CALLER_KEY=$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/api_keys" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "display_name": "azure-caller",

    "allowed_models": ["'"${MODEL_ID}"'"]

  }' | jq -r '.plaintext')
```

## Configure with AISIX OSS[​](#configure-with-aisix-oss "Direct link to Configure with AISIX OSS")

Choose the authentication scheme that matches how your Azure OpenAI resource is managed. Both examples below are complete resources files for a new gateway and use the same model and caller-key configuration. For an existing gateway, add the entries from one example to the matching collections in its current [resources file](https://docs.api7.ai/ai-gateway/reference/resources-file.md#apply-resource-examples-safely). Create a missing collection once and preserve unrelated resources.

Choose the caller API key that applications will send to AISIX before configuring either authentication scheme:

```
# Replace with your value

export AZURE_CALLER_KEY="YOUR_CALLER_API_KEY"

export AZURE_CALLER_KEY_SHA256="$(printf '%s' "$AZURE_CALLER_KEY" | sha256sum | cut -d ' ' -f 1)"
```

In both authentication alternatives, `provider` labels the upstream, `adapter` selects Azure OpenAI, and `api_base` points to the Azure OpenAI resource host. AISIX also accepts the bare resource name, such as `acme-west`. The model's `provider_key_id` must match the selected provider key's `id`, and `model_name` is the Azure deployment name rather than the underlying model ID.

A deployment name may contain letters, digits, `.`, `_`, and `-`, and must not be made only of dots. A name with a dot, such as `gpt-4.1`, needs gateway 1.11.0 or later: gateways 1.9 and 1.10 refuse it with `400` on every endpoint without calling Azure. The AISIX Cloud Admin API refuses a `model_name` outside these rules with `400` `INVALID_REQUEST` when a direct or embedding model is saved. On a wildcard alias, it refuses a `model_name` template only when the template's fixed text makes every resolved name invalid; the gateway checks each resolved name.

The caller key's `allowed_models` value must match the model alias. The resources file and the gateway hold only the caller key's SHA-256 digest, read from `AZURE_CALLER_KEY_SHA256`. Provider key secrets follow the credential-handling behavior described in [Provider Keys](https://docs.api7.ai/ai-gateway/models/provider-keys.md#credential-handling).

### Use Resource API Key Authentication[​](#use-resource-api-key-authentication "Direct link to Use Resource API Key Authentication")

Use this option when your Azure OpenAI resource is managed with a resource API key. Export the key so the loader can interpolate it:

```
# Replace with your value

export AZURE_OPENAI_API_KEY="YOUR_AZURE_API_KEY"
```

Use the exported provider key and caller key in the complete resources file:

resources.yaml

```
_format_version: "2"



provider_keys:

  - id: 7e91ffe8-a720-4fe0-b3cd-9e54a0903702

    display_name: azure-prod

    provider: azure

    adapter: azure-openai

    api_key: ${AZURE_OPENAI_API_KEY}

    api_base: https://acme-west.openai.azure.com



models:

  - id: aa7c496f-57f0-413d-a020-5713658331b9

    display_name: gpt-4o-azure

    provider: azure

    model_name: gpt4o-prod

    provider_key_id: 7e91ffe8-a720-4fe0-b3cd-9e54a0903702



api_keys:

  - id: 331499c7-da95-483c-8fcb-38705dc98912

    display_name: azure-caller

    key_hash: ${AZURE_CALLER_KEY_SHA256}

    allowed_models: ["gpt-4o-azure"]
```

The provider key interpolates the Azure OpenAI resource API key from the environment. Never place the literal secret in the file.

### Use Entra ID Authentication[​](#use-entra-id-authentication "Direct link to Use Entra ID Authentication")

Use this option when your Azure OpenAI resource should be accessed through an Entra ID app registration. Export the client credential as a JSON value:

```
# Replace with your values

export AZURE_ENTRA_CREDENTIAL='{"tenant_id":"YOUR_TENANT_ID","client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'
```

Use the exported client credential and caller key in the complete resources file:

resources.yaml

```
_format_version: "2"



provider_keys:

  - id: cc91cb01-188b-490a-a703-65b3c53ca4ac

    display_name: azure-aad-prod

    provider: azure

    adapter: azure-openai

    api_key: ${AZURE_ENTRA_CREDENTIAL}

    api_base: https://acme-west.openai.azure.com



models:

  - id: aa7c496f-57f0-413d-a020-5713658331b9

    display_name: gpt-4o-azure

    provider: azure

    model_name: gpt4o-prod

    provider_key_id: cc91cb01-188b-490a-a703-65b3c53ca4ac



api_keys:

  - id: 331499c7-da95-483c-8fcb-38705dc98912

    display_name: azure-caller

    key_hash: ${AZURE_CALLER_KEY_SHA256}

    allowed_models: ["gpt-4o-azure"]
```

The JSON credential must include `tenant_id`, `client_id`, and `client_secret`. The `client_secret` value is the secret value, not the secret ID. For national or sovereign clouds, add `authority_host` to the JSON credential. Omit it for public Azure. The value must be a bare HTTP(S) origin, such as `https://login.microsoftonline.us`. `tenant_id` and `client_id` must not contain `/`, `\`, `?`, `#`, `%`, a space, or an ASCII control character, must not contain `..`, and must not be made only of dots, because the tenant is part of the token endpoint path. The gateway refuses the same values.

AISIX builds the outbound chat-completions URL from the provider key's `api_base` and the model's `model_name`. Without [`api_version`](#set-the-api-version), the URL is:

```
https://<resource>.openai.azure.com/openai/deployments/<deployment>/chat/completions?api-version=2024-10-21
```

### Validate and Load the Resources[​](#validate-and-load-the-resources "Direct link to Validate and Load the Resources")

If AISIX is installed locally, validate the complete file before loading it:

```
aisix validate --resources resources.yaml
```

After validation, start the gateway with the referenced environment variables in its process environment. Reload an existing gateway only if those variables are already available to the process; otherwise, restart it with the updated environment.

If you use Docker, adapt the validation and startup commands in the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md). Mount this `resources.yaml` file and pass every environment variable it references with `-e` in both commands.

## Verify the Provider Connection[​](#verify-the-provider-connection "Direct link to Verify the Provider Connection")

Export the AISIX gateway origin:

```
# AISIX_PROXY has no trailing slash or endpoint path

# The local quickstarts use http://127.0.0.1:3000

export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"
```

Send a chat-completions request through the AISIX proxy. The request is identical regardless of which upstream authentication scheme the provider key uses.

```
curl -sS -X POST "$AISIX_PROXY/v1/chat/completions" \

  -H "Authorization: Bearer ${AZURE_CALLER_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "gpt-4o-azure",

    "messages": [

      {

        "role": "user",

        "content": "Say hello from Azure OpenAI."

      }

    ]

  }'
```

The gateway returns an OpenAI-compatible response with the caller-facing alias:

```
{

  "id": "cmpl_azure_example",

  "object": "chat.completion",

  "model": "gpt-4o-azure",

  "choices": [

    {

      "index": 0,

      "message": {

        "role": "assistant",

        "content": "Hello from Azure OpenAI!"

      },

      "finish_reason": "stop"

    }

  ],

  "usage": {

    "prompt_tokens": 7,

    "completion_tokens": 5,

    "total_tokens": 12

  }

}
```

For a resource API-key provider key, AISIX sends the Azure `api-key` header. For an Entra ID provider key, AISIX sends `Authorization: Bearer <token>`.

Check Azure OpenAI metrics, logs, or quota usage for the test request. If AISIX returns an upstream authentication error, check the resource API key or Entra ID credential. If it returns an upstream route error, check `api_base`, the deployment name in `model_name`, and the Azure API version supported by your deployment.

## Supported Endpoints[​](#supported-endpoints "Direct link to Supported Endpoints")

Each gateway endpoint below reaches the Azure deployment named by the model's `model_name`. The upstream path is the one used with a dated API version, which is the default; [`api_version`](#set-the-api-version) can select another dated version or the v1 API.

| Gateway endpoint                                                                                                           | Azure route                                                    | Default `api-version` |
| -------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | --------------------- |
| `/v1/chat/completions`, and `/v1/responses` and `/v1/messages` translated to chat completions                              | `/openai/deployments/<deployment>/chat/completions`            | `2024-10-21`          |
| `/v1/embeddings`                                                                                                           | `/openai/deployments/<deployment>/embeddings`                  | `2024-10-21`          |
| `/v1/images/generations`                                                                                                   | `/openai/deployments/<deployment>/images/generations`          | `2025-04-01-preview`  |
| `/v1/images/edits`                                                                                                         | `/openai/deployments/<deployment>/images/edits`                | `2025-04-01-preview`  |
| `/v1/audio/transcriptions`                                                                                                 | `/openai/deployments/<deployment>/audio/transcriptions`        | `2024-10-21`          |
| `/v1/audio/translations`                                                                                                   | `/openai/deployments/<deployment>/audio/translations`          | `2024-10-21`          |
| `/v1/audio/speech`                                                                                                         | `/openai/deployments/<deployment>/audio/speech`                | `2025-04-01-preview`  |
| [`/v1/realtime`](https://docs.api7.ai/ai-gateway/endpoints/realtime.md)                                                    | `/openai/realtime?deployment=<deployment>`                     | `2024-10-01-preview`  |
| [`/v1/files`, `/v1/batches`, `/v1/fine_tuning/jobs`](https://docs.api7.ai/ai-gateway/endpoints/batch-files-fine-tuning.md) | `/openai/files`, `/openai/batches`, `/openai/fine_tuning/jobs` | `2024-10-21`          |

Every route authenticates the same way: a resource API key is sent in the `api-key` header, and an Entra ID credential is exchanged for a token sent as `Authorization: Bearer <token>`.

On chat completions, embeddings, and audio, the request's `model` is set to the deployment name. On image generation and image editing, AISIX removes `model` from the request on the deployment route, because some image models reject a deployment name there. Every other field is forwarded as the client sent it, and the image response is returned as Azure sent it. On files, batches, and fine-tuning, an `api-version` query that the client sends itself replaces the default.

A gateway before 1.11.0 serves only chat completions, the translated Responses and Messages requests, realtime, and files, batches, and fine-tuning on an Azure OpenAI key. It refuses image generation and image editing with `400`, answers embeddings with `501`, and sends audio requests to an OpenAI-style route that Azure rejects. On realtime and on files, batches, and fine-tuning, it sends the provider key's secret as `api-key` as written, so an Entra ID credential does not work there.

### Generate an Image[​](#generate-an-image "Direct link to Generate an Image")

Create a model for an Azure image deployment the same way as the chat model, with the image deployment's name as `model_name`, and allow it on the caller key. This example assumes a model named `gpt-image-azure`:

```
curl -sS -X POST "$AISIX_PROXY/v1/images/generations" \

  -H "Authorization: Bearer ${AZURE_CALLER_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "gpt-image-azure",

    "prompt": "A minimal illustration of an AI gateway",

    "size": "1024x1024"

  }'
```

Without `api_version`, AISIX sends the request to `/openai/deployments/<deployment>/images/generations?api-version=2025-04-01-preview` without the `model` field and returns Azure's image response, such as:

```
{

  "created": 1710000000,

  "data": [

    {

      "b64_json": "iVBORw0KGgo..."

    }

  ]

}
```

Which response fields Azure returns, such as `b64_json` or `url` and a `usage` block, depends on the image model. See [Image Generation](https://docs.api7.ai/ai-gateway/endpoints/image-generation.md) for the endpoint's request rules.

## Set the API Version[​](#set-the-api-version "Direct link to Set the API Version")

`api_version` is an optional provider key field that sets the Azure OpenAI REST API version for every request sent with the key. It applies only to provider keys whose adapter is `azure-openai`. It takes one of these forms:

* **A dated version**, such as `2024-10-21` or `2025-04-01-preview`. Each request stays on its route in [Supported Endpoints](#supported-endpoints), with this value as the `api-version` query parameter.
* **`v1`**. Requests go to Azure's v1 API, `/openai/v1/<operation>`, with no `api-version` query. For the operations addressed to a deployment, the deployment name is sent as the request's `model`, including on image generation and image editing; realtime sessions take it as the `model` query parameter on `/openai/v1/realtime`. Files, batches, and fine-tuning use `/openai/v1/files`, `/openai/v1/batches`, and `/openai/v1/fine_tuning/jobs`, and an `api-version` query from the client is dropped. `preview` and `latest` are accepted as aliases of `v1` and behave identically.

The value must match `^(\d{4}-\d{2}-\d{2}(-preview)?|v1|preview|latest)$`. When it is omitted, each operation uses its own default version, listed in the **Default `api-version`** column above.

A dated value applies to every operation sent with the key, so choose one that every operation you use supports. Realtime sessions, for example, are not available in every dated version. When two operations need different versions, create a separate provider key for each version and point each model at the key it needs.

`api_version` requires gateway 1.11.0 or later. An earlier gateway loads a provider key that sets it, ignores the field, and keeps using each operation's default dated version.

### Set the API Version in AISIX Enterprise or AISIX Cloud[​](#set-the-api-version-in-aisix-enterprise-or-aisix-cloud "Direct link to Set the API Version in AISIX Enterprise or AISIX Cloud")

On the dashboard, open **Provider keys**, then create an Azure OpenAI key with **New provider key** or open an existing one for editing. Enter the version in **API version**. The field appears only on Azure OpenAI keys. Leave it empty to use each operation's default; clearing it on an existing key removes the setting.

The AISIX Cloud Admin API takes the same value as `api_version` when you create or update a provider key. Send `null` in an update to clear it:

```
curl -sS -X PATCH "$AISIX_CP/provider_keys/$PROVIDER_KEY_ID" \

  -H "Authorization: Bearer $AISIX_TOKEN" \

  -H "Content-Type: application/json" \

  -d '{

    "api_version": "2025-04-01-preview"

  }'
```

The Admin API rejects `api_version` with `400` `INVALID_REQUEST` on a provider key whose adapter is not `azure-openai`, and rejects a value that does not match the pattern above.

### Set the API Version in AISIX OSS[​](#set-the-api-version-in-aisix-oss "Direct link to Set the API Version in AISIX OSS")

Add `api_version` to the provider key entry in the resources file:

resources.yaml

```
provider_keys:

  - id: 7e91ffe8-a720-4fe0-b3cd-9e54a0903702

    display_name: azure-prod

    provider: azure

    adapter: azure-openai

    api_key: ${AZURE_OPENAI_API_KEY}

    api_base: https://acme-west.openai.azure.com

    api_version: v1
```

Other adapters ignore the field.

## Serve Microsoft Foundry Decision Models[​](#serve-microsoft-foundry-decision-models "Direct link to Serve Microsoft Foundry Decision Models")

An `azure` provider key also serves Microsoft Foundry's `Microsoft-Decision-1` decision model on the gateway's `/v1/systemone` and `/v1/decisions` endpoints. Set the key's `api_base` to the Foundry resource endpoint and the model's `model_name` to the deployment name. AISIX calls `/providers/microsoft/v1/systemone` under the resource root with the same API key or Entra ID authentication described above. See [Decision Models](https://docs.api7.ai/ai-gateway/endpoints/decision-models.md#microsoft-foundry).

## Prepare for Production[​](#prepare-for-production "Direct link to Prepare for Production")

Without `api_version`, AISIX sends each operation with its default dated version, listed in [Supported Endpoints](#supported-endpoints). Confirm that the versions your requests use are supported by your Azure OpenAI deployments, pin one with [`api_version`](#set-the-api-version) when needed, and track Azure's [API version deprecation schedule](https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation).

A `max_tokens` sent to a reasoning-model deployment, one whose name contains `o1`, `o3`, `o4`, `gpt-5`, or `gpt-6`, reaches Azure OpenAI as `max_completion_tokens`. See [Reasoning Model Token Cap](https://docs.api7.ai/ai-gateway/providers/openai.md#reasoning-model-token-cap) for the full rule and its order relative to `request.param_renames`.

Azure may attach `prompt_filter_results` and `content_filter_results` to successful responses. AISIX accepts these Azure extension fields and returns the standard OpenAI-compatible response to the caller.

AISIX resolves `api_base` the same way for every operation in [Supported Endpoints](#supported-endpoints):

* **The Azure OpenAI resource host**, `https://<resource>.openai.azure.com`. AISIX keeps the resource name and drops any path after the host, so the endpoint shown in the Azure portal, such as `https://<resource>.openai.azure.com/openai/v1`, can be pasted as is. A bare resource name, such as `acme-west`, resolves to the same host.
* **Any other host**, such as a `*.cognitiveservices.azure.com` or `*.services.ai.azure.com` endpoint, an Azure API Management gateway, a corporate proxy, or a private or test endpoint. AISIX uses the URL as written, except that a trailing `/openai` or `/openai/v1` is dropped, because AISIX appends that prefix itself. Any other path is kept in front of the route: with `https://proxy.example.com/azure`, chat completions go to `https://proxy.example.com/azure/openai/deployments/<deployment>/chat/completions`.

AISIX rejects query strings and fragments in `api_base`. User information in the URL (`user:password@`) is accepted and used as written.

A gateway before 1.11.0 keeps a trailing `/openai` or `/openai/v1` on a host other than the resource host, which doubles the prefix, and on realtime and on files, batches, and fine-tuning it uses `api_base` as written even on the resource host.

caution

Credentials embedded in `api_base` are not write-only. In AISIX Enterprise and AISIX Cloud, AISIX Cloud Admin API provider-key detail and update responses return `api_base`, even though `api_key` itself is write-only. Gateway logs and errors can also include the URL as written.

Use URL credentials only when an intermediary requires HTTP Basic authentication. For Azure service authentication, set `api_key` to either the resource API key or the supported Entra ID credential JSON.

## Next Steps[​](#next-steps "Direct link to Next Steps")

You have now connected AISIX to Azure OpenAI and verified the model alias. Continue with these guides:

* [OpenAI](https://docs.api7.ai/ai-gateway/providers/openai.md): configure a model through the OpenAI API instead of an Azure deployment.
* [Model Aliases](https://docs.api7.ai/ai-gateway/models/model-aliases.md): configure routing, retry behavior, or cost metadata for the alias.
* [Provider Compatibility](https://docs.api7.ai/ai-gateway/providers/compatibility.md): review supported proxy endpoints and provider-specific boundaries.
