Ky udhëzues i shpejtë ju tregon se si të instaloni bibliotekat tona dhe të bëni kërkesën tuaj të parë Gemini API.
Para se të filloni
Ju nevojitet një çelës API Gemini. Nëse nuk e keni tashmë një të tillë, mund ta merrni falas në Google AI Studio .
Instaloni SDK-në Google GenAI
Python
Duke përdorur Python 3.9+ , instaloni paketën google-genai duke përdorur komandën e mëposhtme pip :
pip install -q -U google-genai
JavaScript
Duke përdorur Node.js v18+ , instaloni SDK-në Google Gen AI për TypeScript dhe JavaScript duke përdorur komandën npm të mëposhtme:
npm install @google/genai
Shko
Instaloni google.golang.org/genai në direktorinë e modulit tuaj duke përdorur komandën go get :
go get google.golang.org/genai
Java
Nëse po përdorni Maven, mund të instaloni google-genai duke shtuar sa vijon në varësitë tuaja:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
C#
Instaloni googleapis/go-genai në direktorinë e modulit tuaj duke përdorur komandën dotnet add.
dotnet add package Google.GenAI
Skripti i Aplikacioneve
- Për të krijuar një projekt të ri Apps Script, shkoni te script.new .
- Klikoni Projekt pa titull .
- Riemërto projektin Apps Script AI Studio dhe kliko Riemërto .
- Vendos çelësin tënd API
- Në të majtë, klikoni Cilësimet e Projektit
.
- Nën Vetitë e Skriptit, klikoni Shto vetinë e skriptit .
- Për Pronën , fut emrin e çelësit:
GEMINI_API_KEY. - Për Value , futni vlerën për çelësin API.
- Klikoni Ruaj vetitë e skriptit .
- Në të majtë, klikoni Cilësimet e Projektit
- Zëvendësoni përmbajtjen e skedarit
Code.gsme kodin e mëposhtëm:
Bëni kërkesën tuaj të parë
Ja një shembull që përdor metodën generateContent për të dërguar një kërkesë te Gemini API duke përdorur modelin Gemini 2.5 Flash.
Nëse e vendosni çelësin tuaj API si variablin e mjedisit GEMINI_API_KEY , ai do të merret automatikisht nga klienti kur përdor libraritë e API-t Gemini . Përndryshe, do t'ju duhet ta kaloni çelësin tuaj API si argument gjatë inicializimit të klientit.
Vini re se të gjitha mostrat e kodit në dokumentet e Gemini API supozojnë se ju keni vendosur variablin e mjedisit 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-2.5-flash", 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-2.5-flash",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
Shko
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-2.5-flash",
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-2.5-flash",
"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-2.5-flash", contents: "Explain how AI works in a few words"
);
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
}
}
Skripti i Aplikacioneve
// 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-2.5-flash: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);
}
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash: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"
}
]
}
]
}'
Çfarë vjen më pas
Tani që keni bërë kërkesën tuaj të parë API, mund të dëshironi të eksploroni udhëzuesit e mëposhtëm që tregojnë Gemini në veprim: