API 參考
Gemini Generate Content
透過 RunAPI 的 Gemini 相容 Generate Content 操作傳送請求。
01
概覽
使用 RunAPI 的 Gemini 相容生成內容操作,配合相容模型及該協定的請求與回應格式。
快速入門
- 建立 API 金鑰並將其設定為 RUNAPI_API_KEY。
- 選擇相容的模型,然後發送文檔中的路徑參數和 JSON 主體。
- 讀取此操作所示的 JSON 回應或伺服器推送事件,並處理協定錯誤。
端點
- 基礎 URL
https://runapi.ai- 合約版本
v1- 建議的身份驗證方式
x-goog-api-key: YOUR_API_TOKEN
02
身份驗證載體
RunAPI 接受以下列出的每種載體。生成的範例使用協議的首選標頭。
- header
Authorization: Bearer YOUR_API_TOKEN- header
x-api-key: YOUR_API_TOKEN- header - 建議
x-goog-api-key: YOUR_API_TOKEN- query
?key=YOUR_API_TOKEN
03
相容模型
開啟模型頁面以查看當前定價、速率限制及商業用途詳情。
顯示 56 個相容模型
- claude-fable-5-1
- claude-fable-5
- claude-sonnet-5
- claude-opus-5
- claude-opus-4-8
- claude-opus-4-7
- claude-opus-4-6
- claude-sonnet-4-6
- claude-opus-4-5-20251101
- claude-sonnet-4-5-20250929
- claude-haiku-4-5-20251001
- claude-opus-4-1-20250805
- gpt-5.5
- gpt-5.5-pro
- gpt-5.4
- gpt-5.4-mini
- gpt-5.4-nano
- gpt-5.4-pro
- gpt-5.3-codex
- gpt-5.3-codex-spark
- gpt-5.2
- gpt-5.6-luna
- gpt-5.6-sol
- gpt-5.6-terra
- gpt-6-astra
- gemini-3.7-flash
- gemini-3.6-flash
- gemini-3.5-flash
- gemini-3.1-pro-preview
- gemini-3-flash-preview
- gemini-2.5-pro
- gemini-2.5-flash
- deepseek-v4-flash-vision-exp
- deepseek-v4-pro
- deepseek-v4-flash
- glm-5.2
- glm-5.1
- glm-5-turbo
- glm-5
- glm-4.7
- glm-4.6
- glm-4.5
- glm-4.5-air
- kimi-k3
- kimi-k2.7-code
- kimi-k2.6
- kimi-k2.5
- MiniMax-M3
- MiniMax-M2.7
- MiniMax-M2.7-highspeed
- MiniMax-M2.5
- MiniMax-M2.5-highspeed
- MiniMax-M2.1
- MiniMax-M2
- grok-4.5
- grok-4.3
04
JSON 內容請求合約
僅列出同時具有公開協議和 RunAPI 支援證明的欄位。額外的 JSON 主體欄位將直接傳遞。
JSON 內文4 個欄位
contentsarray
必填
採用 Gemini Content 和 Part 格式的對話內容。
generationConfigobject
選填
請求的 Gemini 生成配置。
systemInstructionobject
選填
以 Gemini Content 形式呈現的系統指令。
toolsarray
選填
模型可呼叫的工具。
路徑參數1 個欄位
modelstring
必填
置於線路路徑中的 RunAPI 模型識別符。
請求範例
JSON
{
"contents": [
{
"parts": [
{
"text": "Summarize one practical improvement for an API deployment."
}
],
"role": "user"
}
]
}
05
HTTP 200
同步回應
結構描述
JSON
{
"additionalProperties": true,
"properties": {
"candidates": {
"items": {
"additionalProperties": true,
"properties": {
"content": {
"additionalProperties": true,
"properties": {
"parts": {
"type": "array"
},
"role": {
"type": "string"
}
},
"type": "object"
},
"finishReason": {
"type": "string"
},
"index": {
"type": "integer"
}
},
"type": "object"
},
"type": "array"
},
"modelVersion": {
"type": "string"
},
"responseId": {
"type": "string"
},
"usageMetadata": {
"additionalProperties": true,
"properties": {
"candidatesTokenCount": {
"type": "integer"
},
"promptTokenCount": {
"type": "integer"
},
"totalTokenCount": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
}
範例
JSON
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Hello! How can I help today?"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
],
"modelVersion": "gemini-3.5-flash",
"responseId": "resp_example",
"usageMetadata": {
"candidatesTokenCount": 8,
"promptTokenCount": 12,
"totalTokenCount": 20
}
}
06
HTTP 400
協定錯誤:invalid_request
結構描述
JSON
{
"additionalProperties": true,
"properties": {
"error": {
"additionalProperties": true,
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"code",
"message",
"status"
],
"type": "object"
}
},
"required": [
"error"
],
"type": "object"
}
範例
JSON
{
"error": {
"code": 400,
"message": "contents is required",
"status": "INVALID_ARGUMENT"
}
}
07
用量
結構描述
JSON
{
"additionalProperties": true,
"properties": {
"candidatesTokenCount": {
"type": "integer"
},
"promptTokenCount": {
"type": "integer"
},
"totalTokenCount": {
"type": "integer"
}
},
"type": "object"
}
範例
JSON
{
"candidatesTokenCount": 8,
"promptTokenCount": 12,
"totalTokenCount": 20
}
08
已生成的程式碼範例
每個 cURL、JavaScript 及 Python 範例均透過此合約發送已驗證的請求,無需使用特定協議的 SDK。
安裝
Shell
pip install requests
PYTHON
import requests
response = requests.post(
"https://runapi.ai/v1beta/models/gemini-3.5-flash:generateContent",
headers={"x-goog-api-key": "YOUR_API_TOKEN", "Content-Type": "application/json"},
json={"contents": [{"role": "user", "parts": [{"text": "Summarize one practical improvement for an API deployment."}]}]}
)
response.raise_for_status()
print(response.json())