Gemini API
Call Gemini models through the Google AI Studio / Gemini Developer API safely: a restricted key per app, billing and spend caps understood before traffic, the right endpoint, and a clean teardown.
Scope: the Gemini Developer API: keys and projects, billing plans and tiers, generateContent, the newer Interactions API, OpenAI compatibility, streaming, function calling, structured output, Batch, context caching, Files, rate and spend limits, paid vs unpaid data use, LiteLLM and client config. Out of scope: Gemini on Google Cloud with IAM and regions (google-vertex-ai), tier choice across providers (cloud-llms, choosing-llm-hosting), gateway setup (llm-gateway, litellm), open Gemma weights on your own hardware (self-hosted-llms), evals (llm-evals).
Work through the steps in order. Load a reference only when a step names it.
Discover first
Inspect before changing anything. Every command is read-only and never prints a secret.
# Is a key available? Names only, never values (GOOGLE_API_KEY wins over GEMINI_API_KEY in the SDKs)
env | cut -d= -f1 | grep -E '^(GEMINI|GOOGLE)_' || echo "no GEMINI_* or GOOGLE_* variables"
# SDK versions (any that exist)
pip show google-genai 2>/dev/null | sed -n '1,2p'
npm ls @google/genai 2>/dev/null | grep genai
# Does the key work, and which models can it see? (free call)
curl -sS "https://generativelanguage.googleapis.com/v1beta/models" -H "x-goog-api-key: ${GEMINI_API_KEY:?}" \
| jq -r '.models[] | [.name, ((.supportedGenerationMethods // []) | join(","))] | @tsv' | head -40
Tier, limits and spend caps are in AI Studio (Dashboard > Projects, Usage, Spend, Billing); see references/limits-and-budgets.md.
Decision flow
- No key, or unrestricted standard keys: create an auth key in a dedicated project; restrict it to the Gemini API (
references/setup-and-auth.md).
- Paid use planned: understand Prepay vs Postpay, the 402 at zero balance, and set a project spend cap first (
references/limits-and-budgets.md).
- New app: choose the native API (
generateContent is the long-standing path; the Interactions API is the newer one and has gaps) per references/api-and-sdks.md. OpenAI-format clients use the compatibility base URL.
- Non-interactive bulk work: Batch API at a discount, its own limits (
references/features.md).
- Sensitive data: only the paid quota keeps prompts out of product improvement; read
references/data-controls.md first.
- Gateway or OpenAI-compatible client:
references/litellm-and-clients.md.
- Errors:
references/troubleshooting.md.
Rules
- Ask before creating billable or persistent resources. Keys, projects, billing links, credits, auto-reload, caches, files and batches need approval and a teardown plan.
- Secrets never touch output. Keys come from environment variables or
0600 files, sent in the x-goog-api-key header. Avoid ?key= URLs, which land in logs.
- Never use free quota for sensitive data. Unpaid services may use content to improve Google products, with human review (check the current terms).
- Prepay means a zero balance stops every key on the billing account. Spend caps are experimental and lag by about 10 minutes; batch and agent jobs can overshoot.
- Never put keys in client apps. Use a backend proxy or a gateway key.
- Pin model IDs and re-list models; preview models have tighter limits and change.
- Never hardcode prices or limits. Read the pricing page and AI Studio.
- Always tear down. Disable keys, delete files and caches, check batches (
references/teardown.md).
Workflows
Account and auth
Call the API
Connect to a gateway or clients
Control cost and risk
Tear down
Troubleshoot
Reference index
references/setup-and-auth.md: key types, AI Studio projects, restrictions, leak response.
references/api-and-sdks.md: generateContent, Interactions API, OpenAI compatibility, model listing.
references/features.md: streaming, tools, structured output, Batch, caching, Files.
references/limits-and-budgets.md: tiers, rate and spend-rate limits, Prepay and Postpay, spend caps.
references/data-controls.md: paid vs unpaid terms, grounding storage, Files retention.
references/litellm-and-clients.md: LiteLLM gemini/ entries and client base URLs.
references/teardown.md: disable keys and delete stored objects.
references/troubleshooting.md: symptom, cause, fix.
Companion skills
cloud-llms: choose between frontier APIs, hyperscalers and GPU rental. Install: npx skills add ScaleDockHQ/llm-skills --skill cloud-llms.
llm-gateway: one endpoint, virtual keys and budgets in front of providers. Install: npx skills add ScaleDockHQ/llm-skills --skill llm-gateway.
litellm: LiteLLM proxy details. Install: npx skills add ScaleDockHQ/llm-skills --skill litellm.
google-vertex-ai: Gemini on Google Cloud with IAM and regions. Install: npx skills add ScaleDockHQ/llm-skills --skill google-vertex-ai.
openai-api, anthropic-api: the sibling frontier APIs. Install: npx skills add ScaleDockHQ/llm-skills --skill openai-api.
structured-outputs, llm-clients: schema-constrained output and client config. Install: npx skills add ScaleDockHQ/llm-skills --skill structured-outputs.
llm-evals, llm-observability: measure quality and trace calls. Install: npx skills add ScaleDockHQ/llm-skills --skill llm-evals.
choosing-llm-hosting, self-hosted-llms: when the data class rules out an API. Install: npx skills add ScaleDockHQ/llm-skills --skill choosing-llm-hosting.
1---2name: gemini-api3description: Use the Google Gemini API directly (generativelanguage.googleapis.com) - keys, projects, generateContent, Interactions API, OpenAI compatibility, streaming, tools, structured output, Batch, context caching, Files, tiers, spend caps, paid vs free data use. Use for GEMINI_API_KEY, google-genai SDK, AI Studio, 429 RESOURCE_EXHAUSTED, 402 Payment Required, LiteLLM gemini/ routes. Cross-provider choice goes to cloud-llms; Vertex AI goes to google-vertex-ai; gateway to llm-gateway.4license: MIT5compatibility: Any OS with bash, curl and jq; optional google-genai SDK (Python, Node, Go, Java) or the openai SDK. Needs network access to generativelanguage.googleapis.com and a key in an environment variable or 0600 file. Creating keys, projects, billing, credits, caches, files or batches needs the user's explicit approval.6metadata:7 source: https://skillmd.com/skills/scaledockhq/gemini-api8 author: ScaleDockHQ9 version: "0.1.0"10---1112# Gemini API1314Call Gemini models through the Google AI Studio / Gemini Developer API safely: a restricted key per app, billing and spend caps understood before traffic, the right endpoint, and a clean teardown.1516**Scope:** the Gemini Developer API: keys and projects, billing plans and tiers, `generateContent`, the newer Interactions API, OpenAI compatibility, streaming, function calling, structured output, Batch, context caching, Files, rate and spend limits, paid vs unpaid data use, LiteLLM and client config. **Out of scope:** Gemini on Google Cloud with IAM and regions (`google-vertex-ai`), tier choice across providers (`cloud-llms`, `choosing-llm-hosting`), gateway setup (`llm-gateway`, `litellm`), open Gemma weights on your own hardware (`self-hosted-llms`), evals (`llm-evals`).1718Work through the steps in order. Load a reference only when a step names it.1920## Discover first2122Inspect before changing anything. Every command is read-only and never prints a secret.2324```bash25# Is a key available? Names only, never values (GOOGLE_API_KEY wins over GEMINI_API_KEY in the SDKs)26env | cut -d= -f1 | grep -E '^(GEMINI|GOOGLE)_' || echo "no GEMINI_* or GOOGLE_* variables"2728# SDK versions (any that exist)29pip show google-genai 2>/dev/null | sed -n '1,2p'30npm ls @google/genai 2>/dev/null | grep genai3132# Does the key work, and which models can it see? (free call)33curl -sS "https://generativelanguage.googleapis.com/v1beta/models" -H "x-goog-api-key: ${GEMINI_API_KEY:?}" \34 | jq -r '.models[] | [.name, ((.supportedGenerationMethods // []) | join(","))] | @tsv' | head -4035```3637Tier, limits and spend caps are in AI Studio (Dashboard > Projects, Usage, Spend, Billing); see `references/limits-and-budgets.md`.3839## Decision flow40411. No key, or unrestricted standard keys: create an **auth key** in a dedicated project; restrict it to the Gemini API (`references/setup-and-auth.md`).422. Paid use planned: understand Prepay vs Postpay, the 402 at zero balance, and set a project spend cap first (`references/limits-and-budgets.md`).433. New app: choose the native API (`generateContent` is the long-standing path; the Interactions API is the newer one and has gaps) per `references/api-and-sdks.md`. OpenAI-format clients use the compatibility base URL.444. Non-interactive bulk work: Batch API at a discount, its own limits (`references/features.md`).455. Sensitive data: only the paid quota keeps prompts out of product improvement; read `references/data-controls.md` first.466. Gateway or OpenAI-compatible client: `references/litellm-and-clients.md`.477. Errors: `references/troubleshooting.md`.4849## Rules5051- **Ask before creating billable or persistent resources.** Keys, projects, billing links, credits, auto-reload, caches, files and batches need approval and a teardown plan.52- **Secrets never touch output.** Keys come from environment variables or `0600` files, sent in the `x-goog-api-key` header. Avoid `?key=` URLs, which land in logs.53- **Never use free quota for sensitive data.** Unpaid services may use content to improve Google products, with human review (check the current terms).54- **Prepay means a zero balance stops every key on the billing account.** Spend caps are experimental and lag by about 10 minutes; batch and agent jobs can overshoot.55- **Never put keys in client apps.** Use a backend proxy or a gateway key.56- **Pin model IDs** and re-list models; preview models have tighter limits and change.57- **Never hardcode prices or limits.** Read the pricing page and AI Studio.58- **Always tear down.** Disable keys, delete files and caches, check batches (`references/teardown.md`).5960## Workflows6162**Account and auth**6364- [ ] Project per environment; auth key restricted to Gemini API; key in a `0600` file -> `references/setup-and-auth.md`65- [ ] Verify with `GET /v1beta/models`.6667**Call the API**6869- [ ] `generateContent` or Interactions with a pinned model and a token cap -> `references/api-and-sdks.md`70- [ ] Streaming, tools, structured output, caching, Files only as needed -> `references/features.md`7172**Connect to a gateway or clients**7374- [ ] LiteLLM `gemini/<model>` with `api_key: os.environ/GEMINI_API_KEY`, or the OpenAI SDK base URL -> `references/litellm-and-clients.md`7576**Control cost and risk**7778- [ ] Billing plan, tier, project spend cap, billing alerts -> `references/limits-and-budgets.md`79- [ ] Paid vs unpaid data use, grounding retention -> `references/data-controls.md`8081**Tear down**8283- [ ] Delete files and caches; confirm batches ended; disable keys; archive test projects -> `references/teardown.md`8485**Troubleshoot**8687- [ ] Match the HTTP status and error status string -> `references/troubleshooting.md`8889## Reference index9091- `references/setup-and-auth.md`: key types, AI Studio projects, restrictions, leak response.92- `references/api-and-sdks.md`: generateContent, Interactions API, OpenAI compatibility, model listing.93- `references/features.md`: streaming, tools, structured output, Batch, caching, Files.94- `references/limits-and-budgets.md`: tiers, rate and spend-rate limits, Prepay and Postpay, spend caps.95- `references/data-controls.md`: paid vs unpaid terms, grounding storage, Files retention.96- `references/litellm-and-clients.md`: LiteLLM `gemini/` entries and client base URLs.97- `references/teardown.md`: disable keys and delete stored objects.98- `references/troubleshooting.md`: symptom, cause, fix.99100## Companion skills101102- `cloud-llms`: choose between frontier APIs, hyperscalers and GPU rental. Install: `npx skills add ScaleDockHQ/llm-skills --skill cloud-llms`.103- `llm-gateway`: one endpoint, virtual keys and budgets in front of providers. Install: `npx skills add ScaleDockHQ/llm-skills --skill llm-gateway`.104- `litellm`: LiteLLM proxy details. Install: `npx skills add ScaleDockHQ/llm-skills --skill litellm`.105- `google-vertex-ai`: Gemini on Google Cloud with IAM and regions. Install: `npx skills add ScaleDockHQ/llm-skills --skill google-vertex-ai`.106- `openai-api`, `anthropic-api`: the sibling frontier APIs. Install: `npx skills add ScaleDockHQ/llm-skills --skill openai-api`.107- `structured-outputs`, `llm-clients`: schema-constrained output and client config. Install: `npx skills add ScaleDockHQ/llm-skills --skill structured-outputs`.108- `llm-evals`, `llm-observability`: measure quality and trace calls. Install: `npx skills add ScaleDockHQ/llm-skills --skill llm-evals`.109- `choosing-llm-hosting`, `self-hosted-llms`: when the data class rules out an API. Install: `npx skills add ScaleDockHQ/llm-skills --skill choosing-llm-hosting`.