Gemini API की क्विकस्टार्ट गाइड
इस क्विकस्टार्ट गाइड में, हमारी लाइब्रेरी इंस्टॉल करने का तरीका बताया गया है. साथ ही, इसमें Interactions API का इस्तेमाल करके, Gemini API के लिए पहला अनुरोध करने का तरीका बताया गया है.
शुरू करने से पहले
Gemini API का इस्तेमाल करने के लिए, आपके पास एक एपीआई पासकोड होना चाहिए. इससे आपके अनुरोधों की पुष्टि की जा सकेगी, सुरक्षा से जुड़ी सीमाएं लागू की जा सकेंगी, और आपके खाते के इस्तेमाल को ट्रैक किया जा सकेगा.
शुरू करने के लिए, AI Studio पर मुफ़्त में एक प्रोजेक्ट बनाएं:
Google GenAI SDK इंस्टॉल करना
Python
Python 3.9 या इसके बाद के वर्शन का इस्तेमाल करके, google-genai पैकेज इंस्टॉल करें. इसके लिए, यहां दी गई pip कमांड का इस्तेमाल करें:
pip install -q -U google-genai
JavaScript
Node.js v18+ का इस्तेमाल करके, npm कमांड का इस्तेमाल करके, Google Gen AI SDK for TypeScript and JavaScript इंस्टॉल करें:
npm install @google/genai
पहला अनुरोध करना
Gemini API को अनुरोध भेजने के लिए, इन दो तरीकों का इस्तेमाल किया जा सकता है:
- (सुझाया गया) Interactions API एक नया प्रिमिटिव है. इसमें कई चरणों वाले टूल के इस्तेमाल, ऑर्केस्ट्रेशन, और टाइप किए गए एक्ज़ीक्यूशन चरणों के ज़रिए जटिल तर्क वाले फ़्लो के लिए नेटिव सपोर्ट उपलब्ध है. आगे चलकर, मुख्य लाइन फ़ैमिली के अलावा अन्य नए मॉडल, एजेंट की तरह काम करने की नई क्षमताओं और टूल को सिर्फ़ Interactions API पर लॉन्च किया जाएगा.
generateContentकी मदद से, मॉडल से आसान और स्टेटलेस जवाब जनरेट किया जा सकता है. हमारा सुझाव है कि Interactions API का इस्तेमाल करें. हालांकि,generateContentपूरी तरह से काम करता है.
इस उदाहरण में, Gemini 3 Flash मॉडल का इस्तेमाल करके Gemini API को अनुरोध भेजने के लिए, Interactions API का इस्तेमाल किया गया है.
अगर आपने एपीआई पासकोड को एनवायरमेंट वैरिएबल GEMINI_API_KEY के तौर पर सेट किया है, तो Gemini API लाइब्रेरी का इस्तेमाल करते समय, क्लाइंट इसे अपने-आप चुन लेगा.
ऐसा न करने पर, क्लाइंट को शुरू करते समय आपको अपनी एपीआई कुंजी को एक तर्क के तौर पर पास करना होगा.
ध्यान दें कि Gemini API के दस्तावेज़ों में दिए गए सभी कोड सैंपल में यह माना गया है कि आपने एनवायरमेंट वैरिएबल GEMINI_API_KEY सेट किया है.
Python
# This will only work for SDK newer than 2.0.0
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
// This will only work for SDK newer than 2.0.0
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
# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-H "Api-Revision: 2026-05-20" \
-d '{
"model": "gemini-3-flash-preview",
"input": "Explain how AI works in a few words"
}'
स्टेटलेस मोड
डिफ़ॉल्ट रूप से, Interactions API, बातचीत की स्थिति को सर्वर-साइड पर मैनेज करता है. ऐसा तब होता है, जब previous_interaction_id का इस्तेमाल किया जाता है. अगर आपको क्लाइंट-साइड पर बातचीत के इतिहास को खुद मैनेज करना है, तो स्टेटलेस मोड में ऑप्ट इन किया जा सकता है. इसके लिए, store=false सेट करें और बाद के अनुरोधों के input फ़ील्ड में, इकट्ठा किए गए चरणों को पास करें.
ज़्यादा जानकारी और बिना किसी स्टेटस वाले कई टर्न वाले उदाहरणों के लिए, टेक्स्ट जनरेट करने से जुड़ी गाइड देखें.
आगे क्या करना है
अब आपने एपीआई का पहला अनुरोध कर लिया है. अब आपको इन गाइड को एक्सप्लोर करना चाहिए. इनमें Gemini की कार्रवाई दिखाई गई है: