يوضّح لك هذا الدليل السريع كيفية تثبيت مكتباتنا وإجراء أول طلب بيانات من واجهة برمجة التطبيقات إلى Gemini API.
قبل البدء
يتطلّب استخدام Gemini API مفتاح واجهة برمجة التطبيقات، ويمكنك إنشاء مفتاح مجانًا للبدء.
تثبيت حزمة Google GenAI SDK
Python
باستخدام Python 3.9 أو إصدار أحدث، ثبِّت حزمة
google-genai باستخدام
أمر
pip التالي:
pip install -q -U google-genai
JavaScript
باستخدام Node.js الإصدار 18 أو إصدار أحدث، ثبِّت حزمة Google Gen AI SDK لـ TypeScript وJavaScript باستخدام أمر npm التالي:
npm install @google/genai
انتقال
ثبِّت google.golang.org/genai في دليل الوحدة باستخدام أمر go get:
go get google.golang.org/genai
جافا
إذا كنت تستخدم Maven، يمكنك تثبيت google-genai عن طريق إضافة ما يلي إلى التبعيات:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
#C
ثبِّت googleapis/go-genai في دليل الوحدة باستخدام أمر dotnet add
dotnet add package Google.GenAI
برمجة التطبيقات
- لإنشاء مشروع جديد في برمجة تطبيقات، انتقِل إلى script.new.
- انقر على مشروع بلا عنوان.
- أعِد تسمية مشروع برمجة التطبيقات AI Studio وانقر على إعادة تسمية.
- ضبط مفتاح واجهة برمجة التطبيقات
- على يمين الصفحة، انقر على إعدادات المشروع
.
- ضمن خصائص النص البرمجي ، انقر على إضافة خاصية للنص البرمجي.
- بالنسبة إلى الخاصية، أدخِل اسم المفتاح:
GEMINI_API_KEY. - بالنسبة إلى القيمة، أدخِل قيمة مفتاح واجهة برمجة التطبيقات.
- انقر على حفظ مواقع النص البرمجي.
- على يمين الصفحة، انقر على إعدادات المشروع
- استبدِل محتويات ملف
Code.gsبالرمز البرمجي التالي:
إجراء أول طلب
في ما يلي مثال يستخدم طريقة
generateContent
لإرسال طلب إلى Gemini API باستخدام نموذج Gemini 2.5 Flash.
إذا ضبطت مفتاح واجهة برمجة التطبيقات كمتغيّر
بيئة GEMINI_API_KEY، سيختاره العميل تلقائيًا عند استخدام مكتبات Gemini 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()
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();
انتقال
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())
}
جافا
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);
}
}
برمجة التطبيقات
// 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"
}
]
}
]
}'
الخطوات التالية
بعد إجراء أول طلب بيانات من واجهة برمجة التطبيقات، قد ترغب في استكشاف الأدلة التالية التي توضّح كيفية استخدام Gemini: