دليل البدء السريع لاستخدام Gemini API

يوضّح لك هذا الدليل السريع كيفية تثبيت مكتباتنا وإجراء أول طلب بيانات من واجهة برمجة التطبيقات باستخدام Gemini API من خلال Interactions API.

قبل البدء

يتطلب استخدام 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

إجراء طلبك الأول

في ما يلي مثال يستخدم Interactions API لإرسال طلب إلى Gemini API باستخدام نموذج Gemini 3 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()

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();

راحة

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"
  }'

الخطوات التالية

بعد إجراء أول طلب بيانات من واجهة برمجة التطبيقات، قد يهمّك استكشاف الأدلة التالية التي تعرض Gemini أثناء العمل: