To call model services from untrusted environments such as browsers and mobile apps, use a secure backend service to generate temporary API keys. This prevents your permanent API key from being exposed.
Prerequisites
Get a permanent API key on the Key Management (Singapore), Key Management (Virginia), Key Management (Beijing), or Key Management (China (Hong Kong)) page. Then set the key as the DASHSCOPE_API_KEY environment variable. For more information, see Configure an API key as an environment variable.
Request example
By default, a temporary API key is valid for 60 seconds. Set a custom time-to-live (TTL) between 1 and 1,800 seconds using the expire_in_seconds parameter.
API keys differ by region.
The following example uses the Singapore region endpoint. Replace WorkspaceId with your actual workspace ID. For the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/tokens?expire_in_seconds=1800
Response example
- Successful response example
- Error response example
Response parameters
Parameter | Type | Description | Example |
|---|---|---|---|
token | String | Generated temporary API key. | st-**** |
expires_at | Number | Expiration time as a UNIX timestamp, in seconds. | 1744080369 |