Gemini Pro API Guide: Key, Endpoint, Model IDs, and OpenAI Compatibility

Gemini Pro API Guide: Key, Endpoint, Model IDs, and OpenAI Compatibility

The Gemini Pro API is accessed through the Gemini API with a key created in Google AI Studio. For a direct REST request, call the model’s generateContent endpoint; for an existing OpenAI SDK integration, point the client at Google’s OpenAI-compatible base URL and use a current Gemini model ID such as gemini-3.1-pro-preview. The important detail is that “Gemini Pro” is a product-family search term, not a permanent API identifier, so production applications should read Google’s current model list before pinning an ID.

Gemini Pro API setup at a glance

You need four values to make a request:

SettingValue
API keyCreate one in Google AI Studio
Native base hosthttps://generativelanguage.googleapis.com
Native API path/v1beta/models/{model}:generateContent
OpenAI-compatible base URLhttps://generativelanguage.googleapis.com/v1beta/openai/
Example model IDgemini-3.1-pro-preview

Google’s Gemini API quickstart documents API-key creation and the native request pattern. Its OpenAI compatibility guide documents the compatibility base URL for applications that already use the OpenAI Python or JavaScript SDK.

Use the native Gemini SDK or REST API when you want Gemini-specific features as soon as Google exposes them. Use the compatibility layer when you already have an OpenAI-style client and want to reduce migration work. Compatibility is useful, but it does not guarantee that every provider-specific option maps perfectly across APIs.

How to get a Google API key for Gemini

Create the key in Google AI Studio, then store it in an environment variable instead of placing it in source code:

export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"

Treat this as a server-side credential. Do not commit it to Git, print it in logs, or embed it in browser JavaScript or a mobile application bundle. If a frontend needs Gemini output, send the user request to your own backend and let the backend call the Google API.

For a production service, also decide who owns the Google Cloud project, how keys are rotated, which environments receive separate credentials, and where request quotas are monitored. Google’s API key guidance explains how Gemini API keys are associated with Google Cloud projects.

How to call the native Gemini API endpoint

The native REST route puts the model ID in the URL. This example asks the current Pro preview model to return a concise migration checklist:

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Create a seven-step checklist for migrating a Python API from one region to two regions. Include rollback checks."
          }
        ]
      }
    ]
  }'

The response contains candidates with generated content. Real applications should handle an empty candidate list, blocked content, timeouts, and non-2xx responses rather than indexing directly into the first response object.

The URL uses v1beta because that is the route shown in Google’s current Gemini API examples. Keep the API version in configuration so you can test a new version without scattering endpoint strings throughout the codebase.

Native endpoint anatomy

The path has three parts:

/v1beta/models/{model}:generateContent
  • v1beta is the API version.
  • {model} is the exact model ID from Google’s Gemini models page.
  • generateContent is the generation method.

A 404 response often means the model ID, API version, or method does not match. Before changing authentication code, compare the full path with the current model documentation.

How to use Gemini with an OpenAI-compatible client

If your application already uses the OpenAI Python package, install it and change the API key, base URL, and model ID:

pip install openai
import os

from openai import OpenAI


client = OpenAI(
    api_key=os.environ["GEMINI_API_KEY"],
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)

response = client.chat.completions.create(
    model="gemini-3.1-pro-preview",
    messages=[
        {
            "role": "system",
            "content": "You are a concise software architecture reviewer.",
        },
        {
            "role": "user",
            "content": "Review a queue worker design and list the top five failure modes.",
        },
    ],
)

print(response.choices[0].message.content)

This is the shortest route for teams with an existing chat-completions abstraction. It also makes an evaluation harness easier to reuse: keep the prompt and response checks constant, then swap the provider configuration.

Do not assume identical behavior just because two providers accept the same SDK call. System instructions, tool schemas, multimodal inputs, safety handling, streaming events, token accounting, and error payloads can differ. Run provider-specific tests before changing production traffic.

How to choose and manage Gemini model IDs

Avoid placing a marketing name such as gemini-pro directly in application logic. Google’s available model IDs change as preview models are introduced, promoted, and retired. At the time this guide was checked, Google’s official models page listed gemini-3.1-pro-preview as a Pro-class model identifier.

Use a configuration layer instead:

import os


GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-3.1-pro-preview")

That small choice makes model upgrades a deployment change rather than a code rewrite. For a larger service, store these fields together:

{
  "provider": "google",
  "base_url": "https://generativelanguage.googleapis.com/v1beta/openai/",
  "model": "gemini-3.1-pro-preview",
  "timeout_seconds": 60
}

Before moving a new model into production:

  1. Confirm that the ID appears in Google’s current model documentation or models API.
  2. Check whether the model is preview, stable, or scheduled for retirement.
  3. Run your own evaluation set for answer quality and tool-call correctness.
  4. Measure latency, token use, and failure rates with representative prompts.
  5. Add a fallback model or a clear failure path before shifting all traffic.

Rate limits are not a single universal number. They depend on the model and usage tier, so read Google’s Gemini API rate-limit documentation and monitor the limits applied to your project.

How to build a provider-switchable backend

An OpenAI-compatible interface can reduce code changes, but provider switching works best when your own application defines the contract. Keep provider configuration outside business logic and normalize the output you actually need.

import os

from openai import OpenAI


PROVIDERS = {
    "gemini": {
        "api_key": os.environ["GEMINI_API_KEY"],
        "base_url": "https://generativelanguage.googleapis.com/v1beta/openai/",
        "model": os.getenv("GEMINI_MODEL", "gemini-3.1-pro-preview"),
    },
    "novita": {
        "api_key": os.environ["NOVITA_API_KEY"],
        "base_url": "https://api.novita.ai/openai",
        "model": os.getenv("NOVITA_MODEL", "xiaomimimo/mimo-v2.5-pro"),
    },
}


def generate(provider_name: str, prompt: str) -> str:
    provider = PROVIDERS[provider_name]
    client = OpenAI(
        api_key=provider["api_key"],
        base_url=provider["base_url"],
    )
    response = client.chat.completions.create(
        model=provider["model"],
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content or ""

This example deliberately exposes the differences instead of hiding them. Each provider keeps its own credential, base URL, and model ID. The application receives one normalized string, while provider-specific tests can cover richer behavior such as tools or multimodal inputs.

Novita AI’s LLM API documentation uses an OpenAI-compatible API shape for supported models. This can be useful when a team wants to compare Gemini with open-source models without rebuilding the entire client layer.

How Gemini fits into an agent backend

An agent backend has at least two separate responsibilities:

  1. Inference: The model decides what to say or which tool to call.
  2. Execution: A controlled runtime performs file, shell, browser, or application actions.

The Gemini API can handle the inference side. It should not be treated as the execution boundary. If a model proposes a shell command, your application still needs to validate the tool call, authorize it, run it in an isolated environment, capture the result, and decide what context to send back to the model.

Novita Agent Sandbox is designed for isolated agent execution workflows. A practical architecture can use Gemini for reasoning while a sandbox handles code or browser tasks separately:

User request
    -> Agent service
        -> Gemini API for reasoning and tool selection
        -> Policy checks for the proposed action
        -> Agent Sandbox for isolated execution
        -> Tool result returned to the agent service
        -> Gemini API for the final response

This separation makes the model replaceable and keeps untrusted execution away from the application server. It also gives the backend one place to enforce timeouts, network policy, file limits, audit logging, and user authorization.

For a first version, expose only a few narrow tools, define JSON schemas for their arguments, reject unknown fields, and place hard limits on execution time and output size. Add broader computer-use or browser capabilities only after the permission model is clear.

When an open-source model is a better fit

Gemini Pro models are a strong option when your application needs Google’s model capabilities and managed API. An open-source model may be a better fit when you need a second provider, want to evaluate model behavior against a visible upstream release, or prefer a model available through an OpenAI-compatible endpoint alongside other infrastructure.

MiMo-V2.5-Pro is one current option on Novita AI. Xiaomi’s upstream model card describes it as an open-source Mixture-of-Experts model, while Novita AI provides the hosted model ID xiaomimimo/mimo-v2.5-pro. Because both the Google compatibility endpoint and Novita AI can be called with an OpenAI-style client, the provider-switchable pattern in the previous section can evaluate them with the same prompts and acceptance checks.

Do not choose on label alone. Build a small evaluation set from your actual workload: code review comments, support questions, retrieval-grounded answers, tool calls, or long documents. Compare output quality, latency, error behavior, and cost using current provider dashboards before making a routing decision.

Common Gemini API errors

400: Invalid request

Check the JSON shape, message roles, tool definitions, and parameter names. An option accepted by another OpenAI-compatible provider may not be accepted by Google’s compatibility layer.

401 or 403: Authentication or permission failure

Confirm that GEMINI_API_KEY is present in the process environment and belongs to the intended Google Cloud project. Also check whether the project and selected model are available for the account and region.

404: Model or method not found

Compare the exact model ID with the current Gemini model list. For native REST calls, verify the API version and :generateContent suffix. For OpenAI-compatible calls, verify that the base URL ends with /v1beta/openai/.

429: Rate limit exceeded

Retry with exponential backoff and jitter, but do not treat retries as a substitute for capacity planning. Queue bursty work, cap concurrent requests, and inspect the project’s current usage tier and model-specific limits.

The SDK works, but the output differs after switching providers

Compatibility covers the request interface, not identical model behavior. Re-run prompt, structured-output, and tool-call tests for every provider and model version.

Conclusion

Start with the native Gemini API when you want the clearest path to Gemini-specific features. Start with the OpenAI-compatible endpoint when you already have an OpenAI-style backend or need a fast provider evaluation. In both cases, keep the API key server-side, put the model ID in configuration, test the exact model version, and separate model reasoning from agent execution.

For a resilient production design, keep at least one alternative model behind the same application-owned interface. That gives your team a practical way to test an open-source option on Novita AI, handle model lifecycle changes, and route agent execution into an isolated sandbox instead of coupling every responsibility to one API call.

FAQ

Is there still a model ID called gemini-pro?

Do not assume that gemini-pro is the current ID. “Gemini Pro API” is commonly used as a search phrase for Google’s higher-capability Gemini models, but applications must use an exact ID from the current Gemini models page. This guide uses gemini-3.1-pro-preview as the checked example.

Where do I get a Google API key for Gemini?

Create a Gemini API key in Google AI Studio. Store it in a server-side secret such as GEMINI_API_KEY, not in source code or frontend JavaScript.

What is the Gemini API endpoint?

The native host is https://generativelanguage.googleapis.com. A generate-content request uses /v1beta/models/{model}:generateContent. Google’s OpenAI-compatible base URL is https://generativelanguage.googleapis.com/v1beta/openai/.

Is the Gemini Studio API different from the Gemini API?

Google AI Studio is the web interface developers use to experiment and create a key. Application requests go to the Gemini API. Searches for “Gemini Studio API” usually refer to this AI Studio-to-API workflow.

Is the Google Bard API the same as the Gemini API?

Gemini is the current API and model brand. Older searches for a Google Bard API should use the current Gemini API documentation, endpoints, and model IDs rather than old Bard examples.

Can I use the OpenAI SDK with Gemini?

Yes. Google documents an OpenAI compatibility endpoint. Set the client base URL to Google’s compatibility URL, provide your Gemini API key, and select a supported Gemini model ID. Test provider-specific features before relying on full behavioral parity.

Can Gemini run code for an AI agent?

Gemini can reason about code and propose tool calls, but execution should happen in a controlled runtime. Keep the model call separate from an isolated environment such as Agent Sandbox, and validate every requested action before running it.