Z tego przewodnika dowiesz się, jak zainstalować nasze biblioteki i wysłać pierwsze żądanie do interfejsu Gemini API.
Zanim zaczniesz
Korzystanie z Gemini API wymaga klucza interfejsu API, który możesz utworzyć bezpłatnie, aby rozpocząć.
Tworzenie klucza interfejsu Gemini API
Instalowanie pakietu Google GenAI SDK
Python
Korzystając z Pythona 3.9 lub nowszego, zainstaluj google-genai za pomocą tego polecenia pip:
pip install -q -U google-genai
JavaScript
Używając 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 pakiet 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 poniższy kod:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
C#
Zainstaluj pakiet googleapis/go-genai w katalogu modułów za pomocą polecenia dotnet add.
dotnet add package Google.GenAI
Google Apps Script
- Aby utworzyć nowy projekt Apps Script, otwórz script.new.
- Kliknij Projekt bez nazwy.
- Zmień nazwę projektu Apps Script na AI Studio i kliknij Zmień nazwę.
- Ustaw 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:
Prześlij pierwszą prośbę
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, zostanie on automatycznie pobrany przez klienta podczas korzystania z bibliotek Gemini API.
W przeciwnym razie podczas inicjowania klienta musisz przekazać klucz interfejsu API jako argument.
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 `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);
}
}
Google 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?
Po wysłaniu pierwszego żądania do interfejsu API możesz zapoznać się z tymi przewodnikami, które pokazują Gemini w działaniu: