Gemini API क्विकस्टार्ट

इस क्विकस्टार्ट में, हमारी लाइब्रेरी इंस्टॉल करने और Gemini API का पहला अनुरोध करने का तरीका बताया गया है.

शुरू करने से पहले

Gemini API का इस्तेमाल करने के लिए, एपीआई पासकोड की ज़रूरत होती है. इसे बिना किसी शुल्क के बनाया जा सकता है.

Gemini API पासकोड बनाना

Google GenAI SDK इंस्टॉल करना

Python

Python 3.9 या इसके बाद के वर्शन का इस्तेमाल करके, pip कमांड का इस्तेमाल करके, google-genai पैकेज इंस्टॉल करें:

pip install -q -U google-genai

JavaScript

Node.js v18+ का इस्तेमाल करके, TypeScript और JavaScript के लिए Google Gen AI SDK इंस्टॉल करें. इसके लिए, यहां दी गई npm कमांड का इस्तेमाल करें:

npm install @google/genai

ऐप पर जाएं

go get कमांड का इस्तेमाल करके, अपनी मॉड्यूल डायरेक्ट्री में google.golang.org/genai इंस्टॉल करें:

go get google.golang.org/genai

Java

अगर Maven का इस्तेमाल किया जा रहा है, तो google-genai को इंस्टॉल किया जा सकता है. इसके लिए, अपनी डिपेंडेंसी में यह जानकारी जोड़ें:

<dependencies>
  <dependency>
    <groupId>com.google.genai</groupId>
    <artifactId>google-genai</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

C#

dotnet add command का इस्तेमाल करके, अपने मॉड्यूल डायरेक्ट्री में googleapis/go-genai इंस्टॉल करें

dotnet add package Google.GenAI

Apps Script

  1. नया Apps Script प्रोजेक्ट बनाने के लिए, script.new पर जाएं.
  2. बिना टाइटल वाला प्रोजेक्ट पर क्लिक करें.
  3. Apps Script प्रोजेक्ट का नाम बदलकर AI Studio करें. इसके बाद, नाम बदलें पर क्लिक करें.
  4. अपना एपीआई पासकोड सेट करें
    1. बाईं ओर, प्रोजेक्ट सेटिंग प्रोजेक्ट सेटिंग का आइकॉन पर क्लिक करें.
    2. स्क्रिप्ट प्रॉपर्टी में जाकर, स्क्रिप्ट प्रॉपर्टी जोड़ें पर क्लिक करें.
    3. प्रॉपर्टी के लिए, कुंजी का नाम डालें: GEMINI_API_KEY.
    4. वैल्यू के लिए, एपीआई पासकोड की वैल्यू डालें.
    5. स्क्रिप्ट प्रॉपर्टी सेव करें पर क्लिक करें.
  5. Code.gs फ़ाइल के कॉन्टेंट की जगह यह कोड डालें:

पहला अनुरोध करना

यहां Gemini 2.5 Flash मॉडल का इस्तेमाल करके, Gemini API को अनुरोध भेजने का एक उदाहरण दिया गया है. इसमें generateContent तरीके का इस्तेमाल किया गया है.

अगर आपने एपीआई पासकोड को एनवायरमेंट वैरिएबल 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())
}

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

आगे क्या करना है

अब आपने एपीआई का पहला अनुरोध कर लिया है. अब आपको इन गाइड को एक्सप्लोर करना चाहिए. इनमें Gemini की कार्रवाई दिखाई गई है: