密钥 — Python SDK
管理应用调用网关时使用的 API 密钥。
概览
Secret 资源涵盖 API 密钥完整生命周期:查询列表、查看详情、创建、编辑、启用、停用、删除,以及按模型查询用量。还提供模型和供应商目录,用于将密钥绑定到指定上游服务子集。
通过 client.secrets 访问。
可用操作
| 方法 | 说明 |
|---|---|
list() | 分页查询密钥,支持可选筛选条件 |
detail() | 按 ID 获取单个密钥详情 |
create() | 创建密钥并返回生成的 API 密钥 |
edit() | 编辑已有密钥 |
action() | 启用、停用或删除密钥 |
usageByModel() | 按模型查询密钥用量明细 |
modelList() | 查询可绑定密钥的模型 |
providers() | 查询可用于密钥的供应商 |
list
分页查询密钥,支持可选筛选条件。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
# Basic listing
result = client.secrets.list()
print(f"Total secrets: {result.data.totalKeys}")
for secret in result.data.pageInfo.list:
print(secret.id, secret.name, secret.status)
# With filters
result = client.secrets.list(page_num=1, page_size=5, status=1)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| page_num | int | 否 | 1 | 页码,从 1 开始 |
| page_size | int | 否 | 10 | 每页条数 |
| status | int | 否 | None | 状态筛选:1=启用,0=停用 |
| expired_before_utc | datetime | 否 | None | 到期时间上限(UTC),筛选此时间之前到期的密钥 |
响应
返回 BaseResponse[SecretPageResultVO]。
SecretPageResultVO 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| totalKeys | int | 密钥总数 |
| activeKeys | int | 活跃密钥数量 |
| totalRequest | int | 总请求数 |
| avgQps | float | 平均 QPS |
| pageInfo | PageInfo[SecretVO] | 分页密钥数据 |
PageInfo 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| total | int | 总记录数 |
| totalPages | int | 总页数 |
| currentPage | int | 当前页码 |
| pageSize | int | 每页条数 |
| list | list[SecretVO] | 数据列表 |
SecretVO 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| id | int | 密钥 ID |
| name | str | 密钥名称 |
| keyId | str | 密钥标识符 |
| status | str | 状态 |
| createdAt | datetime | 创建时间 |
| lastUsed | datetime | 最近使用时间 |
| expiredFlag | bool | 是否过期 |
| allowedModels | list[str] | 允许的模型列表 |
| allowedProviders | list[str] | 允许的供应商列表 |
| ipWhiteList | list[str] | IP 白名单 |
| minuteTokensQuota | int | 每分钟 Token 配额 |
| annualTokensQuota | int | 年度 Token 配额 |
| description | str | 说明 |
| expiredAtUtc | datetime | 到期时间(UTC) |
| byokList | list[ApikeyVO] | 关联的 BYOK 密钥列表 |
| defaultSecret | int | 是否为默认密钥:1=是,0=否 |
| createUser | str | 创建者 |
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
detail
按 ID 获取单个密钥详情。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
detail = client.secrets.detail(id=123)
print(detail.data.name)
print(detail.data.allowedModels)
print(detail.data.allowedProviders)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 是 | -- | 密钥 ID |
响应
返回 BaseResponse[SecretVO]。
SecretVO 字段见上方
client.secrets.list()。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | 请求参数无效 |
1002 | NotFoundError | 找不到密钥 |
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
create
创建密钥并返回生成的 API 密钥字符串。
create() 不返回数据库记录的数字 id。如需获取,请调用 client.secrets.list(...) 并按 name 匹配,见下方示例末尾的查询。
使用示例
from wlt import WltClient
from datetime import datetime, timezone, timedelta
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
# Minimal creation
result = client.secrets.create(name="my-secret")
print(f"New API key: {result.data}")
# Full creation with all options
result = client.secrets.create(
name="production-key",
allowed_models=["qwen-turbo", "qwen-plus"],
allowed_providers=["dashscope"],
ip_white_list=["10.0.0.0/8"],
minute_tokens_quota=100000,
annual_tokens_quota=50000000,
expired_at_utc=datetime(2027, 1, 1, tzinfo=timezone.utc),
byok_ids=[1, 2],
)
print(f"New API key: {result.data}")
# To obtain the new secret's DB id, look it up by name via list():
page = client.secrets.list(page_num=1, page_size=100)
new_id = next(s.id for s in page.data.list if s.name == "production-key")
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| name | str | 否 | None | 密钥名称 |
| allowed_models | list[str] | 否 | None | 允许的模型名称列表 |
| allowed_providers | list[str] | 否 | None | 允许的供应商名称列表 |
| ip_white_list | list[str] | 否 | None | IP 白名单(CIDR 表示法) |
| minute_tokens_quota | int | 否 | None | 每分钟 Token 配额 |
| annual_tokens_quota | int | 否 | None | 年度 Token 配额 |
| expired_at_utc | datetime | 否 | None | UTC 到期时间 |
| byok_ids | list[int] | 否 | None | 要关联的 BYOK 密钥 ID 列表 |
响应
返回 BaseResponse[str],data 字段包含生成的 API 密钥字符串,即实际密钥值。请立即保存,后端不会再次返回。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
edit
编辑已有密钥。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
result = client.secrets.edit(
id=123,
name="updated-secret-name",
allowed_models=["qwen-turbo", "qwen-max"],
minute_tokens_quota=200000,
)
print(f"Success: {result.data}") # True
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 是 | -- | 密钥 ID |
| name | str | 否 | None | 更新后的名称 |
| allowed_models | list[str] | 否 | None | 更新后的允许模型列表 |
| allowed_providers | list[str] | 否 | None | 更新后的允许供应商列表 |
| ip_white_list | list[str] | 否 | None | 更新后的 IP 白名单 |
| minute_tokens_quota | int | 否 | None | 更新后的每分钟 Token 配额 |
| annual_tokens_quota | int | 否 | None | 更新后的年度 Token 配额 |
| expired_at_utc | datetime | 否 | None | 更新后的到期时间(UTC) |
| byok_ids | list[int] | 否 | None | 更新后的 BYOK 密钥 ID |
响应
返回 BaseResponse[bool],成功时 data=True。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | 请求参数无效 |
1002 | NotFoundError | 找不到密钥 |
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
action
对密钥执行操作:"ENABLE"(启用)、"DISABLE"(停用)或 "DELETE"(删除)。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
# Disable a secret
result = client.secrets.action(id=123, status="DISABLE")
print(f"Success: {result.data}") # True
# Enable a secret
result = client.secrets.action(id=123, status="ENABLE")
# Delete a secret
result = client.secrets.action(id=123, status="DELETE")
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 是 | -- | 密钥 ID |
| status | str | 是 | -- | 操作:"ENABLE"、"DISABLE" 或 "DELETE" |
响应
返回 BaseResponse[bool],成功时 data=True。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | 请求参数无效 |
1002 | NotFoundError | 找不到密钥 |
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
usageByModel
按模型分页查询密钥用量明细。
使用示例
from wlt import WltClient
from datetime import datetime
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
result = client.secrets.usage_by_model(
secret_id=123,
start_time=datetime(2026, 4, 1),
end_time=datetime(2026, 4, 21),
page_num=1,
page_size=10,
)
for item in result.data.list:
print(item.model, item.promptTokens, item.completionTokens)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| secret_id | int | 是 | -- | 密钥 ID |
| start_time | datetime | 否 | None | 查询开始时间 |
| end_time | datetime | 否 | None | 查询结束时间 |
| page_num | int | 否 | 1 | 页码 |
| page_size | int | 否 | 10 | 每页条数 |
响应
返回 BaseResponse[PageInfo[SecretModelUsageVO]]。
SecretModelUsageVO 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| model | str | 模型名称 |
| requests | int | 请求数 |
| totalTokens | int | Token 消耗总量 |
| promptTokens | int | 提示词 Token 数 |
| completionTokens | int | 补全 Token 数 |
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | 请求参数无效 |
1002 | NotFoundError | 找不到密钥 |
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
modelList
查询可绑定密钥的模型。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
# List all inference models
models = client.secrets.model_list(usage_type="inference")
for m in models.data:
print(m.modelName, m.provider)
# Filter by provider
models = client.secrets.model_list(
provider="dashscope",
model_name="qwen",
tags=["New"],
)
for m in models.data:
print(m.modelName)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| model_name | str | 否 | None | 模型名称(模糊搜索) |
| provider | str | 否 | None | 供应商标识符 |
| series_provider | str | 否 | None | 系列供应商标识符 |
| model_type | str | 否 | None | 模型类型 |
| view_all_flag | int | 否 | None | 查看全部标记,1=查看全部 |
| page_size | int | 否 | None | 每页条数 |
| page_num | int | 否 | None | 页码 |
| usage_type | str | 否 | None | 用途类型:"inference" 或 "train" |
| training_method | str | 否 | None | 训练方法:"sft" 或 "dpo" |
| tags | list[str] | 否 | None | 标签筛选列表,例如 ["New"] |
| capability | str | 否 | None | 能力筛选,例如 "text_to_text" |
| model_type_list | list[str] | 否 | None | 模型类型列表(多选) |
| origin_providers | list[str] | 否 | None | 原始供应商列表(多选) |
| inputs | list[str] | 否 | None | 输入类型列表,例如 ["text"] |
| outputs | list[str] | 否 | None | 输出类型列表,例如 ["image"] |
响应
返回 BaseResponse[list[ModelInfoVO]]。
ModelInfoVO 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| modelId | str | 模型 ID |
| modelName | str | 模型名称 |
| modelImage | str | 模型图标 URL |
| isNew | int | 是否为新模型,1=是 |
| provider | str | 供应商标识符 |
| providerName | str | 供应商显示名称 |
| originProvider | str | 原始供应商标识符 |
| seriesProvider | str | 系列供应商标识符 |
| modelType | str | 模型类型 |
| tags | list[str] | 标签列表 |
| modelSize | str | 模型规模 |
| feature | str | 特性描述 |
| price | Decimal | 价格 |
| unit | str | 价格单位 |
| available | bool | 是否可用 |
| updated | datetime | 更新时间 |
| deployPlatform | str | 部署平台 |
| regionId | str | 地域 ID |
| deployFrameworks | list[str] | 部署框架列表 |
| labels | list[str] | 标签列表 |
| capabilities | list[str] | 能力列表,例如 text_to_text |
| inputs | list[str] | 输入类型集合 |
| outputs | list[str] | 输出类型集合 |
| priceExtend | PartnerApiPriceExtend | 扩展价格信息 |
| dataSource | str | 数据来源(model_card 或 model_router) |
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
providers
获取可用于密钥的供应商列表。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
providers = client.secrets.providers()
for p in providers.data:
print(p.id, p.name)
参数
无。
响应
返回 BaseResponse[list[ProviderVO]]。
ProviderVO 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| id | str | 供应商标识符 |
| name | str | 供应商显示名称 |
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |