创建 API Key

更新时间:
复制 MD 格式

创建 UAC API Key

前提条件

已获取阿里云账号或 RAM 用户的 AccessKey,并已为其授予调用本接口所需的 RAM 权限。建议将 AccessKey 配置为环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET,避免明文写入代码。

接口

POST/tokenplan/api-keys

本接口是阿里云 OpenAPI,采用 AccessKey 签名(签名算法 ACS3-HMAC-SHA256),不支持 Authorization: Bearer {API_KEY} 方式。请求需携带公共请求头 x-acs-action: CreateTokenPlanKey、x-acs-version: 2026-02-10、x-acs-date、x-acs-content-sha256、x-acs-signature-nonce 以及 Authorization。

推荐使用阿里云 SDK 或 OpenAPI Explorer 发起调用,可免去自行计算签名。详细说明见认证。

请求参数

所有参数均通过 Query String 传递。

AccountIdstring必填

账号 ID

Descriptionstring可选

Key 描述

返回参数

Successboolean

是否成功

Codestring

错误码(成功时为空)

Messagestring

错误信息(成功时为空)

Dataobject

业务数据

ApiKeyIdstring

API Key ID(系统生成)

SourceIdstring

BaiLian 返回的 source_id

PlainApiKeystring

BaiLian 返回的明文 API Key(只在创建时返回一次)

MaskedApiKeystring

BaiLian 返回的 masked_api_key(如 sk_***cdef)

Descriptionstring

Key 描述

CreatedAtstring

Key 创建时间

请求示例

本接口使用阿里云 OpenAPI 签名,下例中 ${SIGNATURE} 需按 ACS3-HMAC-SHA256 算法计算得出,x-acs-date、x-acs-signature-nonce 需替换为实际值。示例 URL 中的中文取值为便于阅读未做编码;实际计算签名时,规范化请求(CanonicalRequest)的 Query String 需按 RFC 3986 对键和取值做百分号编码,否则会返回 SignatureDoesNotMatch。使用阿里云 SDK 时会自动处理。

curl -X POST "https://modelstudio.cn-beijing.aliyuncs.com/tokenplan/api-keys?AccountId=acc_123456789&Description=APIKEY描述" \
    --header "x-acs-action: CreateTokenPlanKey" \
    --header "x-acs-version: 2026-02-10" \
    --header "x-acs-date: 2026-01-01T12:00:00Z" \
    --header "x-acs-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" \
    --header "x-acs-signature-nonce: ${SIGNATURE_NONCE}" \
    --header "Authorization: ACS3-HMAC-SHA256 Credential=${ALIBABA_CLOUD_ACCESS_KEY_ID},SignedHeaders=host;x-acs-action;x-acs-content-sha256;x-acs-date;x-acs-signature-nonce;x-acs-version,Signature=${SIGNATURE}"
import os

from alibabacloud_modelstudio20260210.client import Client
from alibabacloud_modelstudio20260210 import models
from alibabacloud_tea_openapi import models as open_api_models

config = open_api_models.Config(
    access_key_id=os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],
    access_key_secret=os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
    endpoint="modelstudio.cn-beijing.aliyuncs.com",
)
client = Client(config)

request = models.CreateTokenPlanKeyRequest(
    account_id="acc_123456789",
    description="APIKEY描述",
)
response = client.create_token_plan_key(request)
print(response.body)
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.modelstudio20260210.AsyncClient;
import com.aliyun.sdk.service.modelstudio20260210.DefaultAsyncClientBuilder;
import com.aliyun.sdk.service.modelstudio20260210.models.CreateTokenPlanKeyRequest;
import com.aliyun.sdk.service.modelstudio20260210.models.CreateTokenPlanKeyResponse;

public class CreateTokenPlanKey {
    public static void main(String[] args) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
            .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
            .build());
        AsyncClient client = new DefaultAsyncClientBuilder()
            .credentialsProvider(provider)
            .region("cn-beijing")
            .build();

        CreateTokenPlanKeyRequest request = CreateTokenPlanKeyRequest.builder()
            .accountId("acc_123456789")
            .description("APIKEY描述")
            .build();
        CreateTokenPlanKeyResponse response = client.createTokenPlanKey(request).get();
        System.out.println(response.getBody());
        client.close();
    }
}

返回示例

{
  "Success": true,
  "Data": {
    "ApiKeyId": "ak-123456",
    "SourceId": "123",
    "PlainApiKey": "sk-ws.abc123456",
    "MaskedApiKey": "sk_***6",
    "Description": "APIKEY描述",
    "CreatedAt": "2025-07-18T03:19:17Z"
  }
}

错误码

如果调用失败,会返回错误信息。更多错误码及解决方法,请参见Token Plan API 错误码。