Authentication
Get an API Key and configure the endpoint and OpenAI SDK for the OpenAI-compatible API.
Get an API Key
- Open the Model Studio console, sign in with your Alibaba Cloud account, and activate Model Studio.
- On the API Key page, create and copy an API Key.
WarningAPI Keys have account-level permissions. Keep them secure and do not share them publicly or commit them to a code repository.
Use the API Key
Store the API Key in an environment variable and send it in the Authorization header:
export DASHSCOPE_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
curl "https://{workspace_id}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/agents" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "OpenAI-Beta: agents=v1"
OpenAI-Beta: agents=v1 is for compatibility with OpenAI Agents API clients. The API does not use this header to choose defaults.
Endpoint
https://{workspace_id}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
Access uses a workspace-specific domain, and the API Key must belong to the target workspace. The current region is cn-beijing. Replace {workspace_id} with the actual workspace ID.
OpenAI SDK configuration
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DASHSCOPE_API_KEY"],
base_url="https://{workspace_id}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
After configuration, start from Quickstart or Agent.
Permissions
- Alibaba Cloud primary account: can call all APIs.
- RAM user: can call APIs after the required permissions are granted.
- API Key permissions are configured in the console. A production API Key can access only resources in its workspace.
Request ID
Responses include x-request-id in the header. Keep this value for troubleshooting. For the error body structure, see Errors.