Typescript SDK

将 Alephant 集成到您的 TypeScript 和 Node.js 应用程序中

以 Markdown 格式查看

官方 Alephant TypeScript SDK 让 TypeScript 和 Node.js 应用程序能够访问 Alephant SaaS API。它基于我们的 OpenAPI 规范通过 Fern 生成。

安装

通过 npm、yarn 或 pnpm 安装 SDK:

$npm install @alephantai/saas-api

初始化

使用 SaaS API 基础 URL 和身份验证令牌导入并初始化客户端。传入 Authorization 请求头所需的完整值,例如 Bearer <token>

1import { AlephantSaaSClient } from "@alephantai/saas-api";
2
3const client = new AlephantSaaSClient({
4 baseUrl: process.env.ALEPHANT_SAAS_BASE_URL!,
5 apiKey: `Bearer ${process.env.ALEPHANT_API_TOKEN}`,
6});

使用示例

冒烟测试

使用 health 端点验证 SDK 能否连接到已配置的 SaaS API 主机。

1const health = await client.system.healthCheck();
2console.log(health.data);

列出工作区

获取您的帐户有权访问的所有工作区列表。

1async function fetchWorkspaces() {
2 try {
3 const response = await client.workspaces.listUsersWorkspaces();
4 console.log(response);
5 } catch (error) {
6 console.error("Failed to fetch workspaces:", error);
7 }
8}

创建 Agent 和 Virtual Key

您可以使用 SDK 以编程方式创建新的 AI Agents 及其关联的 Virtual Keys。

1async function createNewAgent() {
2 const newAgent = await client.agents.createAgent({
3 name: "Customer Support Bot",
4 provider: "openai",
5 model: "gpt-4-turbo",
6 environment: "production"
7 });
8
9 console.log(`Created agent with ID: ${newAgent.id}`);
10 // A Virtual Key is automatically generated for this agent
11}

错误处理

SDK 会针对非 2xx API 响应抛出结构化错误,让您能够以编程方式捕获并处理特定失败情形(例如速率限制或验证错误)。

仓库

如需高级配置、类型和源代码,请参阅生成的 SDK 软件包。