# Gemini (Google AI Studio)

[Google Gemini](https://ai.google.dev/gemini-api/docs) is a family of multimodal models available through Google AI Studio and Google Cloud Vertex AI. This guide connects the Google AI Studio endpoint to AISIX so applications can call Gemini with gateway-managed credentials, access controls, rate limits, and usage accounting.

This guide uses the Google AI Studio endpoint. To route Gemini through Google Cloud instead, use [Google Vertex AI](https://docs.api7.ai/ai-gateway/providers/google-vertex-ai.md).

## 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.

* A Google AI Studio API key from [Google AI Studio](https://aistudio.google.com/apikey).

* `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 a provider key, model alias, and caller API key for the Gemini-backed chat-completions route.

Because Google AI Studio provides an OpenAI-compatible endpoint, AISIX connects through the `openai` adapter and uses the Google AI Studio API root as `api_base`.

### Create a Provider Key[​](#create-a-provider-key "Direct link to Create a Provider Key")

Create the provider key that stores the Google AI Studio credential and API root, and allow the environment to use it:

```
# Replace with your values

export GEMINI_API_KEY="YOUR_PROVIDER_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": "gemini-prod",

    "provider": "google",

    "api_key": "'"${GEMINI_API_KEY}"'",

    "api_base": "https://generativelanguage.googleapis.com/v1beta/openai",

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

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



echo "${PROVIDER_KEY_ID}"
```

❶ `provider` is `google`, the catalog provider ID for Google AI Studio. The AISIX Cloud Admin API derives the adapter from the catalog provider; the `adapter` field is only accepted on BYO provider keys.

❷ `api_key` stores the Google AI Studio API key. It follows the credential-handling behavior in [Provider Keys](https://docs.api7.ai/ai-gateway/models/provider-keys.md#credential-handling).

❸ `api_base` is optional for this catalog provider, because the AISIX Cloud Admin API fills in this same value when you omit it. Set it explicitly so the upstream root stays visible on the resource. Use the root without a trailing slash; AISIX appends `/chat/completions` to it.

The command captures the returned provider key ID in `PROVIDER_KEY_ID`.

### Create a Model[​](#create-a-model "Direct link to Create a Model")

Create the model alias callers will send in requests:

```
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": "gemini-flash-prod",

    "model_name": "gemini-3.6-flash",

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

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



echo "${MODEL_ID}"
```

❶ `display_name` is the alias callers send in `model`.

❷ `model_name` is the Gemini model ID, for example `gemini-3.6-flash`. Use a stable ID from the [Gemini models](https://ai.google.dev/gemini-api/docs/models) page and review its lifecycle before deploying it; Google retires older stable and preview IDs on published schedules.

❸ `provider_key_id` attaches the alias to the Gemini provider key.

### Create a Caller API Key[​](#create-a-caller-api-key "Direct link to Create a Caller API Key")

Create the caller API key resource that can access the model alias. The server generates the key value and returns the plaintext once in the response:

```
AISIX_API_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": "gemini-app",

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

  }' | jq -r '.plaintext')



echo "${AISIX_API_KEY}"
```

The `allowed_models` value references the model by its ID. Store the plaintext key securely; it is not retrievable later.

The new resources project to the attached gateway automatically.

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

Export the upstream credential and choose the caller API key that applications will send to the gateway:

```
export GEMINI_API_KEY="YOUR_PROVIDER_API_KEY"

export CALLER_API_KEY="YOUR_CALLER_API_KEY"

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

For a new gateway, use this complete resources file. For an existing gateway, merge the entries into its [current file](https://docs.api7.ai/ai-gateway/reference/resources-file.md#apply-resource-examples-safely), preserving its other resources:

resources.yaml

```
_format_version: "2"



provider_keys:

  - id: 9fe2de42-af97-40eb-8589-01c8bb5a1707

    display_name: "gemini-prod"

    provider: "google"

    adapter: "openai"

    api_key: ${GEMINI_API_KEY}

    api_base: "https://generativelanguage.googleapis.com/v1beta/openai"



models:

  - id: a76e5284-84fa-4d48-b82c-d824b8b4814a

    display_name: "gemini-flash-prod"

    provider: "google"

    model_name: "gemini-3.6-flash"

    provider_key_id: 9fe2de42-af97-40eb-8589-01c8bb5a1707



api_keys:

  - id: 7c21f087-1587-4bfd-8855-2b13c17d51dc

    display_name: "gemini-app"

    key_hash: ${CALLER_API_KEY_SHA256}

    allowed_models:

      - "gemini-flash-prod"
```

If AISIX is installed locally, validate the 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. After the resources load, prepare the shared verification request below:

```
export AISIX_API_KEY="$CALLER_API_KEY"
```

## 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:

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

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

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

  -d '{

    "model": "gemini-flash-prod",

    "messages": [

      {

        "role": "user",

        "content": "Say hello from Gemini."

      }

    ]

  }'
```

The gateway returns an OpenAI-compatible response that echoes the caller-facing alias `gemini-flash-prod`. If the request fails, check the provider key `api_key`, `api_base`, and the Gemini model ID in `model_name`.

## Use Gemini Thinking and Tools[​](#use-gemini-thinking-and-tools "Direct link to Use Gemini Thinking and Tools")

Google's OpenAI-compatible chat endpoint accepts `reasoning_effort`. AISIX forwards this and other unmodeled top-level fields unchanged, so callers can use the levels supported by the selected Gemini model:

```
{

  "model": "gemini-flash-prod",

  "messages": [{"role": "user", "content": "Plan a safe database migration."}],

  "reasoning_effort": "medium"

}
```

Gemini-specific controls under `extra_body.google`, such as `thinking_config`, also pass through. Do not send both `reasoning_effort` and Google's thinking-level or thinking-budget control in one request because they configure the same behavior. For `gemini-3.6-flash` and newer models, remove deprecated sampling fields such as `temperature`, `top_p`, and `top_k`; AISIX forwards them rather than removing them for you.

Gemini 3 tool calls carry a required thought signature at `tool_calls[].extra_content.google.thought_signature`. When continuing a function-calling turn on `/v1/chat/completions`, retain the complete assistant `tool_calls` objects and send them back unchanged with the tool results. AISIX preserves those raw objects, but it does not manage application conversation state.

Do not use the `/v1/responses` or translated `/v1/messages` routes for multi-step Gemini 3 tool loops. Those bridges reconstruct portable tool-call fields and do not replay Google's provider-specific thought signatures, so the next Gemini request can fail with a `400` validation error. See [Thought signatures for OpenAI compatibility](https://ai.google.dev/gemini-api/docs/generate-content/thought-signatures#openai).

## Review Endpoint Support[​](#review-endpoint-support "Direct link to Review Endpoint Support")

The Google AI Studio OpenAI-compatible API has more routes than AISIX currently models for the `google` provider. Choose the route according to both layers:

| Route                         | Behavior with a `google` provider key                                                                                                                                                                                                                                                                                                                                                                                |
| ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `/v1/chat/completions`        | Supported, including streaming and multimodal image, audio, or video inputs accepted by the selected model.                                                                                                                                                                                                                                                                                                          |
| `/v1/embeddings`              | Supported with a separate alias for a Gemini embedding model, such as `gemini-embedding-2`.                                                                                                                                                                                                                                                                                                                          |
| `/v1/responses`               | Supported through the chat-based [Responses bridge](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md). It does not call Google's native Interactions API, and fields without a chat equivalent are ignored.                                                                                                                                                                                                |
| `/v1/messages`                | Supported through translation to chat completions. `/v1/messages/count_tokens` is unavailable because it requires an Anthropic-backed model.                                                                                                                                                                                                                                                                         |
| `/v1/completions`             | Not supported by Google's compatibility API, which documents chat completions rather than the legacy prompt-completions route.                                                                                                                                                                                                                                                                                       |
| `/v1/audio/*`                 | Not supported. Send supported audio input through chat completions; Gemini speech generation and the Live API use different native APIs.                                                                                                                                                                                                                                                                             |
| `/v1/images/generations`      | Rejected because the normalized AISIX route accepts only models whose provider is `openai` or whose provider key uses the `azure-openai` adapter (see [Provider Requirement](https://docs.api7.ai/ai-gateway/endpoints/image-generation.md#provider-requirement)), even though Google publishes an OpenAI-compatible image route. Use `/passthrough/google/images/generations` with a current Google image model ID. |
| `/v1/videos`                  | Rejected because `google` is not in the normalized video route's provider allowlist. Use `/passthrough/google/videos` to submit and `/passthrough/google/videos/<id>` to poll Google's OpenAI-compatible video operations.                                                                                                                                                                                           |
| `/v1/rerank`                  | Not supported. The AISIX route's provider allowlist excludes `google`, and Google does not publish a matching rerank route here.                                                                                                                                                                                                                                                                                     |
| `/v1/files` and `/v1/batches` | Not an end-to-end AISIX workflow. Google supports OpenAI-compatible batch creation and status, but file upload and download require its native API outside this catalog route.                                                                                                                                                                                                                                       |
| `/passthrough/google/*`       | Available through a configured [passthrough route](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md) for raw routes under the OpenAI-compatible API base.                                                                                                                                                                                                                                           |

The `/passthrough/google` paths on this page assume a [passthrough route](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md) claiming that prefix, with the catalog `api_base` as its `target_url` and this provider key attached; grant the route name on the caller API key's `allowed_routes`. That target ends in `/v1beta/openai`, so passthrough stays inside Google's OpenAI-compatible API section. It cannot reach sibling native routes such as `models/<model>:generateContent`, `models/<model>:streamGenerateContent`, `interactions`, native file operations, or the Live API. Those native APIs also use a different request shape and API-key header, so do not construct native Gemini paths behind this catalog provider key.

Passthrough forwards request and response bodies without rewriting an AISIX model alias, so send the exact Google model ID. For the request shapes that record token usage, see [Envelope Detection and Usage](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md#envelope-detection-and-token-usage). Recorded passthrough tokens do not advance `tpm` or `tpd` counters, resolve a model cost, or add budget spend; caller-key request-count limits still apply to every call. See [Passthrough Routes](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md).

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

You have now connected AISIX to Gemini through Google AI Studio and verified the model alias. Continue with these guides:

* [Model Aliases](https://docs.api7.ai/ai-gateway/models/model-aliases.md): configure routing, retry behavior, or cost metadata for this alias.
* [Google Vertex AI](https://docs.api7.ai/ai-gateway/providers/google-vertex-ai.md): route Gemini through Google Cloud instead.
* [Provider-Specific Overrides](https://docs.api7.ai/ai-gateway/models/provider-keys.md#configure-provider-specific-overrides): adapt request and response shapes when an upstream API differs from its adapter.
* [Provider Compatibility](https://docs.api7.ai/ai-gateway/providers/compatibility.md): review supported proxy endpoints and provider-specific boundaries.
