Gemini API reference

您可以透過 Gemini API 存取 Google 的最新生成式模型。本 API 參考資料提供 Gemini API SDK 中可用的類別和方法的詳細資訊。選擇語言,然後按照設定步驟開始建構。

安裝 Gemini API 程式庫

使用 Node.js 18 以上版本,請使用下列 npm 指令安裝 Google Gen AI SDK for TypeScript and JavaScript

npm install @google/genai

提出第一個要求

使用 generateContent 方法將要求傳送至 Gemini API。

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "Explain how AI works in a few words",
  });
  console.log(response.text);
}

main();

後續步驟

如果您是初學者,請參閱下列指南,瞭解 Gemini API 程式設計模型:

您也可以查看功能指南,瞭解不同的 Gemini API 功能,並取得程式碼範例: