|
| 1 | +--- |
| 2 | +title: Import OpenAI-Compatible Google Gemini API - Azure API Management |
| 3 | +description: How to import an OpenAI-compatible Google Gemini model as a REST API in Azure API Management and manage a chat completions endpoint |
| 4 | +ms.service: azure-api-management |
| 5 | +author: dlepow |
| 6 | +ms.author: danlep |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 07/06/2025 |
| 9 | +ms.collection: ce-skilling-ai-copilot |
| 10 | +ms.custom: template-how-to |
| 11 | +--- |
| 12 | + |
| 13 | +# Import an OpenAI-compatible Google Gemini API |
| 14 | + |
| 15 | +[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)] |
| 16 | + |
| 17 | +This article shows you how to import an OpenAI-compatible Google Gemini API to access models such as `gemini-2.0-flash`. For these models, Azure API Management can manage an OpenAI-compatible chat completions endpoint. |
| 18 | + |
| 19 | +Learn more about managing AI APIs in API Management: |
| 20 | + |
| 21 | +* [AI gateway capabilities in Azure API Management](genai-gateway-capabilities.md) |
| 22 | +* [Import a language model API](openai-compatible-llm-api.md) |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- An existing API Management instance. [Create one if you haven't already](get-started-create-service-instance.md). |
| 27 | +- An API key for the Gemini API. If you don't have one, create it at [Google AI Studio](https://aistudio.google.com/apikey) and store it in a safe location. |
| 28 | + |
| 29 | + |
| 30 | +## Import an OpenAI-compatible Gemini API using the portal |
| 31 | + |
| 32 | +1. In the [Azure portal](https://portal.azure.com), navigate to your API Management instance. |
| 33 | +1. In the left menu, under **APIs**, select **APIs** > **+ Add API**. |
| 34 | +1. Under **Define a new API**, select **Language Model API**. |
| 35 | + |
| 36 | + :::image type="content" source="media/openai-compatible-llm-api/openai-api.png" alt-text="Screenshot of creating a passthrough language model API in the portal." ::: |
| 37 | +1. On the **Configure API** tab: |
| 38 | + 1. Enter a **Display name** and optional **Description** for the API. |
| 39 | + 1. In **URL**, enter the following base URL from the [Gemini OpenAI compatibility documentation](https://ai.google.dev/gemini-api/docs/openai): |
| 40 | + `https://generativelanguage.googleapis.com/v1beta/openai` |
| 41 | + |
| 42 | + 1. In **Path**, append a path that your API Management instance uses to route requests to the Gemini API endpoints. |
| 43 | + 1. In **Type**, select **Create OpenAI API**. |
| 44 | + 1. In **Access key**, enter the following: |
| 45 | + 1. **Header name**: *Authorization*. |
| 46 | + 1. **Header value (key)**: `Bearer` followed by your API key for the Gemini API. |
| 47 | + |
| 48 | + :::image type="content" source="media/openai-compatible-google-gemini-api/gemini-import.png" alt-text="Screenshot of importing a Gemini LLM API in the portal."::: |
| 49 | + |
| 50 | +1. On the remaining tabs, optionally configure policies to manage token consumption, semantic caching, and AI content safety. For details, see [Import a language model API](openai-compatible-llm-api.md). |
| 51 | +1. Select **Review**. |
| 52 | +1. After settings are validated, select **Create**. |
| 53 | + |
| 54 | +API Management creates the API and configures the following: |
| 55 | + |
| 56 | +* A [backend](backends.md) resource and a [set-backend-service](set-backend-service-policy.md) policy that direct API requests to the Google Gemini endpoint. |
| 57 | +* Access to the LLM backend using the Gemini API key you provided. The key is protected as a secret [named value](api-management-howto-properties.md) in API Management. |
| 58 | +* (optionally) Policies to help you monitor and manage the API. |
| 59 | + |
| 60 | +### Test Gemini model |
| 61 | + |
| 62 | +After importing the API, you can test the chat completions endpoint for the API. |
| 63 | + |
| 64 | +1. Select the API that you created in the previous step. |
| 65 | +1. Select the **Test** tab. |
| 66 | +1. Select the `POST Creates a model response for the given chat conversation` operation, which is a `POST` request to the `/chat/completions` endpoint. |
| 67 | +1. In the **Request body** section, enter the following JSON to specify the model and an example prompt. In this example, the `gemini-2.0-flash` model is used. |
| 68 | + |
| 69 | + ```json |
| 70 | + { |
| 71 | + "model": "gemini-2.0-flash", |
| 72 | + "messages": [ |
| 73 | + { |
| 74 | + "role": "system", |
| 75 | + "content": "You are a helpful assistant" |
| 76 | + }, |
| 77 | + { |
| 78 | + "role": "user", |
| 79 | + "content": "How are you?" |
| 80 | + } |
| 81 | + ], |
| 82 | + "max_tokens": 50 |
| 83 | + } |
| 84 | + ``` |
| 85 | + |
| 86 | + When the test is successful, the backend responds with a successful HTTP response code and some data. Appended to the response is token usage data to help you monitor and manage your language model token consumption. |
| 87 | + |
| 88 | + :::image type="content" source="media/openai-compatible-google-gemini-api/gemini-test.png" alt-text="Screenshot of testing a Gemini LLM API in the portal."::: |
| 89 | + |
| 90 | +## Related content |
| 91 | + |
| 92 | +* [AI gateway capabilities in Azure API Management](genai-gateway-capabilities.md) |
0 commit comments