跳到主要內容
API 參考
API 參考

Gemini 生成內容

透過 RunAPI 相容 Gemini 的 Generate Content 操作傳送請求。

01

概覽

使用 RunAPI 的 Gemini 相容「Generate Content」操作,搭配相容的模型及該協定的請求與回應格式。

快速入門

  1. 建立 API 金鑰並將其設定為 RUNAPI_API_KEY。
  2. 選擇相容的模型,然後傳送文件中指定的路徑參數與 JSON 本文。
  3. 讀取此操作顯示的 JSON 回應或伺服器傳送事件,並處理協定錯誤。

端點

POST /v1beta/models/{model}:generateContent
基礎 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 個相容模型
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

協定錯誤:invalid_request

HTTP 400

結構描述

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())