本快速入门将向您介绍如何安装我们的 库 并发出您的第一个 Gemini API 请求。
准备工作
如需使用 Gemini API,您需要一个 API 密钥来对请求进行身份验证、强制执行安全限制以及跟踪您账号的使用情况。
在 AI Studio 上免费创建一个,即可开始使用:
安装 Google GenAI SDK
Python
使用 Python 3.9+,使用以下
pip 命令安装
google-genai 软件包:
pip install -q -U google-genai
JavaScript
使用 Node.js v18+,安装适用于 TypeScript 和 JavaScript 的 Google Gen AI SDK,使用以下 npm command:
npm install @google/genai
Go
使用 go get command 命令在 模块目录中安装 google.golang.org/genai:
go get google.golang.org/genai
Java
如果您使用的是 Maven,则可以通过将 google-genai添加到依赖项中来安装 以下内容:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
C#
使用 dotnet add 命令在 模块目录中安装 googleapis/go-genai
dotnet add package Google.GenAI
Apps 脚本
- 如需创建新的 Apps 脚本项目,请前往 script.new。
- 点击未命名项目 。
- 将 Apps 脚本项目重命名为 AI Studio ,然后点击重命名 。
- 设置您的 API 密钥
- 点击左侧的项目设置
。
- 在脚本属性 下,点击添加脚本属性 。
- 对于属性,输入密钥名称:
GEMINI_API_KEY。 - 对于值,输入 API 密钥的值。
- 点击保存脚本属性 。
- 点击左侧的项目设置
- 将
Code.gs文件内容替换为以下代码:
提交第一个请求
您可以通过以下两种方式向 Gemini API 发送请求:
- (推荐) Interactions API 是一种新的基元,通过类型化执行步骤原生支持多步工具使用、编排和复杂的推理流程。今后,除了核心主线系列之外的新模型,以及新的智能体功能和工具,都将仅在 Interactions API 上发布。
generateContent提供了一种从模型生成简单无状态响应的方法。虽然我们建议使用 Interactions API,但generateContent受到完全支持。
此示例使用
generateContent方法
使用 Gemini 2.5 Flash 模型向 Gemini API 发送请求。
如果您将 API 密钥 设置为
环境变量 GEMINI_API_KEY,则在使用 Gemini API 库 时,客户端会自动提取该密钥。
否则,您需要在初始化客户端时将 API 密钥 作为
实参传递。
请注意,Gemini API 文档中的所有代码示例都假定您已设置环境变量 GEMINI_API_KEY。
Python
from google import genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
response = client.models.generate_content(
model="gemini-3-flash-preview", contents="Explain how AI works in a few words"
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
const ai = new GoogleGenAI({});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
Go
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
result, err := client.Models.GenerateContent(
ctx,
"gemini-3-flash-preview",
genai.Text("Explain how AI works in a few words"),
nil,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text())
}
Java
package com.example;
import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
public class GenerateTextFromTextInput {
public static void main(String[] args) {
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
Client client = new Client();
GenerateContentResponse response =
client.models.generateContent(
"gemini-3-flash-preview",
"Explain how AI works in a few words",
null);
System.out.println(response.text());
}
}
C#
using System.Threading.Tasks;
using Google.GenAI;
using Google.GenAI.Types;
public class GenerateContentSimpleText {
public static async Task main() {
// The client gets the API key from the environment variable `GOOGLE_API_KEY`.
var client = new Client();
var response = await client.Models.GenerateContentAsync(
model: "gemini-3-flash-preview", contents: "Explain how AI works in a few words"
);
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
}
}
Apps 脚本
// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
function main() {
const payload = {
contents: [
{
parts: [
{ text: 'Explain how AI works in a few words' },
],
},
],
};
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent';
const options = {
method: 'POST',
contentType: 'application/json',
headers: {
'x-goog-api-key': apiKey,
},
payload: JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response);
const content = data['candidates'][0]['content']['parts'][0]['text'];
console.log(content);
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}'
后续步骤
现在您已发出第一个 API 请求,不妨探索以下指南,了解 Gemini 的实际应用: