TypeScript SDK
API Router 控制台 API 的官方 TypeScript 客户端库。
- Node.js:>= 16.0.0
- TypeScript:>= 5.0,用于开发
为什么使用 SDK
- 完整 TypeScript 类型支持:每个请求和响应均提供完整类型,SDK 自带
.d.ts文件。 - 透明认证:内部将 API 密钥交换为 Bearer 令牌,过期时自动刷新,无需手动调用
loginByApiKey。 - 支持 SSE:
modelLab.stream()返回遍历响应数据块的原生异步迭代器。 - CJS 与 ESM:同时支持使用
require和import的项目。
安装
npm install wlt-platform
# or
yarn add wlt-platform
pnpm add wlt-platform
快速开始
import { WltClient } from 'wlt-platform';
const client = new WltClient({
apiKey: 'your-api-key',
baseUrl: 'https://your-console-host',
});
// List your API keys (Secrets)
const result = await client.secrets.list({ pageNum: 1, pageSize: 20 });
console.log(result.pageInfo.list);
// Stream a chat completion
for await (const chunk of client.modelLab.stream({
provider: 'dashscope',
modelName: 'Qwen2.5-72B-Instruct',
userPrompt: 'Hello!',
})) {
process.stdout.write(chunk);
}
配置
将 baseUrl 设置为部署的 WLT 管理端绝对源地址,例如 https://your-console-host,该地址必须提供 /xt-console/api/...。不要包含 /msp-console 等控制台页面路径、/xt-console/api 路径或推理 /v1 前缀;SDK 会自行附加管理路径。
import { WltClient } from 'wlt-platform';
const client = new WltClient({
apiKey: 'your-api-key', // required
baseUrl: 'https://your-console-host',
timeout: 60_000, // default 30000
maxRetries: 3, // default 2
defaultHeaders: { 'X-Custom': 'v' },
});
基础 URL 解析
优先级从高到低:
| 优先级 | 来源 |
|---|---|
| 1 | 构造函数 baseUrl 参数 |
| 2 | WLT_BASE_URL 环境变量 |
| 3 | ~/.wlt/config.json 中的 baseUrl / base_url,仅适用于 CLI 用户 |
| 4 | 内置默认值 |
末尾斜杠会自动移除。
模块参考
| 模块 | 访问方式 | 方法数 |
|---|---|---|
| 用户 | client.user | 1 |
| 密钥 | client.secrets | 8 |
| API 密钥 | client.apiKeys | 7 |
| 用量 | client.usage | 4 |
| 账单 | client.billing | 4 |
| 支付 | client.payment | 5 |
| 模型广场 | client.modelGallery | 3 |
| 模型实验室 | client.modelLab | 3 |
管理员范围
独立的 AdminClient 处理管理员范围操作,包括账单报告、终端客户增删改查、模型定价、额度、速率限制和上下文记录。它通过管理员用户名和密码认证,无需验证码或 API 密钥,并签发仅适用于管理员端点的会话令牌。调用方需要以管理员身份操作时,可与 WltClient 一起创建此客户端。
import { AdminClient } from 'wlt-platform';
const admin = new AdminClient({
username: 'admin',
password: 'your-admin-password',
baseUrl: 'https://your-console-host',
});
const info = await admin.auth.info(); // admin identity
const totals = await admin.billing.customerTotal({ // billing rollup
startTime: '2026-06-01 00:00:00',
endTime: '2026-06-30 23:59:59',
});
const customers = await admin.customer.list({ pageNum: 1, pageSize: 20 }); // customer CRUD
| 模块 | 访问方式 | 方法数 |
|---|---|---|
| 认证 | admin.auth | 1 |
| 账单 | admin.billing | 6 |
| 客户 | admin.customer | 3 |
| 定价 | admin.pricing | 9 |
| 额度 | admin.credits | 2 |
| 速率限制 | admin.rateLimit | 2 |
| 上下文 | admin.context | 3 |