> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://developers.alephant.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://developers.alephant.io/_mcp/server.

# Typescript SDK

> Alephant Analytics TypeScript SDK 的安装、配置和使用

官方 Alephant Analytics TypeScript SDK 让 TypeScript 和 Node.js 应用程序能够访问 Alephant Analytics API。它让您可以查询遥测数据、获取每日用量序列，并检索用于 FinOps 仪表板的实时汇总统计信息。

## 安装

通过 npm、yarn 或 pnpm 安装 SDK：

```bash
npm install @alephantai/logs-collector-analytics
```

## 初始化

导入并初始化客户端。默认环境为开发环境（`https://analytics-dev.alephant.io`）。仅在生产 Analytics 服务已部署后使用生产环境。

```typescript
import {
  LogsCollectorAnalyticsClient,
  LogsCollectorAnalyticsEnvironment,
} from "@alephantai/logs-collector-analytics";

const client = new LogsCollectorAnalyticsClient({
  environment: LogsCollectorAnalyticsEnvironment.Development,
});
```

## 使用示例

### 冒烟测试

使用公开 health 端点验证 SDK 能否连接到 Analytics 开发服务。

```typescript
const health = await client.analyticsAtomic.getHealth();
console.log(health.data.status);
```

### 实时 24 小时面板

获取滚动实时 24 小时仪表板面板。经过身份验证的 SaaS analytics 路由接受作为请求字段的 `Authorization` 和 `X-Workspace-Id` 请求头。

```typescript
async function fetchLivePanel() {
  try {
    const response = await client.analyticsSaas.getSaasLive24H({
      limit: 5,
      Authorization: `Bearer ${process.env.ALEPHANT_API_TOKEN}`,
      "X-Workspace-Id": process.env.ALEPHANT_WORKSPACE_ID,
    });

    console.log(response.data);
  } catch (error) {
    console.error("Failed to fetch live panel:", error);
  }
}
```

### 获取用量序列

获取指定日期范围内的每日用量指标。

```typescript
async function fetchCostTimeseries() {
  const response = await client.analyticsSaas.getSaasUsage({
    dateFrom: "2026-03-01",
    dateTo: "2026-03-31",
    Authorization: `Bearer ${process.env.ALEPHANT_API_TOKEN}`,
    "X-Workspace-Id": process.env.ALEPHANT_WORKSPACE_ID,
  });

  console.log(response.data);
}
```

## 错误处理

SDK 会针对非 2xx API 响应抛出结构化错误，让您能够以编程方式妥善处理身份验证、速率限制或验证错误。