# Integrate Azure OpenAI Service

[Azure OpenAI Service](https://azure.microsoft.com/en-us/products/ai-services/openai-service) provides access to OpenAI models through Azure's enterprise infrastructure. This guide shows how to route traffic to Azure OpenAI through API7 Gateway using the [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md) plugin.

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

* Install [Docker](https://docs.docker.com/get-docker/).

* Install [cURL](https://curl.se/) to send requests to the services for validation.

* Have a running API7 Gateway instance.

* Have an Azure account with an [Azure OpenAI Service resource](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource) deployed.

* Create a [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md) and save it to an environment variable:

  ```
  export API_KEY=your-dashboard-token   # replace with your Dashboard token
  ```

* Replace `{gateway_group_id}` with your gateway group ID. Use `default` if you are following the quickstart.

* If you are following the Admin API examples, create or reuse a service in API7 Gateway. If you do not have one yet, follow [Create or Reuse a Service](https://docs.api7.ai/api7-gateway/ai-gateway/get-started.md#create-or-reuse-a-service), then save its ID to an environment variable:

  ```
  export SERVICE_ID=your-service-id         # replace with your service ID
  ```

## Configure Azure Authentication[​](#configure-azure-authentication "Direct link to Configure Azure Authentication")

Obtain the API key and endpoint from your Azure OpenAI resource. Save the key to an environment variable:

```
export AZ_OPENAI_API_KEY=your-azure-api-key   # replace with your Azure OpenAI API key
```

Azure OpenAI uses the `api-key` header for authentication (not the `Authorization` header used by OpenAI).

## Configure the AI Proxy for Azure[​](#configure-the-ai-proxy-for-azure "Direct link to Configure the AI Proxy for Azure")

Create a route with the [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md) plugin:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/azure-openai-route?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

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

  --data-binary @- <<EOF

  {

    "name": "azure-openai-route",

    "service_id": "$SERVICE_ID",

    "paths": ["/azure-openai"],

    "plugins": {

      "ai-proxy": {

        "provider": "azure-openai",

        "auth": {

          "header": {

            "api-key": "$AZ_OPENAI_API_KEY"

          }

        },

        "options": {

          "model": "gpt-4"

        },

        "override": {

          "endpoint": "https://YOUR-RESOURCE.openai.azure.com/openai/deployments/YOUR-DEPLOYMENT/chat/completions?api-version=2024-10-21"

        }

      }

    }

  }

EOF
```

❶ Set the provider to `azure-openai`.

❷ Attach the Azure API key using the `api-key` header.

❸ Specify the full Azure OpenAI endpoint, including your resource name, deployment name, and API version.

adc.yaml

```
services:

  - name: Azure OpenAI Service

    routes:

      - uris:

          - /azure-openai

        name: azure-openai-route

        plugins:

          ai-proxy:

            provider: azure-openai

            auth:

              header:

                api-key: ${AZ_OPENAI_API_KEY}

            options:

              model: gpt-4

            override:

              endpoint: https://YOUR-RESOURCE.openai.azure.com/openai/deployments/YOUR-DEPLOYMENT/chat/completions?api-version=2024-10-21

    labels:

      docs-example: azure-openai-service
```

❶ Set the provider to `azure-openai`.

❷ Attach the Azure API key using the `api-key` header.

❸ Specify the full Azure OpenAI endpoint, including your resource name, deployment name, and API version.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-openai-service
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-openai-service
```

## Multi-Model Routing across Deployments[​](#multi-model-routing-across-deployments "Direct link to Multi-Model Routing across Deployments")

Use [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md) to route traffic across multiple Azure regions or deployment versions for high availability:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/azure-multi-route?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

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

  --data-binary @- <<EOF

  {

    "service_id": "$SERVICE_ID",

    "name": "azure-multi-route",

    "paths": ["/azure-openai-multi"],

    "plugins": {

      "ai-proxy-multi": {

        "instances": [

          {

            "name": "east-us",

            "provider": "azure-openai",

            "auth": { "header": { "api-key": "$AZ_OPENAI_API_KEY" } },

            "options": { "model": "gpt-4" },

            "override": { "endpoint": "https://my-resource-eastus.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21" },

            "weight": 1,

            "priority": 2

          },

          {

            "name": "west-europe",

            "provider": "azure-openai",

            "auth": { "header": { "api-key": "$AZ_OPENAI_API_KEY_EU" } },

            "options": { "model": "gpt-4" },

            "override": { "endpoint": "https://my-resource-westeu.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21" },

            "weight": 1,

            "priority": 1

          }

        ]

      }

    }

  }

EOF
```

❶ Define the primary instance in the East US region.

❷ Set `priority` to `2` (highest) so this instance is used first.

❸ Set `priority` to `1` for the West Europe instance, which serves as a fallback when East US is unavailable.

adc.yaml

```
services:

  - name: Azure OpenAI Multi-Region

    routes:

      - uris:

          - /azure-openai-multi

        name: azure-multi-route

        plugins:

          ai-proxy-multi:

            instances:

              - name: east-us

                provider: azure-openai

                auth:

                  header:

                    api-key: ${AZ_OPENAI_API_KEY}

                options:

                  model: gpt-4

                override:

                  endpoint: https://my-resource-eastus.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21

                weight: 1

                priority: 2

              - name: west-europe

                provider: azure-openai

                auth:

                  header:

                    api-key: ${AZ_OPENAI_API_KEY_EU}

                options:

                  model: gpt-4

                override:

                  endpoint: https://my-resource-westeu.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21

                weight: 1

                priority: 1

    labels:

      docs-example: azure-openai-multi-region
```

❶ Define the primary instance in the East US region.

❷ Set `priority` to `2` (highest) so this instance is used first.

❸ Set `priority` to `1` for the West Europe instance, which serves as a fallback when East US is unavailable.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-openai-multi-region
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-openai-multi-region
```

For more routing strategies, see [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md).

## Validate the Configuration[​](#validate-the-configuration "Direct link to Validate the Configuration")

Send this request to the multi-model route. To test the single-model route, use `/azure-openai` instead of `/azure-openai-multi`.

```
curl "http://127.0.0.1:9080/azure-openai-multi" -X POST \

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

  -d '{

    "messages": [

      { "role": "system", "content": "You are an AI assistant that helps people find information." },

      { "role": "user", "content": "Write a 50-word introduction for API gateways." }

    ]

  }'
```

You should receive a response similar to the following:

```
{

  "choices": [

    {

      "message": {

        "role": "assistant",

        "content": "API gateways serve as the central entry point for managing, securing, and routing API traffic in modern architectures. They handle authentication, rate limiting, load balancing, and protocol translation, enabling organizations to expose services reliably while maintaining security and observability across distributed systems."

      },

      "index": 0,

      "finish_reason": "stop"

    }

  ],

  "usage": {

    "prompt_tokens": 28,

    "completion_tokens": 45,

    "total_tokens": 73

  }

}
```

To enable streaming responses, set `"stream": true` in the request body. Use the [`proxy-buffering`](https://docs.api7.ai/hub/proxy-buffering.md) plugin to disable NGINX `proxy_buffering` to avoid server-sent events (SSE) being buffered.

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

You have learned how to route traffic to Azure OpenAI through API7 Gateway. See [Azure OpenAI Service REST API reference](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference) to learn more.

* [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md) — Route across Azure regions and providers.
* [RAG Integration](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/retrieval-augmented-generation.md) — Use Azure OpenAI with Azure AI Search for retrieval-augmented generation.
