API 密钥 - TypeScript SDK
client.keys 管理用于客户端认证的 API 密钥。查看到的明文属于敏感凭据,禁止写入日志。
概述
可用操作
| 方法 | 说明 |
|---|---|
list(options?) | 列出可见的 API 密钥;管理员可按所属用户筛选。 |
create(body) | 创建 API 密钥。 |
reveal(id) | 在策略允许时查看明文。 |
update(id, body) | 更新描述、启用状态或过期时间。 |
delete(id) | 删除 API 密钥。 |
list
列出可见的 API 密钥;管理员可按所属用户筛选。
请求
可选参数:{ownerId}。
响应
ApiKeyResponse[].
create
创建 API 密钥。
请求
keyValue 必填;description、isActive 和 expiresAt 可选。密钥值由调用方生成。
响应
ApiKeyResponse;可能包含仅展示一次的 plaintextKey。
reveal
在策略允许时查看明文。
请求
必须提供字符串 id。
响应
string.
update
更新描述、启用状态或过期时间。
请求
必须提供字符串 id 和请求体。
响应
更新后的 ApiKeyResponse。
delete
删除 API 密钥。
请求
必须提供字符串 id。
响应
boolean.
字段参考与示例
请求体支持 id、keyValue、isActive、description、expiresAt 和 createdBy。调用方必须自行生成并提交 keyValue;普通调用方不应设置归属字段。ApiKeyResponse 包含 id、isActive、description、expiresAt、createdBy、userId、掩码处理的 keyValue,以及可能仅展示一次的 plaintextKey。
响应中的 id 为数字,而后续密钥管理方法接受字符串;请使用 String(...) 转换。
const keys = await client.keys.list();
const created = await client.keys.create<{ id: number; plaintextKey?: string }>({
keyValue: "sk-created-by-caller",
description: "automation key",
isActive: true,
});
const keyId = String(created.id);
const plaintext = created.plaintextKey ?? await client.keys.reveal<string>(keyId);
const updated = await client.keys.update(keyId, {
description: "renamed key",
isActive: true,
});
const deleted = await client.keys.delete<boolean>(keyId);