Skip to main content
Google Gemini is a very powerful AI conversational system that can generate fluent and natural responses within seconds by entering prompts. This document mainly describes how to use the Gemini Generate Content API. This is Google’s official native API format, supporting the generateContent and streamGenerateContent endpoints.

Differences from the Chat Completions API

The Gemini Generate Content API uses Google’s official native request format (the contents field), rather than the OpenAI-compatible format (the messages field). If you are already using the Google Gemini SDK or are familiar with the official API format, you can use this API directly without modifying the request format.

Current Support Scope

The interface accepts text, image, video, and audio inputs (inlineData or fileData), thinking configuration, and custom function declarations. Native fields such as audio output, built-in tools, and explicit context caching do not yet have guaranteed unified availability; please refer to the actual response of the specific model. When submitting audio and other files using fileData.fileUri, please ensure that the file can be accessed directly via HTTPS, does not depend on a login state, and that the validity period of the signed link covers the entire request processing time. Non-streaming audio analysis returns results only after the model has finished processing, which may exceed 90 seconds; clients should set a sufficiently long total timeout according to file size and business scenarios, to avoid disconnecting while the server is still processing.

Application Process

Before using the Gemini Generate Content API, please first visit the Gemini Generate Content API page and click the “Get” button to obtain the credentials required for requests. If you have not yet logged in or registered, you will be automatically redirected to the login page. The first application includes free credits.

Basic Usage

Non-Streaming

Send a POST request to /v1beta/models/{model}:generateContent:
Note: The flash models in the gemini-3.x series are thinking models and will consume reasoning tokens first; please set generationConfig.maxOutputTokens to above 512, otherwise only empty content may be returned.
Example response:
The token fields in usageMetadata are billed according to native Gemini semantics:
  • promptTokenCount includes cachedContentTokenCount; cache-hit portions are calculated at the cache read price and will not be counted again as regular input.
  • promptTokensDetails and cacheTokensDetails are normalized into text_tokens, image_tokens, video_tokens, audio_tokens, and their corresponding cached_* details under usage.prompt_tokens_details. These values are included in prompt_tokens and cached_tokens respectively; different modalities are recalculated only at their respective prices and will not be billed repeatedly.
  • Both candidatesTokenCount and thoughtsTokenCount are calculated at the output token price.
  • toolUsePromptTokenCount is an independent tool input token count and is calculated at the input token price.
Ace Data Cloud additionally returns a normalized usage object. Across model protocols, read the cost of the current call uniformly from usage.cost. cost is an optional cost preview; when price previews are temporarily unavailable, the model response will still return normally but may not include this field. The final bill is subject to usage records.

Streaming

Send a POST request to /v1beta/models/{model}:streamGenerateContent?alt=sse:
Streaming responses will gradually return content in SSE (Server-Sent Events) format. Intermediate events usually contain only incremental content; the final event containing authoritative usage information will include usageMetadata and normalized usage, while the cost remains in usage.cost:
Closing the connection indicates the end of the stream; native Gemini streams do not send data: [DONE]. If the final price preview is temporarily unavailable, usage will still return token usage, but will omit cost.

Supported Models

Advanced Features

System Instructions

Generation Configuration

JSON Mode

Thinking Mode (Thinking)

Models that support thinking capabilities (such as gemini-2.5-flash and gemini-2.5-pro) can enable thinking mode:

Function Calling

Multi-turn Conversation

Image Understanding

Safety Settings

Content filtering can be controlled through safetySettings:

Error Handling