ক্ষণস্থায়ী টোকেন হল WebSockets-এর মাধ্যমে Gemini API অ্যাক্সেস করার জন্য স্বল্পস্থায়ী প্রমাণীকরণ টোকেন। আপনি যখন ব্যবহারকারীর ডিভাইস থেকে API (একটি ক্লায়েন্ট-টু-সার্ভার বাস্তবায়ন) এর সাথে সরাসরি সংযোগ করছেন তখন সেগুলি নিরাপত্তা বাড়ানোর জন্য ডিজাইন করা হয়েছে। স্ট্যান্ডার্ড API কীগুলির মতো, ক্ষণস্থায়ী টোকেনগুলি ক্লায়েন্ট-সাইড অ্যাপ্লিকেশন যেমন ওয়েব ব্রাউজার বা মোবাইল অ্যাপ থেকে বের করা যেতে পারে। কিন্তু যেহেতু ক্ষণস্থায়ী টোকেনগুলি দ্রুত মেয়াদোত্তীর্ণ হয় এবং সীমাবদ্ধ করা যেতে পারে, তারা উল্লেখযোগ্যভাবে একটি উত্পাদন পরিবেশে নিরাপত্তা ঝুঁকি হ্রাস করে।
কিভাবে ক্ষণস্থায়ী টোকেন কাজ করে
ক্ষণস্থায়ী টোকেনগুলি কীভাবে উচ্চ স্তরে কাজ করে তা এখানে রয়েছে:
আপনার ক্লায়েন্ট (যেমন ওয়েব অ্যাপ) আপনার ব্যাকএন্ড দিয়ে প্রমাণীকরণ করে।
আপনার ব্যাকএন্ড জেমিনি API এর প্রভিশনিং পরিষেবা থেকে একটি ক্ষণস্থায়ী টোকেন অনুরোধ করে৷
Gemini API একটি স্বল্পস্থায়ী টোকেন ইস্যু করে।
আপনার ব্যাকএন্ড লাইভ API-এ WebSocket সংযোগের জন্য ক্লায়েন্টকে টোকেন পাঠায়। আপনি একটি ক্ষণস্থায়ী টোকেন দিয়ে আপনার API কী অদলবদল করে এটি করতে পারেন।
ক্লায়েন্ট তারপর টোকেন ব্যবহার করে যেন এটি একটি API কী।
এটি নিরাপত্তা বাড়ায় কারণ বের করা হলেও, টোকেনটি স্বল্পস্থায়ী, একটি দীর্ঘস্থায়ী এপিআই কী ক্লায়েন্ট-সাইডের বিপরীতে। যেহেতু ক্লায়েন্ট সরাসরি জেমিনিতে ডেটা পাঠায়, তাই এটি লেটেন্সিও উন্নত করে এবং রিয়েল টাইম ডেটা প্রক্সি করার জন্য আপনার ব্যাকএন্ড এড়ায়।
একটি ক্ষণস্থায়ী টোকেন তৈরি করুন
মিথুন থেকে কীভাবে একটি ক্ষণস্থায়ী টোকেন পেতে হয় তার একটি সরলীকৃত উদাহরণ এখানে। ডিফল্টরূপে, আপনার কাছে এই অনুরোধের টোকেন ব্যবহার করে নতুন লাইভ API সেশন শুরু করার জন্য 1 মিনিট থাকবে ( newSessionExpireTime ), এবং সেই সংযোগে বার্তা পাঠাতে 30 মিনিট সময় থাকবে ( expireTime )৷
পাইথন
importdatetimenow=datetime.datetime.now(tz=datetime.timezone.utc)client=genai.Client(http_options={'api_version':'v1alpha',})token=client.auth_tokens.create(config={'uses':1,# The ephemeral token can only be used to start a single session'expire_time':now+datetime.timedelta(minutes=30),# Default is 30 minutes in the future# 'expire_time': '2025-05-17T00:00:00Z', # Accepts isoformat.'new_session_expire_time':now+datetime.timedelta(minutes=1),# Default 1 minute in the future'http_options':{'api_version':'v1alpha'},})# You'll need to pass the value under token.name back to your client to use it
জাভাস্ক্রিপ্ট
import{GoogleGenAI}from"@google/genai";constclient=newGoogleGenAI({});constexpireTime=newDate(Date.now()+30*60*1000).toISOString();consttoken:AuthToken=awaitclient.authTokens.create({config:{uses:1,// The defaultexpireTime:expireTime// Default is 30 minsnewSessionExpireTime:newDate(Date.now()+(1*60*1000)),// Default 1 minute in the futurehttpOptions:{apiVersion:'v1alpha'},},});
expireTime মান সীমাবদ্ধতা, ডিফল্ট এবং অন্যান্য ফিল্ড স্পেকের জন্য, API রেফারেন্স দেখুন। expireTime সময়সীমার মধ্যে, প্রতি 10 মিনিটে কলটি পুনরায় সংযোগ করার জন্য আপনার sessionResumption প্রয়োজন হবে (এটি একই টোকেন দিয়ে করা যেতে পারে এমনকি যদি uses: 1 )।
কনফিগারেশনের একটি সেটে একটি ক্ষণস্থায়ী টোকেন লক করাও সম্ভব। এটি আপনার অ্যাপ্লিকেশনের নিরাপত্তা আরও উন্নত করতে এবং আপনার সিস্টেমের নির্দেশাবলী সার্ভারের দিকে রাখতে কার্যকর হতে পারে।
পাইথন
client=genai.Client(http_options={'api_version':'v1alpha',})token=client.auth_tokens.create(config={'uses':1,'live_connect_constraints':{'model':'gemini-2.0-flash-live-001','config':{'session_resumption':{},'temperature':0.7,'response_modalities':['TEXT']}},'http_options':{'api_version':'v1alpha'},})# You'll need to pass the value under token.name back to your client to use it
জাভাস্ক্রিপ্ট
import{GoogleGenAI}from"@google/genai";constclient=newGoogleGenAI({});constexpireTime=newDate(Date.now()+30*60*1000).toISOString();consttoken=awaitclient.authTokens.create({config:{uses:1,// The defaultexpireTime:expireTime,liveConnectConstraints:{model:'gemini-2.0-flash-live-001',config:{sessionResumption:{},temperature:0.7,responseModalities:['TEXT']}},httpOptions:{apiVersion:'v1alpha'}}});// You'll need to pass the value under token.name back to your client to use it
এছাড়াও আপনি ক্ষেত্রগুলির একটি উপসেট লক করতে পারেন, আরও তথ্যের জন্য SDK ডকুমেন্টেশন দেখুন৷
একটি ক্ষণস্থায়ী টোকেন সহ লাইভ API-এর সাথে সংযোগ করুন৷
একবার আপনার কাছে একটি ক্ষণস্থায়ী টোকেন হয়ে গেলে, আপনি এটি ব্যবহার করেন যেন এটি একটি API কী (তবে মনে রাখবেন, এটি শুধুমাত্র লাইভ API-এর জন্য এবং শুধুমাত্র API-এর v1alpha সংস্করণের সাথে কাজ করে)।
মনে রাখবেন যে ক্ষণস্থায়ী টোকেনগুলির ব্যবহার শুধুমাত্র ক্লায়েন্ট-টু-সার্ভার বাস্তবায়ন পদ্ধতি অনুসরণ করে এমন অ্যাপ্লিকেশনগুলিকে স্থাপন করার সময় মূল্য যোগ করে।
জাভাস্ক্রিপ্ট
import{GoogleGenAI,Modality}from'@google/genai';// Use the token generated in the "Create an ephemeral token" section hereconstai=newGoogleGenAI({apiKey:token.name});constmodel='gemini-2.0-flash-live-001';constconfig={responseModalities:[Modality.TEXT]};asyncfunctionmain(){constsession=awaitai.live.connect({model:model,config:config,callbacks:{...},});// Send content...session.close();}main();
[[["সহজে বোঝা যায়","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,["# Ephemeral tokens are short-lived authentication tokens for accessing the Gemini\nAPI through [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API). They are designed to enhance security when\nyou are connecting directly from a user's device to the API (a\n[client-to-server](/gemini-api/docs/live#implementation-approach)\nimplementation). Like standard API keys, ephemeral tokens can be extracted from\nclient-side applications such as web browsers or mobile apps. But because\nephemeral tokens expire quickly and can be restricted, they significantly reduce\nthe security risks in a production environment.\n| **Note:** Ephemeral tokens are only compatible with [Live API](/gemini-api/docs/live) at this time. You should use them when accessing the Live API directly from client-side applications to enhance API key security.\n\nHow ephemeral tokens work\n-------------------------\n\nHere's how ephemeral tokens work at a high level:\n\n1. Your client (e.g. web app) authenticates with your backend.\n2. Your backend requests an ephemeral token from Gemini API's provisioning service.\n3. Gemini API issues a short-lived token.\n4. Your backend sends the token to the client for WebSocket connections to Live API. You can do this by swapping your API key with an ephemeral token.\n5. The client then uses the token as if it were an API key.\n\nThis enhances security because even if extracted, the token is short-lived,\nunlike a long-lived API key deployed client-side. Since the client sends data\ndirectly to Gemini, this also improves latency and avoids your backends needing\nto proxy the real time data.\n\nCreate an ephemeral token\n-------------------------\n\nHere is a simplified example of how to get an ephemeral token from Gemini.\nBy default, you'll have 1 minute to start new Live API sessions using the token\nfrom this request (`newSessionExpireTime`), and 30 minutes to send messages over\nthat connection (`expireTime`). \n\n### Python\n\n import datetime\n\n now = datetime.datetime.now(tz=datetime.timezone.utc)\n\n client = genai.Client(\n http_options={'api_version': 'v1alpha',}\n )\n\n token = client.auth_tokens.create(\n config = {\n 'uses': 1, # The ephemeral token can only be used to start a single session\n 'expire_time': now + datetime.timedelta(minutes=30), # Default is 30 minutes in the future\n # 'expire_time': '2025-05-17T00:00:00Z', # Accepts isoformat.\n 'new_session_expire_time': now + datetime.timedelta(minutes=1), # Default 1 minute in the future\n 'http_options': {'api_version': 'v1alpha'},\n }\n )\n\n # You'll need to pass the value under token.name back to your client to use it\n\n### JavaScript\n\n import { GoogleGenAI } from \"@google/genai\";\n\n const client = new GoogleGenAI({});\n const expireTime = new Date(Date.now() + 30 * 60 * 1000).toISOString();\n\n const token: AuthToken = await client.authTokens.create({\n config: {\n uses: 1, // The default\n expireTime: expireTime // Default is 30 mins\n newSessionExpireTime: new Date(Date.now() + (1 * 60 * 1000)), // Default 1 minute in the future\n httpOptions: {apiVersion: 'v1alpha'},\n },\n });\n\nFor `expireTime` value constraints, defaults, and other field specs, see the\n[API reference](https://ai.google.dev/api/live#ephemeral-auth-tokens).\nWithin the `expireTime` timeframe, you'll need\n[`sessionResumption`](/gemini-api/docs/live-session#session-resumption) to\nreconnect the call every 10 minutes (this can be done with the same token even\nif `uses: 1`).\n\nIt's also possible to lock an ephemeral token to a set of configurations. This\nmight be useful to further improve security of your application and keep your\nsystem instructions on the server side. \n\n### Python\n\n client = genai.Client(\n http_options={'api_version': 'v1alpha',}\n )\n\n token = client.auth_tokens.create(\n config = {\n 'uses': 1,\n 'live_connect_constraints': {\n 'model': 'gemini-2.0-flash-live-001',\n 'config': {\n 'session_resumption':{},\n 'temperature':0.7,\n 'response_modalities':['TEXT']\n }\n },\n 'http_options': {'api_version': 'v1alpha'},\n }\n )\n\n # You'll need to pass the value under token.name back to your client to use it\n\n### JavaScript\n\n import { GoogleGenAI } from \"@google/genai\";\n\n const client = new GoogleGenAI({});\n const expireTime = new Date(Date.now() + 30 * 60 * 1000).toISOString();\n\n const token = await client.authTokens.create({\n config: {\n uses: 1, // The default\n expireTime: expireTime,\n liveConnectConstraints: {\n model: 'gemini-2.0-flash-live-001',\n config: {\n sessionResumption: {},\n temperature: 0.7,\n responseModalities: ['TEXT']\n }\n },\n httpOptions: {\n apiVersion: 'v1alpha'\n }\n }\n });\n\n // You'll need to pass the value under token.name back to your client to use it\n\nYou can also lock a subset of fields, see the [SDK documentation](https://googleapis.github.io/python-genai/genai.html#genai.types.CreateAuthTokenConfig.lock_additional_fields)\nfor more info.\n\nConnect to Live API with an ephemeral token\n-------------------------------------------\n\nOnce you have an ephemeral token, you use it as if it were an API key (but\nremember, it only works for the live API, and only with the `v1alpha` version of\nthe API).\n\nNote that use of ephemeral tokens only adds value when deploying applications\nthat follow [client-to-server implementation](/gemini-api/docs/live#implementation-approach) approach. \n\n### JavaScript\n\n import { GoogleGenAI, Modality } from '@google/genai';\n\n // Use the token generated in the \"Create an ephemeral token\" section here\n const ai = new GoogleGenAI({\n apiKey: token.name\n });\n const model = 'gemini-2.0-flash-live-001';\n const config = { responseModalities: [Modality.TEXT] };\n\n async function main() {\n\n const session = await ai.live.connect({\n model: model,\n config: config,\n callbacks: { ... },\n });\n\n // Send content...\n\n session.close();\n }\n\n main();\n\n| **Note:** If not using the SDK, note that ephemeral tokens must either be passed in an `access_token` query parameter, or in an HTTP `Authorization` prefixed by the [auth-scheme](https://datatracker.ietf.org/doc/html/rfc7235#section-2.1) `Token`.\n\nSee [Get started with Live API](/gemini-api/docs/live) for more examples.\n\nBest practices\n--------------\n\n- Set a short expiration duration using the `expire_time` parameter.\n- Tokens expire, requiring re-initiation of the provisioning process.\n- Verify secure authentication for your own backend. Ephemeral tokens will only be as secure as your backend authentication method.\n- Generally, avoid using ephemeral tokens for backend-to-Gemini connections, as this path is typically considered secure.\n\nLimitations\n-----------\n\nEphemeral tokens are only compatible with [Live API](/gemini-api/docs/live) at this time.\n\nWhat's next\n-----------\n\n- Read the Live API [reference](https://ai.google.dev/api/live#ephemeral-auth-tokens) on ephemeral tokens for more information."]]