Import an Azure OpenAI API as a REST API

APPLIES TO: All API Management tiers

This article shows how to import an Azure OpenAI API into an Azure API Management instance from its OpenAPI specification. After importing the API as a REST API, you can manage and secure it, and publish it to developers.

Prerequisites

  • An existing API Management instance. Create one if you haven't already.

  • Access granted to Azure OpenAI in the desired Azure subscription. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Open an issue on this repo to contact us if you have an issue.

  • An Azure OpenAI resource with a model deployed. For more information about model deployment, see the resource deployment guide.

    Make a note of the deployment ID (name). You'll need it when you test the imported API in API Management.

Download the OpenAPI specification

Download the OpenAPI specification for an endpoint that your model supports. For example, download the OpenAPI specification for the chat completion endpoint of the GPT-35-Turbo and GPT-4 models.

  1. In a text editor, open the specification file that you downloaded.
  2. In the servers element in the specification, substitute the name of your Azure OpenAI resource endpoint for the placeholder values in the specification. The following example servers element is updated with the contoso.openai.azure.com resource endpoint.
    [...]
    "servers": [
        {
          "url": "https://contoso.openai.azure.com/openai",
          "variables": {
            "endpoint": {
              "default": "contoso.openai.azure.com"
            }
          }
        }
      ],
    [...]
    
  3. Make a note of the value of the API version in the specification. You'll need it to test the API. Example: 2023-05-15.

Add OpenAPI specification to API Management

  1. In the Azure portal, navigate to your API Management instance.
  2. In the left menu, select APIs > + Add API.
  3. Under Define a new API, select OpenAPI. Enter a Display name and Name for the API and enter an API URL suffix.
  4. Select Create.

The API is imported and displays operations from the OpenAPI specification.

Test the new API in the portal

Operations can be called directly from the portal, which provides a convenient way for administrators to view and test the operations of an API.

  1. Select the API you created in the previous step.

  2. Select the Test tab.

  3. Select an operation. The page displays fields for query parameters and fields for the headers.

    Note

    In the test console, API Management automatically populates an Ocp-Apim-Subscription-Key header, and configures the subscription key of the built-in all-access subscription. This key enables access to every API in the API Management instance. Optionally display the Ocp-Apim-Subscription-Key header by selecting the "eye" icon next to the HTTP Request.

  4. Depending on the operation, enter query parameter values, header values, or a request body. Select Send.

    When the test is successful, the backend responds with a successful HTTP response code and some data.

    Tip

    By default, the test console sends a request to API Management's CORS proxy, which forwards the request to the API Management instance, which then forwards it to the backend. This proxy uses public IP address 13.91.254.72 and can only reach public endpoints. If you want to send a request directly from the browser to the API Management service, select Bypass CORS proxy. Use this option when you want to use the test console and your API Management gateway is network-isolated or doesn't allow traffic from the CORS proxy.

To debug an API, see Tutorial: Debug your APIs using request tracing.

Important

Authentication to the OpenAI API requires an API key or a managed identity. To configure authentication using API Management policies, see Authenticate and authorize to Azure OpenAI API.