Z tego krótkiego wprowadzenia dowiesz się, jak zainstalować nasze biblioteki i wysłać pierwsze żądanie do interfejsu Gemini API.
Zanim zaczniesz
Aby korzystać z interfejsu Gemini API, potrzebujesz klucza interfejsu API. Możesz go utworzyć bezpłatnie.
Utwórz klucz interfejsu Gemini API
Zainstaluj pakiet Google GenAI SDK
Python
Jeśli używasz Pythona w wersji 3.9 lub nowszej, zainstaluj pakiet
google-genai za pomocą tego
polecenia pip:
pip install -q -U google-genai
JavaScript
Jeśli używasz Node.js w wersji 18 lub nowszej, zainstaluj pakiet Google Gen AI SDK dla TypeScript i JavaScript za pomocą tego polecenia npm:
npm install @google/genai
Go
Zainstaluj google.golang.org/genai w katalogu modułu za pomocą polecenia go get:
go get google.golang.org/genai
Java
Jeśli używasz Maven, możesz zainstalować google-genai, dodając do zależności ten kod:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
C#
Zainstaluj googleapis/go-genai w katalogu modułu za pomocą polecenia dotnet add
dotnet add package Google.GenAI
Apps Script
- Aby utworzyć nowy projekt Apps Script, wejdź na script.new.
- Kliknij Projekt bez tytułu.
- Zmień nazwę projektu Apps Script na AI Studio i kliknij Zmień nazwę.
- Ustaw swój klucz interfejsu API
- Po lewej stronie kliknij Ustawienia projektu
.
- W sekcji Właściwości skryptu kliknij Dodaj właściwość skryptu.
- W polu Właściwość wpisz nazwę klucza:
GEMINI_API_KEY. - W polu Wartość wpisz wartość klucza interfejsu API.
- Kliknij Zapisz właściwości skryptu.
- Po lewej stronie kliknij Ustawienia projektu
- Zastąp zawartość pliku
Code.gstym kodem:
Wyślij pierwsze żądanie
Oto przykład, który używa metody
generateContent
do wysłania żądania do interfejsu Gemini API za pomocą modelu Gemini 2.5 Flash.
Jeśli ustawisz klucz interfejsu API jako
zmienną środowiskową GEMINI_API_KEY, klient automatycznie go wykryje podczas korzystania z
bibliotek interfejsu Gemini API.
W przeciwnym razie musisz przekazać klucz interfejsu API jako
argument podczas inicjowania klienta.
Pamiętaj, że wszystkie przykłady kodu w dokumentacji interfejsu Gemini API zakładają, że masz ustawioną zmienną środowiskową 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 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"
}
]
}
]
}'
Co dalej?
Teraz, gdy wysłałeś pierwsze żądanie do interfejsu API, możesz zapoznać się z tymi przewodnikami, które pokazują, jak działa Gemini: