מדריך להתחלה מהירה של Gemini API
במדריך למתחילים הזה מוסבר איך להתקין את הספריות שלנו ולשלוח את בקשת ה-API הראשונה ל-Gemini API באמצעות Interactions API.
לפני שמתחילים
כדי להשתמש ב-Gemini API, צריך מפתח API. אפשר ליצור מפתח כזה בחינם כדי להתחיל.
התקנה של Google GenAI SDK
Python
באמצעות Python 3.9 ואילך, מתקינים את החבילה google-genai באמצעות פקודת pip הבאה:
pip install -q -U google-genai
JavaScript
באמצעות Node.js v18+, מתקינים את Google Gen AI SDK ל-TypeScript ול-JavaScript באמצעות פקודת npm הבאה:
npm install @google/genai
שליחת הבקשה הראשונה
הנה דוגמה לשימוש ב-Interactions API כדי לשלוח בקשה ל-Gemini API באמצעות מודל Gemini 3 Flash.
אם מגדירים את מפתח ה-API כמשתנה הסביבה GEMINI_API_KEY, הלקוח יזהה אותו באופן אוטומטי כשמשתמשים בספריות Gemini API.
אחרת, תצטרכו להעביר את מפתח ה-API כארגומנט כשמפעילים את הלקוח.
שימו לב: כל דוגמאות הקוד במסמכי ה-Gemini API מניחות שהגדרתם את משתנה הסביבה GEMINI_API_KEY.
Python
from google import genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Explain how AI works in a few words"
)
# Print the model's text response
for step in interaction.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text":
print(content_block.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 interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Explain how AI works in a few words",
});
const modelStep = interaction.steps.find(s => s.type === 'model_output');
if (modelStep) {
for (const contentBlock of modelStep.content) {
if (contentBlock.type === 'text') console.log(contentBlock.text);
}
}
}
main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "Explain how AI works in a few words"
}'
המאמרים הבאים
אחרי ששלחתם את הבקשה הראשונה ל-API, כדאי לעיין במדריכים הבאים שבהם אפשר לראות את Gemini בפעולה: