প্রাথমিক পরীক্ষার জন্য, আপনি একটি API কী হার্ড কোড করতে পারেন, কিন্তু এটি শুধুমাত্র অস্থায়ী হওয়া উচিত কারণ এটি নিরাপদ নয়। আপনি API কীকে স্পষ্টভাবে প্রদান করা বিভাগে API কী-এর হার্ড কোডিংয়ের উদাহরণ খুঁজে পেতে পারেন।
পরিবেশ পরিবর্তনশীল হিসাবে API কী সেট করা হচ্ছে
আপনি যদি এনভায়রনমেন্ট ভেরিয়েবল GEMINI_API_KEY বা GOOGLE_API_KEY সেট করেন, Gemini API লাইব্রেরিগুলির একটি ব্যবহার করার সময় API কীটি স্বয়ংক্রিয়ভাবে ক্লায়েন্ট দ্বারা বাছাই করা হবে৷ এটি সুপারিশ করা হয় যে আপনি শুধুমাত্র এই ভেরিয়েবলগুলির মধ্যে একটি সেট করুন, কিন্তু যদি উভয়ই সেট করা থাকে, GOOGLE_API_KEY অগ্রাধিকার নেয়৷
আপনি যদি ব্রাউজারে REST API, বা JavaScript ব্যবহার করেন, তাহলে আপনাকে API কীটি স্পষ্টভাবে প্রদান করতে হবে।
বিভিন্ন অপারেটিং সিস্টেমের সাথে পরিবেশ পরিবর্তনশীল GEMINI_API_KEY হিসাবে আপনি কীভাবে স্থানীয়ভাবে আপনার API কী সেট করতে পারেন তা এখানে।
লিনাক্স/ম্যাকোস - ব্যাশ
Bash হল একটি সাধারণ Linux এবং macOS টার্মিনাল কনফিগারেশন। নিম্নলিখিত কমান্ডটি চালানোর মাধ্যমে আপনি এটির জন্য একটি কনফিগারেশন ফাইল আছে কিনা তা পরীক্ষা করতে পারেন:
~/.bashrc
যদি প্রতিক্রিয়াটি "এমন কোন ফাইল বা ডিরেক্টরি নেই" হয়, তাহলে আপনাকে এই ফাইলটি তৈরি করতে হবে এবং নিম্নলিখিত কমান্ডগুলি চালিয়ে এটি খুলতে হবে বা zsh ব্যবহার করতে হবে:
touch~/.bashrcopen~/.bashrc
এর পরে, আপনাকে নিম্নলিখিত এক্সপোর্ট কমান্ড যোগ করে আপনার API কী সেট করতে হবে:
exportGEMINI_API_KEY=<YOUR_API_KEY_HERE>
ফাইল সংরক্ষণ করার পরে, রান করে পরিবর্তনগুলি প্রয়োগ করুন:
source~/.bashrc
macOS - Zsh
Zsh হল একটি সাধারণ Linux এবং macOS টার্মিনাল কনফিগারেশন। নিম্নলিখিত কমান্ডটি চালানোর মাধ্যমে আপনি এটির জন্য একটি কনফিগারেশন ফাইল আছে কিনা তা পরীক্ষা করতে পারেন:
~/.zshrc
যদি প্রতিক্রিয়াটি হয় "এমন কোন ফাইল বা ডিরেক্টরি নেই", তাহলে আপনাকে এই ফাইলটি তৈরি করতে হবে এবং নিম্নলিখিত কমান্ডগুলি চালিয়ে এটি খুলতে হবে, বা bash ব্যবহার করতে হবে:
touch~/.zshrcopen~/.zshrc
এর পরে, আপনাকে নিম্নলিখিত এক্সপোর্ট কমান্ড যোগ করে আপনার API কী সেট করতে হবে:
exportGEMINI_API_KEY=<YOUR_API_KEY_HERE>
ফাইল সংরক্ষণ করার পরে, রান করে পরিবর্তনগুলি প্রয়োগ করুন:
হয় "ব্যবহারকারীর ভেরিয়েবল" (বর্তমান ব্যবহারকারীর জন্য) বা "সিস্টেম ভেরিয়েবল" (সকল ব্যবহারকারীর জন্য - সতর্কতার সাথে ব্যবহার করুন) সম্পাদনা করুন।
ভেরিয়েবল তৈরি করুন এবং export GEMINI_API_KEY=your_key_here যোগ করুন
পরিবর্তনগুলি প্রয়োগ করুন
স্পষ্টভাবে API কী প্রদান করা হচ্ছে
কিছু ক্ষেত্রে, আপনি স্পষ্টভাবে একটি API কী প্রদান করতে চাইতে পারেন। যেমন:
আপনি একটি সাধারণ API কল করছেন এবং API কীকে কঠিন কোডিং পছন্দ করছেন৷
আপনি Gemini API লাইব্রেরি দ্বারা পরিবেশ ভেরিয়েবলের স্বয়ংক্রিয় আবিষ্কারের উপর নির্ভর না করে সুস্পষ্ট নিয়ন্ত্রণ চান
আপনি এমন একটি পরিবেশ ব্যবহার করছেন যেখানে পরিবেশ ভেরিয়েবল সমর্থিত নয় (যেমন ওয়েব) অথবা আপনি REST কল করছেন।
আপনি কীভাবে স্পষ্টভাবে একটি API কী প্রদান করতে পারেন তার উদাহরণ নিচে দেওয়া হল:
পাইথন
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)
জাভাস্ক্রিপ্ট
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())}
জাভা
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());}}
বিশ্রাম
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" } ] } ] }'
আপনার API কী সুরক্ষিত রাখুন
আপনার জেমিনি API কীকে পাসওয়ার্ডের মতো ব্যবহার করুন। যদি আপস করা হয়, অন্যরা আপনার প্রকল্পের কোটা ব্যবহার করতে পারে, চার্জ দিতে পারে (যদি বিলিং সক্ষম করা থাকে), এবং ফাইলগুলির মতো আপনার ব্যক্তিগত ডেটা অ্যাক্সেস করতে পারে।
গুরুতর নিরাপত্তা নিয়ম
সোর্স কন্ট্রোলের জন্য কখনই এপিআই কী কমিট করবেন না। Git এর মত সংস্করণ নিয়ন্ত্রণ সিস্টেমে আপনার API কী চেক করবেন না।
ক্লায়েন্ট-সাইডে কখনই API কীগুলি প্রকাশ করবেন না। প্রডাকশনে ওয়েব বা মোবাইল অ্যাপে সরাসরি আপনার API কী ব্যবহার করবেন না। ক্লায়েন্ট-সাইড কোডের কীগুলি (আমাদের জাভাস্ক্রিপ্ট/টাইপস্ক্রিপ্ট লাইব্রেরি এবং REST কলগুলি সহ) বের করা যেতে পারে।
সর্বোত্তম অনুশীলন
API কীগুলির সাথে সার্ভার-সাইড কলগুলি ব্যবহার করুন আপনার API কী ব্যবহার করার সবচেয়ে নিরাপদ উপায় হল একটি সার্ভার-সাইড অ্যাপ্লিকেশন থেকে Gemini API কল করা যেখানে কীটি গোপন রাখা যেতে পারে৷
ক্লায়েন্ট-সাইড অ্যাক্সেসের জন্য ক্ষণস্থায়ী টোকেন ব্যবহার করুন (কেবল লাইভ API): লাইভ API-তে সরাসরি ক্লায়েন্ট-সাইড অ্যাক্সেসের জন্য, আপনি ক্ষণস্থায়ী টোকেন ব্যবহার করতে পারেন। তারা কম নিরাপত্তা ঝুঁকি নিয়ে আসে এবং উৎপাদন ব্যবহারের জন্য উপযুক্ত হতে পারে। আরও তথ্যের জন্য ক্ষণস্থায়ী টোকেন গাইড পর্যালোচনা করুন।
আপনার কীতে সীমাবদ্ধতা যুক্ত করার কথা বিবেচনা করুন: আপনি 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)."]]