在指定 Vault 中创建一个 Credential。auth 支持 environment_variable 与 env_oauth 两种类型。secret_value 等敏感值仅写入时传入,响应中不返回。
前提
已完成 Endpoint 与鉴权配置,详见API 总览与认证。
接口
/vaults/{vault_id}/credentials路径参数
vault_idstring必填Vault ID,格式 vlt_xxx
请求体
authobject必填认证信息,支持 environment_variable 与 env_oauth 两种类型。
display_namestring可选Credential 可读名称,最大 255 字符
metadataobject可选业务自定义键值对元数据
auth 类型
auth 支持 environment_variable 与 env_oauth 两种类型:
environment_variable:以环境变量形式注入会话,请求时由 Agent 直接读取该环境变量。env_oauth:通过钉钉 OAuth 授权获取 Access Token,由 Credential 在出网请求时把凭证注入到指定位置,并在 Access Token 失效时自动刷新。适用于无需在代码中持有 Token 的第三方系统调用场景。
typestring必填固定为 environment_variable 或 env_oauth
secret_namestring必填环境变量名称,需满足 ^[a-zA-Z][a-zA-Z0-9_]*$:以字母开头,仅支持字母、数字和下划线。创建后不可修改。钉钉授权方式下默认使用 DINGTALK_USER_ACCESS_TOKEN
secret_valuestring可选环境变量值,仅 environment_variable 类型需要传入;仅写入时传入,响应中不返回
modestring仅 env_oauth 必填env_oauth 类型的 Token 获取方式:INIT_BY_ACCESS_TOKEN 表示直接填写已有 Access Token;INIT_BY_CLIENT_ID_SECRET_ID 表示通过 Client ID 与 Client Secret 换取 Token
providerstring仅 env_oauth 必填OAuth 提供方,取值 dingtalk(钉钉)或 standard(自定义 OAuth)
access_tokenstring可选已有的 Access Token,mode 为 INIT_BY_ACCESS_TOKEN 时传入;仅写入时传入,响应中不返回
refreshobject可选Token 刷新配置,mode 为 INIT_BY_CLIENT_ID_SECRET_ID 时传入
refresh.client_idstringOAuth 应用的 Client ID
refresh.client_secretstringOAuth 应用的 Client Secret,仅写入时传入,响应中不返回
refresh.token_endpointstring获取 Token 的 Endpoint 地址
refresh.token_endpoint_authobjectEndpoint 认证方式,类型取 client_secret_basic 或 client_secret_post
refresh.refresh_tokenstring已有的 Refresh Token,可选;仅写入时传入,响应中不返回
networkingobject必填网络策略,限制只有发往指定域名的请求才会被替换为真实密钥
networking.allowed_hostsarray必填允许替换真实密钥的域名列表,支持 *.example.com 通配子域,最多 10 个。填 * 对全部域名生效
injection_locationobject必填替换位置,标识密钥写入请求的位置。替换位置为请求头;请求体与查询参数中的占位符不会被替换
injection_location.headerboolean可选是否启用请求头替换,固定传 true
说明同一 Vault 内 secret_name 不能重复,否则返回 409 CREDENTIAL_SECRET_NAME_DUPLICATE“该Vault下已存在相同变量名称的凭证”。
说明env_oauth 类型下,access_token、refresh.client_secret 等敏感值同样仅写入时传入,响应与查询接口均不返回真实值。
请求示例
curl -X POST "$AGENTSTUDIO_URL/vaults/vlt_xxx/credentials" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"display_name": "第三方 API 密钥",
"auth": {
"type": "environment_variable",
"secret_name": "API_KEY",
"secret_value": "sk-xxxx",
"networking": {
"allowed_hosts": [
"api.example.com",
"*.example.com"
]
},
"injection_location": {
"header": true
}
}
}'
curl -X POST "$AGENTSTUDIO_URL/vaults/vlt_xxx/credentials" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"display_name": "钉钉授权凭证",
"auth": {
"type": "env_oauth",
"mode": "INIT_BY_ACCESS_TOKEN",
"provider": "dingtalk",
"secret_name": "DINGTALK_USER_ACCESS_TOKEN",
"access_token": "xxxxxxxx",
"networking": {
"allowed_hosts": [
"api.dingtalk.com"
]
},
"injection_location": {
"header": true
}
}
}'
curl -X POST "$AGENTSTUDIO_URL/vaults/vlt_xxx/credentials" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"display_name": "自定义 OAuth 凭证",
"auth": {
"type": "env_oauth",
"mode": "INIT_BY_CLIENT_ID_SECRET_ID",
"provider": "standard",
"secret_name": "THIRD_PARTY_ACCESS_TOKEN",
"refresh": {
"client_id": "client_xxx",
"client_secret": "secret_xxx",
"token_endpoint": "https://auth.example.com/oauth/token",
"token_endpoint_auth": {
"type": "client_secret_basic"
}
},
"networking": {
"allowed_hosts": [
"openapi.example.com"
]
},
"injection_location": {
"header": true
}
}
}'
响应字段
idstringCredential ID,格式 cred_xxx
typestring固定为 credential
vault_idstring所属 Vault ID
display_namestringCredential 可读名称
authobject认证信息,敏感值已脱敏(如 secret_value 不返回)
auth.typestring认证类型,取值 environment_variable 或 env_oauth
auth.secret_namestring环境变量名称
auth.providerstringenv_oauth 类型的 OAuth 提供方,取值 dingtalk 或 standard
auth.access_tokenstringenv_oauth 类型已配置 Access Token 时返回,值为脱敏占位,不返回真实 Token
auth.refreshobjectenv_oauth 类型的 Token 刷新配置,仅返回 client_id、token_endpoint 与 token_endpoint_auth,不返回 secret
auth.networkingobject网络策略
auth.networking.allowed_hostsarray允许替换真实密钥的域名列表
auth.injection_locationobject替换位置。替换位置为请求头;请求体与查询参数中的占位符不会被替换
auth.injection_location.headerboolean是否启用请求头替换
metadataobject业务自定义键值对
archived_atstring | null归档时间,未归档时为 null
created_atstring创建时间,ISO 8601
updated_atstring最近更新时间,ISO 8601
request_idstring请求唯一标识
响应示例
{
"id": "cred_xxx",
"type": "credential",
"vault_id": "vlt_xxx",
"display_name": "第三方 API 密钥",
"auth": {
"type": "environment_variable",
"secret_name": "API_KEY",
"networking": {
"allowed_hosts": [
"api.example.com",
"*.example.com"
]
},
"injection_location": {
"header": true
}
},
"metadata": {},
"archived_at": null,
"created_at": "2026-07-10T08:00:00Z",
"updated_at": "2026-07-10T08:00:00Z",
"request_id": "req_xxx"
}
错误码
| 状态码 | 错误码 | 说明 |
|---|---|---|
| 400 | InvalidParameter | Required parameter missing or invalid. |
| 401 | InvalidApiKey | Invalid API-key provided. |
| 409 | CREDENTIAL_SECRET_NAME_DUPLICATE | secret_name 在该 Vault 内已存在。 |