Questa guida rapida mostra come installare le nostre librerie ed effettuare la prima richiesta all'API Gemini.
Prima di iniziare
Per utilizzare l'API Gemini è necessaria una chiave API. Puoi crearne una senza costi per iniziare.
Installa l'SDK Google GenAI
Python
Utilizzando Python 3.9 o versioni successive, installa il
google-genai pacchetto
utilizzando il seguente
comando pip:
pip install -q -U google-genai
JavaScript
Utilizzando Node.js v18+, installa l' SDK Google Gen AI per TypeScript e JavaScript utilizzando il seguente comando npm:
npm install @google/genai
Vai
Installa google.golang.org/genai nella directory del modulo utilizzando il comando go get:
go get google.golang.org/genai
Java
Se utilizzi Maven, puoi installare google-genai aggiungendo quanto segue alle dipendenze:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
C#
Installa googleapis/go-genai nella directory del modulo utilizzando il comando dotnet add
dotnet add package Google.GenAI
Apps Script
- Per creare un nuovo progetto Apps Script, vai a script.new.
- Fai clic su Progetto senza titolo.
- Rinomina il progetto Apps Script AI Studio e fai clic su Rinomina.
- Imposta la chiave API
- A sinistra, fai clic su Impostazioni progetto
.
- In Proprietà script fai clic su Aggiungi proprietà script.
- In Proprietà, inserisci il nome della chiave:
GEMINI_API_KEY. - In Valore, inserisci il valore della chiave API.
- Fai clic su Salva proprietà script.
- A sinistra, fai clic su Impostazioni progetto
- Sostituisci i contenuti del file
Code.gscon il seguente codice:
Effettua la prima richiesta
Ecco un esempio che utilizza il
generateContent metodo
per inviare una richiesta all'API Gemini utilizzando il modello Gemini 2.5 Flash.
Se imposti la chiave API come
variabile di ambiente GEMINI_API_KEY, il client la rileverà automaticamente quando utilizzi le librerie dell'API Gemini.
In caso contrario, dovrai passare la chiave API come
argomento durante l'inizializzazione del client.
Tieni presente che tutti gli esempi di codice nella documentazione dell'API Gemini presuppongono che tu abbia impostato la variabile di ambiente 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();
Vai
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"
}
]
}
]
}'
Passaggi successivi
Ora che hai effettuato la prima richiesta API, potresti voler esplorare le seguenti guide che mostrano Gemini in azione: