Skip to main content

Version: latest

Integrate Google Gemini

Google Gemini provides access to Google's Gemini models through the AI Studio API. This guide shows how to route traffic to Gemini through API7 Gateway using the ai-proxy plugin.

Prerequisites

  • Install Docker.

  • Install cURL to send requests to the services for validation.

  • Have a running API7 Enterprise Gateway instance.

  • Obtain the Admin API key. Save it to an environment variable:

    export ADMIN_API_KEY=your-admin-api-key   # replace with your API key
  • Obtain the ID of the service you want to configure. Save it to an environment variable:

    export SERVICE_ID=your-service-id         # replace with your service ID

Obtain a Gemini API Key

Generate an API key from Google AI Studio. Save the key to an environment variable:

export GEMINI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxx   # replace with your API key

Configure the AI Proxy for Gemini

Create a route with the ai-proxy plugin:

curl "http://127.0.0.1:7080/apisix/admin/routes?gateway_group_id=default" -X PUT \
-H "X-API-KEY: $ADMIN_API_KEY" \
-d '{
"id": "gemini-route",
"service_id": "$SERVICE_ID",
"paths": ["/gemini"],
"plugins": {
"ai-proxy": {
"provider": "gemini",
"auth": {
"header": {
"Authorization": "Bearer '"$GEMINI_API_KEY"'"
}
},
"options": {
"model": "gemini-2.0-flash"
}
}
}
}'

❶ Set the provider to gemini to use the Google AI Studio API.

❷ Attach the Gemini API key in the Authorization header.

❸ Set the model to gemini-2.0-flash. See the Gemini models page for available models.

note

For enterprise workloads on Google Cloud, consider using Vertex AI instead. Vertex AI provides service account authentication, regional endpoints, and enterprise SLAs.

Validate the Configuration

Send a chat completion request:

curl "http://127.0.0.1:9080/gemini" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "user", "content": "What is the speed of light?" }
]
}'

You should receive a response similar to the following:

{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"model": "gemini-2.0-flash",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The speed of light in a vacuum is approximately 299,792,458 meters per second (about 186,282 miles per second)."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 28,
"total_tokens": 36
}
}

To enable streaming responses, set "stream": true in the request body. Use the proxy-buffering plugin to disable NGINX proxy_buffering to avoid server-sent events (SSE) being buffered.

Next Steps

You have learned how to route traffic to Google Gemini through API7 Gateway. See the Gemini API documentation to learn more about available models and capabilities.

API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation