Proxy Gemini Requests
Google Gemini provides an OpenAI-compatible API that allows you to access Gemini models using the familiar OpenAI API format.
This guide shows how to integrate APISIX with Google Gemini using the ai-proxy plugin. With provider set to gemini, you do not need to set a custom endpoint.
Prerequisite(s)
- Install Docker.
- Install cURL to send requests to the services for validation.
- Follow the Getting Started Tutorial to start a new APISIX instance in Docker or on Kubernetes.
Obtain a Google API Key
Create an account and API key by following the Google AI Studio. Optionally save the key to an environment variable:
export GEMINI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # replace with your API key
Create a Route to Gemini
Create a route with the ai-proxy plugin as such:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '{
"id": "gemini-chat",
"uri": "/anything",
"plugins": {
"ai-proxy": {
"provider": "gemini",
"auth": {
"header": {
"Authorization": "Bearer '"$GEMINI_API_KEY"'"
}
},
"options": {
"model": "gemini-2.5-flash"
}
}
}
}'
❶ Set the provider to gemini.
❷ Attach the Google API key using the Authorization header.
❸ Set a model supported by Gemini, for example gemini-2.5-flash.
services:
- name: Gemini Service
routes:
- uris:
- /anything
name: gemini-chat
plugins:
ai-proxy:
provider: gemini
auth:
header:
Authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
options:
model: gemini-2.5-flash
❶ Set the provider to gemini.
❷ Attach the Google API key using the Authorization header.
❸ Set a model supported by Gemini, for example gemini-2.5-flash.
Synchronize the configuration to APISIX:
adc sync -f adc.yaml
Create a Kubernetes manifest file to configure a route:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: ingress-apisix
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
provider: gemini
auth:
header:
Authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
options:
model: gemini-2.5-flash
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: ingress-apisix
name: gemini-chat
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: ingress-apisix
name: gemini-route
spec:
ingressClassName: apisix
http:
- name: gemini-route
match:
paths:
- /anything
plugins:
- name: ai-proxy
enable: true
config:
provider: gemini
auth:
header:
Authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
options:
model: gemini-2.5-flash
❶ Set the provider to gemini.
❷ Attach the Google API key using the Authorization header.
❸ Set a model supported by Gemini, for example gemini-2.5-flash.
Apply the configuration to your cluster:
kubectl apply -f gemini-route.yaml
Verify
Send a request with the following prompts to the route:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a helpful AI assistant" },
{ "role": "user", "content": "What is the capital of France?" }
]
}'
You should receive a response similar to the following:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The capital of France is **Paris**.",
"role": "assistant"
}
}
],
"model": "gemini-2.5-flash",
"object": "chat.completion",
"usage": {
"completion_tokens": 8,
"prompt_tokens": 15,
"total_tokens": 41
},
...
}
Next Steps
You have learned how to integrate APISIX with Google Gemini. See the Google AI for Developers and Models pages for more details.
If you would like to stream responses, enable streaming in your request and use the proxy-buffering plugin to disable NGINX proxy_buffering to avoid server-sent events (SSE) being buffered.