创建 Credential

更新时间:
复制 MD 格式

在指定 Vault 中创建一个 Credential。auth 支持 environment_variable 与 env_oauth 两种类型。secret_value 等敏感值仅写入时传入,响应中不返回。

前提

已完成 Endpoint 与鉴权配置,详见API 总览与认证。

接口

POST/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_idstring

OAuth 应用的 Client ID

refresh.client_secretstring

OAuth 应用的 Client Secret,仅写入时传入,响应中不返回

refresh.token_endpointstring

获取 Token 的 Endpoint 地址

refresh.token_endpoint_authobject

Endpoint 认证方式,类型取 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
      }
    }
  }'

响应字段

idstring

Credential ID,格式 cred_xxx

typestring

固定为 credential

vault_idstring

所属 Vault ID

display_namestring

Credential 可读名称

authobject

认证信息,敏感值已脱敏(如 secret_value 不返回)

auth.typestring

认证类型,取值 environment_variable 或 env_oauth

auth.secret_namestring

环境变量名称

auth.providerstring

env_oauth 类型的 OAuth 提供方,取值 dingtalk 或 standard

auth.access_tokenstring

env_oauth 类型已配置 Access Token 时返回,值为脱敏占位,不返回真实 Token

auth.refreshobject

env_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"
}

错误码

状态码错误码说明
400InvalidParameterRequired parameter missing or invalid.
401InvalidApiKeyInvalid API-key provided.
409CREDENTIAL_SECRET_NAME_DUPLICATEsecret_name 在该 Vault 内已存在。