Skip to main content

Gemini (Google AI Studio)

Connect AISIX AI Gateway to Google Gemini through the Google AI Studio OpenAI-compatible endpoint. Applications call Gemini models through the gateway. AISIX keeps the Google AI Studio credential on the gateway side. It authorizes model access with caller API keys and allowlists, and applies the same rate-limit and usage-accounting controls as other model aliases.

Google AI Studio uses the openai adapter with a Google api_base. To route Gemini through Google Cloud instead, use Google Vertex AI.

Prerequisites

The following examples use a self-hosted AISIX gateway. Before configuring the upstream, prepare the following:

Configure the Gemini Upstream

Create a provider key, model alias, and caller API key for the Gemini-backed chat-completions route.

Create a Provider Key

Create the provider key that stores the Google AI Studio credential and API root:

# Replace with your values
export AISIX_ADMIN_KEY="YOUR_ADMIN_KEY"
export GEMINI_API_KEY="YOUR_PROVIDER_API_KEY"

curl -sS -X POST "http://127.0.0.1:3001/admin/v1/provider_keys" \
-H "Authorization: Bearer ${AISIX_ADMIN_KEY}" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"display_name": "gemini-prod",
"provider": "gemini",
"adapter": "openai",
"secret": "${GEMINI_API_KEY}",
"api_base": "https://generativelanguage.googleapis.com/v1beta/openai"
}
EOF

provider is gemini.

adapter is openai, because Google AI Studio accepts OpenAI chat-completions requests.

secret stores the Google AI Studio API key. It follows the credential-handling behavior in Provider Credentials.

api_base is required. Use the root without a trailing slash; AISIX appends /chat/completions to it.

Copy the returned provider key ID.

Create a Model

Create the model alias callers will send in requests:

# Replace with your values
export PROVIDER_KEY_ID="YOUR_PROVIDER_KEY_ID"

curl -sS -X POST "http://127.0.0.1:3001/admin/v1/models" \
-H "Authorization: Bearer ${AISIX_ADMIN_KEY}" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"display_name": "gemini-flash-prod",
"provider": "gemini",
"model_name": "gemini-2.5-flash",
"provider_key_id": "${PROVIDER_KEY_ID}"
}
EOF

display_name is the alias callers send in model.

model_name is the Gemini model ID, for example gemini-2.5-flash or gemini-2.5-pro.

provider_key_id attaches the alias to the Gemini provider key.

Create a Caller API Key

Choose the caller API key value that the application will send to AISIX, then hash it for the admin resource:

# Replace with your values
export AISIX_API_KEY="YOUR_CALLER_API_KEY"

CALLER_KEY_HASH=$(printf '%s' "${AISIX_API_KEY}" | shasum -a 256 | awk '{print $1}')

Create the caller API key resource that can access the model alias:

curl -sS -X POST "http://127.0.0.1:3001/admin/v1/apikeys" \
-H "Authorization: Bearer ${AISIX_ADMIN_KEY}" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"key_hash": "${CALLER_KEY_HASH}",
"allowed_models": ["gemini-flash-prod"]
}
EOF

The allowed_models value must match the model alias you created.

Verify the Provider Connection

Send a chat-completions request through the AISIX proxy:

curl -sS -X POST "http://127.0.0.1:3000/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 secret, api_base, and the Gemini model ID in model_name.

Next Steps

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