If you are trying to use Gemini with an OpenAI-compatible client, the most important detail is the base URL.
For direct Google Gemini API access, the official OpenAI-compatible base URL is:
https://generativelanguage.googleapis.com/v1beta/openai/
For Gemini access through Flatkey’s OpenAI-compatible router, the base URL is:
https://router.flatkey.ai/v1
Both approaches can keep your app close to the OpenAI SDK shape, but they solve different problems. The direct Google endpoint is the shortest path when you only need Gemini. A router path is useful when your team wants Gemini, GPT, Claude, DeepSeek, Qwen, and other models behind one gateway, one key workflow, one usage log, and one billing surface.
This guide explains the official Gemini OpenAI-compatible endpoint, how to configure it in Python, JavaScript, and REST, when to use Flatkey instead, and what to test before sending production traffic.
Official Gemini OpenAI-Compatible Base URL
Google’s Gemini API documentation confirms that Gemini models can be accessed through OpenAI Python and JavaScript libraries, plus REST, by changing the API key, base URL, and model.
The direct Gemini OpenAI-compatible base URL is:
https://generativelanguage.googleapis.com/v1beta/openai/
In Python, the OpenAI SDK parameter is base_url. In JavaScript, it is baseURL. For REST calls, the full chat completions endpoint becomes:
https://generativelanguage.googleapis.com/v1beta/openai/chat/completions
That distinction matters because many configuration issues come from mixing SDK base URLs with full REST endpoints. If your SDK asks for base_url or baseURL, use the base URL. If you are making a raw HTTP request, use the full endpoint path.
Direct Gemini Endpoint vs Flatkey Router
The two setup paths look similar in code, but they are different operationally.
| Setup choice | Base URL | API key | Best for |
|---|---|---|---|
| Direct Google Gemini API | https://generativelanguage.googleapis.com/v1beta/openai/ |
Gemini API key from Google AI Studio | Apps that only need Gemini and want provider-native account control |
| Gemini through Flatkey | https://router.flatkey.ai/v1 |
Flatkey API key | Teams that want Gemini alongside other model providers behind one OpenAI-compatible gateway |
Use the direct Google endpoint when the goal is simple: call Gemini with an OpenAI-compatible client and manage everything inside Google’s developer environment.
Use Flatkey when the API call is only part of a larger routing workflow. If your application already switches between providers, tracks usage across models, manages quotas, or needs a clean rollback path, a router can reduce the amount of provider-specific logic inside the app.
Direct Gemini API Setup With OpenAI SDK
If you are calling Google directly, use your Gemini API key and Google’s OpenAI-compatible base URL.
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-3.5-flash",
messages=[
{
"role": "user",
"content": "Reply with one sentence confirming the Gemini route works."
}
],
)
print(response.choices[0].message.content)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.GEMINI_API_KEY,
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
});
const response = await openai.chat.completions.create({
model: "gemini-3.5-flash",
messages: [
{
role: "user",
content: "Reply with one sentence confirming the Gemini route works.",
},
],
});
console.log(response.choices[0].message.content);
REST
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GEMINI_API_KEY" \
-d '{
"model": "gemini-3.5-flash",
"messages": [
{
"role": "user",
"content": "Reply with one sentence confirming the Gemini route works."
}
]
}'
Model names can change as Google updates Gemini. Before production use, confirm the current model ID in the official Gemini API documentation or your Google AI Studio environment.
Gemini Through Flatkey Router
Flatkey keeps the OpenAI-compatible client pattern, but points your requests to Flatkey’s router instead of Google’s direct endpoint.
The Flatkey router base URL is:
https://router.flatkey.ai/v1
In this setup, you use a Flatkey API key and choose a Gemini model ID from the Flatkey catalog or dashboard. Do not assume that every Google model string is available under the exact same name through a router. Confirm the model ID before testing.
Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["FLATKEY_API_KEY"],
base_url=os.environ.get("OPENAI_BASE_URL", "https://router.flatkey.ai/v1"),
)
response = client.chat.completions.create(
model=os.environ["FLATKEY_GEMINI_MODEL"],
messages=[
{
"role": "user",
"content": "Reply with one sentence confirming the Flatkey Gemini route works.",
}
],
)
print(response.choices[0].message.content)
print(response.usage)
JavaScript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.FLATKEY_API_KEY,
baseURL: process.env.OPENAI_BASE_URL || "https://router.flatkey.ai/v1",
});
const response = await client.chat.completions.create({
model: process.env.FLATKEY_GEMINI_MODEL,
messages: [
{
role: "user",
content: "Reply with one sentence confirming the Flatkey Gemini route works.",
},
],
});
console.log(response.choices[0].message.content);
console.log(response.usage);
The code change is small, but the production check is different. With Flatkey, a successful response is not the only signal. You should also confirm that the request appears in Flatkey usage logs with the expected model, status, token usage, and cost.
Which Base URL Should You Use?
If your team only wants to test Gemini and does not need a routing layer, start with Google’s direct endpoint:
https://generativelanguage.googleapis.com/v1beta/openai/
That is the cleanest path for a single-provider Gemini integration.
If your team is already managing multiple model providers, or expects to switch providers by configuration, use the router pattern:
https://router.flatkey.ai/v1
The router approach is less about changing the SDK method and more about simplifying operations. Instead of putting provider-specific keys, URLs, model names, logs, and rollback logic throughout the app, you keep those decisions closer to configuration and routing policy.
A practical rule is this: direct Gemini is best for a focused integration; Flatkey is better when Gemini needs to live inside a broader model access workflow.
Common Base URL Mistakes
The most common error is using the wrong key with the wrong endpoint. A Gemini API key belongs with Google’s direct base URL. A Flatkey key belongs with Flatkey’s router base URL. If those are mixed, the request may fail before the model is even reached.
Another common issue is confusing the base URL with the full REST endpoint. SDK clients normally want the base URL:
https://generativelanguage.googleapis.com/v1beta/openai/
Raw REST calls need the full path:
https://generativelanguage.googleapis.com/v1beta/openai/chat/completions
Teams also run into trouble when they test only a simple chat completion but production uses streaming, tools, image input, embeddings, or file workflows. OpenAI-compatible does not mean every endpoint and parameter behaves identically across providers. Treat compatibility as a request-shape convenience, not a promise of full feature parity.
Feature Testing Before Production
A safe Gemini OpenAI-compatible migration should test more than one successful response.
Start with a non-streaming chat completion and confirm your app can parse the response. Then check the model name, usage object, error shape, and timeout behavior. If your production app uses streaming, function calling, image understanding, embeddings, or structured outputs, test each of those paths separately.
For Flatkey routing, add one extra layer of validation: confirm the request in the Flatkey dashboard. You want to know which model was used, whether the request succeeded, how many tokens were counted, and what cost was recorded. That log is one of the main reasons to use a router in the first place.
Before sending real traffic, keep the previous base URL and model as rollback values. A good migration should be reversible by changing configuration, not by redeploying application code.
Recommended Environment Variables
For direct Gemini access:
GEMINI_API_KEY="your-gemini-api-key"
OPENAI_BASE_URL="https://generativelanguage.googleapis.com/v1beta/openai/"
GEMINI_MODEL="gemini-3.5-flash"
For Flatkey routing:
FLATKEY_API_KEY="your-flatkey-api-key"
OPENAI_BASE_URL="https://router.flatkey.ai/v1"
FLATKEY_GEMINI_MODEL="confirm-this-in-flatkey"
Keeping these values in environment variables makes testing and rollback easier. The application can continue using an OpenAI-compatible client while configuration decides whether requests go to Google directly, Flatkey, or another compatible route.
When Flatkey Adds Value
Flatkey is not needed for every Gemini integration. If you only want to call Gemini directly and manage Google credentials yourself, the official endpoint is enough.
Flatkey becomes more useful when the team’s problem is broader than one model call. For example, a product team may want Gemini for one workflow, GPT for another, Claude for longer writing tasks, and other models for cost-sensitive jobs. Without a router, each provider can bring its own key management, pricing checks, usage logs, and failure patterns.
A router gives the team a single place to manage access, observe usage, compare costs, and change model routes. That does not remove the need to test model behavior, but it can keep provider switching from spreading across the codebase.
FAQ
What is the official Gemini OpenAI-compatible base URL?
The official direct base URL is:
https://generativelanguage.googleapis.com/v1beta/openai/
Use this with a Gemini API key when calling Google directly through an OpenAI-compatible SDK.
Should I use base_url or baseURL?
Use base_url in the Python OpenAI SDK. Use baseURL in the JavaScript OpenAI SDK. Both refer to the same idea: the base URL that tells the client where to send requests.
What is the REST endpoint for Gemini chat completions?
For raw REST requests, use:
https://generativelanguage.googleapis.com/v1beta/openai/chat/completions
Do not use the full REST endpoint as the SDK base URL unless your client specifically asks for a full endpoint path.
What base URL should I use for Gemini through Flatkey?
Use:
https://router.flatkey.ai/v1
Then use a Flatkey API key and a Gemini model ID confirmed in Flatkey’s current catalog or dashboard.
Can I use the same Gemini model ID in Flatkey?
Not automatically. Model names and availability can vary by routing provider and catalog. Confirm the exact Flatkey model ID before production traffic.
Does OpenAI-compatible mean full feature parity?
No. It means supported endpoints can use familiar OpenAI-style request and response shapes. You still need to test the exact features your app uses, especially streaming, tools, vision, embeddings, and file-related workflows.
Bottom Line
If you are looking for the official Gemini OpenAI-compatible endpoint, use Google’s direct base URL:
https://generativelanguage.googleapis.com/v1beta/openai/
If you want Gemini inside a multi-provider routing workflow, use Flatkey’s OpenAI-compatible router:
https://router.flatkey.ai/v1
The right choice depends on what you are trying to simplify. Direct Gemini simplifies a single provider integration. Flatkey simplifies model access across providers, usage visibility, billing checks, and rollback.
Before production, test the exact model, endpoint, request shape, feature path, usage log, and failure behavior. A base URL swap can get the first response working quickly, but a real migration is only done when the team knows where the request went, what it cost, and how to switch back if needed.



