Gemini API का इस्तेमाल करने के लिए, आपको एक एपीआई पासकोड की ज़रूरत होगी. Google AI Studio में जाकर, कुछ ही क्लिक में बिना किसी शुल्क के पासकोड बनाया जा सकता है.
एपीआई पासकोड मिलने के बाद, Gemini API से कनेक्ट करने के लिए आपके पास ये विकल्प होते हैं:
शुरुआती टेस्टिंग के लिए, एपीआई पासकोड को हार्ड कोड किया जा सकता है. हालांकि, यह सिर्फ़ कुछ समय के लिए होना चाहिए, क्योंकि यह सुरक्षित नहीं है. एपीआई पासकोड को हार्ड कोड करने के उदाहरण, एपीआई पासकोड को साफ़ तौर पर उपलब्ध कराना सेक्शन में देखे जा सकते हैं.
एपीआई पासकोड को एनवायरमेंट वैरिएबल के तौर पर सेट करना
अगर आपने एनवायरमेंट वैरिएबल GEMINI_API_KEY या GOOGLE_API_KEY सेट किया है, तो Gemini API लाइब्रेरी में से किसी एक का इस्तेमाल करते समय, क्लाइंट अपने-आप एपीआई पासकोड चुन लेगा. हमारा सुझाव है कि आप इनमें से सिर्फ़ एक वैरिएबल सेट करें. हालांकि, अगर दोनों सेट किए जाते हैं, तो GOOGLE_API_KEY को प्राथमिकता दी जाएगी.
अगर ब्राउज़र पर REST API या JavaScript का इस्तेमाल किया जा रहा है, तो आपको एपीआई कुंजी साफ़ तौर पर देनी होगी.
यहां बताया गया है कि अलग-अलग ऑपरेटिंग सिस्टम पर, एनवायरमेंट वैरिएबल GEMINI_API_KEY के तौर पर एपीआई पासकोड को स्थानीय तौर पर कैसे सेट किया जा सकता है.
Linux/macOS - Bash
Bash, Linux और macOS के टर्मिनल कॉन्फ़िगरेशन में आम तौर पर इस्तेमाल किया जाता है. यह देखने के लिए कि आपके पास इसके लिए कॉन्फ़िगरेशन फ़ाइल है या नहीं, यह निर्देश चलाएं:
~/.bashrc
अगर जवाब में "ऐसी कोई फ़ाइल या डायरेक्ट्री नहीं है" दिखता है, तो आपको यह फ़ाइल बनानी होगी. इसके बाद, यहां दिए गए निर्देशों का पालन करके इसे खोलें या zsh का इस्तेमाल करें:
touch~/.bashrcopen~/.bashrc
इसके बाद, आपको एपीआई कुंजी सेट करनी होगी. इसके लिए, नीचे दिए गए एक्सपोर्ट कमांड को जोड़ें:
exportGEMINI_API_KEY=<YOUR_API_KEY_HERE>
फ़ाइल सेव करने के बाद, बदलाव लागू करने के लिए यह कमांड चलाएं:
source~/.bashrc
macOS - Zsh
Zsh, Linux और macOS के टर्मिनल का सामान्य कॉन्फ़िगरेशन है. यह देखने के लिए कि आपके पास इसके लिए कॉन्फ़िगरेशन फ़ाइल है या नहीं, यह निर्देश चलाएं:
~/.zshrc
अगर जवाब में "ऐसी कोई फ़ाइल या डायरेक्ट्री नहीं है" दिखता है, तो आपको यह फ़ाइल बनानी होगी. इसके बाद, यहां दिए गए निर्देशों का पालन करके इसे खोलें या bash का इस्तेमाल करें:
touch~/.zshrcopen~/.zshrc
इसके बाद, आपको एपीआई कुंजी सेट करनी होगी. इसके लिए, नीचे दिए गए एक्सपोर्ट कमांड को जोड़ें:
exportGEMINI_API_KEY=<YOUR_API_KEY_HERE>
फ़ाइल सेव करने के बाद, बदलाव लागू करने के लिए यह कमांड चलाएं:
source~/.zshrc
Windows
सिस्टम सेटिंग में "Environment Variables" खोजें
"उपयोगकर्ता वैरिएबल" (मौजूदा उपयोगकर्ता के लिए) या "सिस्टम वैरिएबल" (सभी उपयोगकर्ताओं के लिए - इसका इस्तेमाल सावधानी से करें) में से किसी एक में बदलाव करें.
वैरिएबल बनाएं और export GEMINI_API_KEY=your_key_here जोड़ें
बदलाव लागू करना
एपीआई पासकोड साफ़ तौर पर उपलब्ध कराना
कुछ मामलों में, आपको एपीआई पासकोड साफ़ तौर पर देना पड़ सकता है. उदाहरण के लिए:
आपको एक सामान्य एपीआई कॉल करना है और एपीआई पासकोड को हार्ड कोड करना है.
आपको Gemini API लाइब्रेरी की ओर से, एनवायरमेंट वैरिएबल का अपने-आप पता लगाने की सुविधा पर भरोसा नहीं करना है और आपको इस पर पूरा कंट्रोल चाहिए
आपने ऐसा एनवायरमेंट चुना है जहां एनवायरमेंट वैरिएबल काम नहीं करते (जैसे कि वेब) या आपने REST कॉल किए हैं.
एपीआई पासकोड को साफ़ तौर पर बताने के कुछ उदाहरण यहां दिए गए हैं:
Python
fromgoogleimportgenaiclient=genai.Client(api_key="YOUR_API_KEY")response=client.models.generate_content(model="gemini-2.5-flash",contents="Explain how AI works in a few words")print(response.text)
JavaScript
import{GoogleGenAI}from"@google/genai";constai=newGoogleGenAI({apiKey:"YOUR_API_KEY"});asyncfunctionmain(){constresponse=awaitai.models.generateContent({model:"gemini-2.5-flash",contents:"Explain how AI works in a few words",});console.log(response.text);}main();
ऐप पर जाएं
packagemainimport("context""fmt""log""google.golang.org/genai")funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,&genai.ClientConfig{APIKey:"YOUR_API_KEY",Backend:genai.BackendGeminiAPI,})iferr!=nil{log.Fatal(err)}result,err:=client.Models.GenerateContent(ctx,"gemini-2.5-flash",genai.Text("Explain how AI works in a few words"),nil,)iferr!=nil{log.Fatal(err)}fmt.Println(result.Text())}
Java
packagecom.example;importcom.google.genai.Client;importcom.google.genai.types.GenerateContentResponse;publicclassGenerateTextFromTextInput{publicstaticvoidmain(String[]args){Clientclient=Client.builder().apiKey("YOUR_API_KEY").build();GenerateContentResponseresponse=client.models.generateContent("gemini-2.5-flash","Explain how AI works in a few words",null);System.out.println(response.text());}}
REST
curl"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$YOUR_API_KEY"\-H'Content-Type: application/json'\-XPOST\-d'{ "contents": [ { "parts": [ { "text": "Explain how AI works in a few words" } ] } ] }'
अपने एपीआई पासकोड को सुरक्षित रखना
अपने Gemini API पासकोड को पासवर्ड की तरह इस्तेमाल करें. अगर क्रेडेंशियल से समझौता किया जाता है, तो अन्य लोग आपके प्रोजेक्ट के कोटे का इस्तेमाल कर सकते हैं. साथ ही, बिलिंग चालू होने पर शुल्क चुका सकते हैं. इसके अलावा, वे आपकी निजी जानकारी, जैसे कि फ़ाइलें ऐक्सेस कर सकते हैं.
सुरक्षा से जुड़े गंभीर नियम
एपीआई कुंजियों को कभी भी सोर्स कंट्रोल में न डालें. अपनी एपीआई पासकोड को Git जैसे वर्शन कंट्रोल सिस्टम में सेव न करें.
क्लाइंट-साइड पर कभी भी एपीआई कुंजियां ज़ाहिर न करें. प्रोडक्शन में वेब या मोबाइल ऐप्लिकेशन में, अपनी एपीआई कुंजी का सीधे तौर पर इस्तेमाल न करें. क्लाइंट-साइड कोड (इसमें हमारी JavaScript/TypeScript लाइब्रेरी और REST कॉल शामिल हैं) में मौजूद कुंजियां निकाली जा सकती हैं.
सबसे सही तरीके
एपीआई पासकोड के साथ सर्वर-साइड कॉल का इस्तेमाल करें अपने एपीआई पासकोड का इस्तेमाल करने का सबसे सुरक्षित तरीका यह है कि Gemini API को सर्वर-साइड ऐप्लिकेशन से कॉल किया जाए. इससे पासकोड को गोपनीय रखा जा सकता है.
क्लाइंट-साइड ऐक्सेस के लिए, कुछ समय के लिए मान्य टोकन का इस्तेमाल करें (सिर्फ़ Live API के लिए): Live API को सीधे तौर पर क्लाइंट-साइड से ऐक्सेस करने के लिए, कुछ समय के लिए मान्य टोकन का इस्तेमाल किया जा सकता है. इनमें सुरक्षा से जुड़े जोखिम कम होते हैं और इनका इस्तेमाल प्रोडक्शन के लिए किया जा सकता है. ज़्यादा जानकारी के लिए, कुछ समय के लिए मान्य टोकन की गाइड देखें.
अपनी कुंजी पर पाबंदियां लगाएं:एपीआई पासकोड से जुड़ी पाबंदियां जोड़कर, किसी कुंजी की अनुमतियों को सीमित किया जा सकता है. अगर कभी कुंजी लीक हो जाती है, तो इससे होने वाले संभावित नुकसान को कम किया जा सकता है.
कुछ सामान्य सबसे सही तरीकों के बारे में जानने के लिए, यह सहायता लेख भी पढ़ें.
[[["समझने में आसान है","easyToUnderstand","thumb-up"],["मेरी समस्या हल हो गई","solvedMyProblem","thumb-up"],["अन्य","otherUp","thumb-up"]],[["वह जानकारी मौजूद नहीं है जो मुझे चाहिए","missingTheInformationINeed","thumb-down"],["बहुत मुश्किल है / बहुत सारे चरण हैं","tooComplicatedTooManySteps","thumb-down"],["पुराना","outOfDate","thumb-down"],["अनुवाद से जुड़ी समस्या","translationIssue","thumb-down"],["सैंपल / कोड से जुड़ी समस्या","samplesCodeIssue","thumb-down"],["अन्य","otherDown","thumb-down"]],["आखिरी बार 2025-08-22 (UTC) को अपडेट किया गया."],[],[],null,["# Using Gemini API keys\n\nTo use the Gemini API, you need an API key. You can create a key for free with a\nfew clicks in [Google AI Studio](https://aistudio.google.com/app/apikey).\n\nOnce you have an API key, you have the following options to connect to the\nGemini API:\n\n- [Setting your API key as an environment variable](#set-api-env-var)\n- [Providing your API key explicitly](#provide-api-key-explicitly)\n\nFor initial testing, you can hard code an API key, but this should only be\ntemporary since it's not secure. You can find examples for hard coding the API\nkey in [Providing API key explicitly](#provide-api-key-explicitly) section.\n\nSetting API key as environment variable\n---------------------------------------\n\nIf you set the environment variable `GEMINI_API_KEY` or `GOOGLE_API_KEY`, the\nAPI key will automatically be picked up by the client when using one of the\n[Gemini API libraries](/gemini-api/docs/libraries). It's recommended that you\nset only one of those variables, but if both are set, `GOOGLE_API_KEY` takes\nprecedence.\n\nIf you're using the REST API, or JavaScript on the browser, you will need to\nprovide the API key explicitly.\n\nHere is how you can set your API key locally as the environment variable\n`GEMINI_API_KEY` with different operating systems. \n\n### Linux/macOS - Bash\n\nBash is a common Linux and macOS terminal configuration. You can check if\nyou have a configuration file for it by running the following command: \n\n ~/.bashrc\n\nIf the response is \"No such file or directory\", you will need to create this\nfile and open it by running the following commands, or use `zsh`: \n\n touch ~/.bashrc\n open ~/.bashrc\n\nNext, you need to set your API key by adding the following export command: \n\n export GEMINI_API_KEY=\u003cYOUR_API_KEY_HERE\u003e\n\nAfter saving the file, apply the changes by running: \n\n source ~/.bashrc\n\n### macOS - Zsh\n\nZsh is a common Linux and macOS terminal configuration. You can check if\nyou have a configuration file for it by running the following command: \n\n ~/.zshrc\n\nIf the response is \"No such file or directory\", you will need to create this\nfile and open it by running the following commands, or use `bash`: \n\n touch ~/.zshrc\n open ~/.zshrc\n\nNext, you need to set your API key by adding the following export command: \n\n export GEMINI_API_KEY=\u003cYOUR_API_KEY_HERE\u003e\n\nAfter saving the file, apply the changes by running: \n\n source ~/.zshrc\n\n### Windows\n\n1. Search for \"Environment Variables\" in the system settings\n2. Edit either \"User variables\" (for current user) or \"System variables\" (for all users - use with caution).\n3. Create the variable and add `export GEMINI_API_KEY=your_key_here`\n4. Apply the changes\n\nProviding API key explicitly\n----------------------------\n\nIn some cases, you may want to explicitly provide an API key. For example:\n\n- You're doing a simple API call and prefer hard coding the API key.\n- You want explicit control without having to rely on automatic discovery of environment variables by the Gemini API libraries\n- You're using an environment where environment variables are not supported (e.g web) or you are making REST calls.\n\nBelow are examples for how you can provide an API key explicitly: \n\n### Python\n\n from google import genai\n\n client = genai.Client(api_key=\"\u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e\")\n\n response = client.models.generate_content(\n model=\"gemini-2.5-flash\", contents=\"Explain how AI works in a few words\"\n )\n print(response.text)\n\n### JavaScript\n\n import { GoogleGenAI } from \"@google/genai\";\n\n const ai = new GoogleGenAI({ apiKey: \"\u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e\" });\n\n async function main() {\n const response = await ai.models.generateContent({\n model: \"gemini-2.5-flash\",\n contents: \"Explain how AI works in a few words\",\n });\n console.log(response.text);\n }\n\n main();\n\n### Go\n\n package main\n\n import (\n \"context\"\n \"fmt\"\n \"log\"\n \"google.golang.org/genai\"\n )\n\n func main() {\n ctx := context.Background()\n client, err := genai.NewClient(ctx, &genai.ClientConfig{\n APIKey: \"\u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e\",\n Backend: genai.BackendGeminiAPI,\n })\n if err != nil {\n log.Fatal(err)\n }\n\n result, err := client.Models.GenerateContent(\n ctx,\n \"gemini-2.5-flash\",\n genai.Text(\"Explain how AI works in a few words\"),\n nil,\n )\n if err != nil {\n log.Fatal(err)\n }\n fmt.Println(result.Text())\n }\n\n### Java\n\n package com.example;\n\n import com.google.genai.Client;\n import com.google.genai.types.GenerateContentResponse;\n\n public class GenerateTextFromTextInput {\n public static void main(String[] args) {\n Client client = Client.builder().apiKey(\"\u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e\").build();\n\n GenerateContentResponse response =\n client.models.generateContent(\n \"gemini-2.5-flash\",\n \"Explain how AI works in a few words\",\n null);\n\n System.out.println(response.text());\n }\n }\n\n### REST\n\n curl \"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$\u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e\" \\\n -H 'Content-Type: application/json' \\\n -X POST \\\n -d '{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"Explain how AI works in a few words\"\n }\n ]\n }\n ]\n }'\n\nKeep your API key secure\n------------------------\n\nTreat your Gemini API key like a password. If compromised, others can use your\nproject's quota, incur charges (if billing is enabled), and access your\nprivate data, such as files.\n\n### Critical security rules\n\n- **Never commit API keys to source control.** Do not check your API key into version control systems like Git.\n\n- **Never expose API keys on the client-side.** Do not use your API key directly\n in web or mobile apps in production. Keys in client-side code\n (including our JavaScript/TypeScript libraries and REST calls) can be\n extracted.\n\n### Best practices\n\n- **Use server-side calls with API keys** The most secure way to use your API\n key is to call the Gemini API from a server-side application where the key\n can be kept confidential.\n\n- **Use ephemeral tokens for client-side access (Live API only):** For direct\n client-side access to the Live API, you can use ephemeral tokens. They come with\n lower security risks and can be suitable for production use. Review\n [ephemeral tokens](/gemini-api/docs/ephemeral-tokens) guide for more information.\n\n- **Consider adding restrictions to your key:** You can limit a key's permissions\n by adding [API key restrictions](https://cloud.google.com/api-keys/docs/add-restrictions-api-keys#add-api-restrictions).\n This minimizes the potential damage if the key is ever leaked.\n\nFor some general best practices, you can also review this\n[support article](https://support.google.com/googleapi/answer/6310037)."]]