इस क्विकस्टार्ट में, हमारी लाइब्रेरी इंस्टॉल करने और Gemini API के लिए पहला अनुरोध भेजने का तरीका बताया गया है.
शुरू करने से पहले
Gemini API का इस्तेमाल करने के लिए, एपीआई पासकोड की ज़रूरत होती है. इसे मुफ़्त में बनाया जा सकता है.
Google GenAI SDK इंस्टॉल करना
Python
Python 3.9 या इसके बाद के वर्शन का इस्तेमाल करके,
google-genai पैकेज
इंस्टॉल करें. इसके लिए,
pip का यह कमांड इस्तेमाल करें:
pip install -q -U google-genai
JavaScript
Node.js v18 या इसके बाद के वर्शन का इस्तेमाल करके, TypeScript और JavaScript के लिए Google Gen AI SDK इंस्टॉल करें. इसके लिए, npm का यह कमांड इस्तेमाल करें:
npm install @google/genai
Go
अपने मॉड्यूल डायरेक्ट्री में google.golang.org/genai इंस्टॉल करें. इसके लिए, go get command का इस्तेमाल करें:
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#
अपने मॉड्यूल डायरेक्ट्री में googleapis/go-genai इंस्टॉल करें. इसके लिए, dotnet add कमांड का इस्तेमाल करें
dotnet add package Google.GenAI
Apps Script
- Apps Script का नया प्रोजेक्ट बनाने के लिए, script.new पर जाएं.
- बिना टाइटल वाला प्रोजेक्ट पर क्लिक करें.
- Apps Script प्रोजेक्ट का नाम बदलकर AI Studio करें और नाम बदलें पर क्लिक करें.
- अपना एपीआई पासकोड सेट करें
- बाईं ओर, प्रोजेक्ट की सेटिंग
पर क्लिक करें.
- स्क्रिप्ट की प्रॉपर्टी में जाकर, स्क्रिप्ट प्रॉपर्टी जोड़ें पर क्लिक करें.
- प्रॉपर्टी के लिए, पासकोड का नाम डालें:
GEMINI_API_KEY. - वैल्यू के लिए, एपीआई पासकोड की वैल्यू डालें.
- स्क्रिप्ट प्रॉपर्टी सेव करें पर क्लिक करें.
- बाईं ओर, प्रोजेक्ट की सेटिंग
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();
Go
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 के काम करने का तरीका दिखाया गया है: