Skip to content

Gemini-compatible API

Use this page only when an existing application specifically requires the Gemini format. Otherwise, prefer the OpenAI SDK.

Gemini puts the model ID and action in the URL:

POST https://omrouter.com/v1beta/models/{model}:generateContent

Use the model name shown on the Pricing page. URL-encode the model name before placing it in the path; the Python example below already does this.

Terminal window
curl --silent --show-error \
"https://omrouter.com/v1beta/models/${OMROUTER_MODEL_ID}:generateContent" \
--header "x-goog-api-key: $OMROUTER_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"contents": [
{
"role": "user",
"parts": [
{"text": "Reply with one short sentence."}
]
}
]
}'
Terminal window
python -m pip install requests
example.py
import os
from urllib.parse import quote
import requests
model = quote(os.environ["OMROUTER_MODEL_ID"], safe="")
response = requests.post(
f"https://omrouter.com/v1beta/models/{model}:generateContent",
headers={
"x-goog-api-key": os.environ["OMROUTER_API_KEY"],
"Content-Type": "application/json",
},
json={
"contents": [
{
"role": "user",
"parts": [{"text": "Reply with one short sentence."}],
}
]
},
timeout=120,
)
response.raise_for_status()
print(response.json()["candidates"][0]["content"]["parts"][0]["text"])

Use the x-goog-api-key header. A key query parameter also works, but it puts the API key in the URL where browser history and logs can retain it.

Google SDK support for custom API addresses varies by version. If the SDK can change its base URL, confirm that requests go to omrouter.com; otherwise, use the HTTP example on this page.

See Gemini generateContent for the request and response fields used here.