Hướng dẫn bắt đầu nhanh này sẽ hướng dẫn bạn cách cài đặt các thư viện của chúng tôi và thực hiện yêu cầu API đầu tiên đối với Gemini API.
Trước khi bắt đầu
Để sử dụng Gemini API, bạn cần có một khoá API. Bạn có thể tạo một khoá miễn phí để bắt đầu.
Cài đặt SDK Google GenAI
Python
Khi sử dụng Python 3.9+, hãy cài đặt gói
google-genai bằng lệnh
pip sau:
pip install -q -U google-genai
JavaScript
Khi sử dụng Node.js phiên bản 18 trở lên, hãy cài đặt SDK Google Gen AI cho TypeScript và JavaScript bằng lệnh npm sau:
npm install @google/genai
Go
Cài đặt google.golang.org/genai trong thư mục mô-đun bằng lệnh go get:
go get google.golang.org/genai
Java
Nếu đang sử dụng Maven, bạn có thể cài đặt google-genai bằng cách thêm phần sau vào các phần phụ thuộc:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
C#
Cài đặt googleapis/go-genai trong thư mục mô-đun bằng lệnh dotnet add
dotnet add package Google.GenAI
Apps Script
- Để tạo một dự án Apps Script mới, hãy truy cập vào script.new.
- Nhấp vào Dự án chưa đặt tên.
- Đổi tên dự án Apps Script thành AI Studio rồi nhấp vào Đổi tên.
- Đặt khoá API
- Ở bên trái, hãy nhấp vào Cài đặt dự án
.
- Trong phần Thuộc tính của tập lệnh , hãy nhấp vào Thêm thuộc tính của tập lệnh.
- Đối với Thuộc tính, hãy nhập tên khoá:
GEMINI_API_KEY. - Đối với Giá trị, hãy nhập giá trị cho khoá API.
- Nhấp vào Lưu thuộc tính của tập lệnh.
- Ở bên trái, hãy nhấp vào Cài đặt dự án
- Thay thế nội dung tệp
Code.gsbằng mã sau:
Thực hiện yêu cầu đầu tiên
Sau đây là một ví dụ sử dụng phương thức
generateContent để gửi yêu cầu đến Gemini API bằng mô hình Gemini 2.5 Flash.
Nếu bạn đặt khoá API làm
biến môi trường GEMINI_API_KEY, thì ứng dụng sẽ tự động chọn khoá này khi sử dụng
khi sử dụng các thư viện Gemini API.
Nếu không, bạn cần phải truyền khoá API làm
đối số khi khởi chạy ứng dụng.
Xin lưu ý rằng tất cả các mã mẫu trong tài liệu về Gemini API đều giả định rằng bạn đã đặt biến môi trường 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 `GEMINI_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 Script
// 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"
}
]
}
]
}'
Bước tiếp theo
Giờ đây, bạn đã thực hiện yêu cầu API đầu tiên. Bạn có thể muốn khám phá các hướng dẫn sau đây để xem Gemini hoạt động: