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

# Claude Haiku 5.5 API

> 使用准确模型 ID 调用 Claude Haiku 5.5，并读取原生响应。

调用 Claude Haiku 5.5 时使用 `model: "claude-haiku-5-5"`。下面通过 Anthropic Messages 发送同步请求。

这些代码用于演示接入方式，不是已执行的模型输出。将密钥保存在服务端环境变量 `BEATAPI_API_KEY` 中；cURL 里的 `<BEATAPI_API_KEY>` 是占位符，需要替换为自己的密钥。不要把密钥放进浏览器代码。

## 发送请求

`POST https://api.beatapi.io/v1/messages`

在服务端项目中安装 `@anthropic-ai/sdk`。

```typescript
import Anthropic from "@anthropic-ai/sdk";

const apiKey = process.env.BEATAPI_API_KEY;
if (!apiKey) throw new Error("Set BEATAPI_API_KEY in your server environment.");

const client = new Anthropic({
  apiKey,
  baseURL: "https://api.beatapi.io",
});

const message = await client.messages.create({
  model: "claude-haiku-5-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Classify this support request as BILLING, TECHNICAL, or ACCOUNT. Return the category and a one-sentence reason. Request: I was charged twice for the same invoice." }],
});

if (message.stop_reason !== "end_turn") {
  throw new Error(`Handle the stop reason before accepting the result: ${message.stop_reason}`);
}
const text = message.content
  .filter((block) => block.type === "text")
  .map((block) => block.text)
  .join("\n");
if (!text.trim()) throw new Error("The response contains no text answer.");
console.log(text);
console.log({ id: message.id, model: message.model, usage: message.usage });
```

`max_tokens: 1024` 是本示例选用的输出预算，不是模型的最大输出上限。SDK 会补上接口路径，因此 `baseURL` 不包含 `/v1`。

### cURL

```bash
curl --request POST \
  --url https://api.beatapi.io/v1/messages \
  --header 'Authorization: Bearer <BEATAPI_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "claude-haiku-5-5",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "Classify this support request as BILLING, TECHNICAL, or ACCOUNT. Return the category and a one-sentence reason. Request: I was charged twice for the same invoice."
    }
  ]
}'
```

## 读取并校验结果

SDK 返回原生 Messages 响应。只从 `type` 为 `text` 的块读取文本，并检查真实响应中的 `stop_reason` 和 `usage`。本例仅接受 `end_turn`；`max_tokens` 表示回答可能被截断，`tool_use` 表示需要应用处理工具请求，不能当作任务已经完成。

HTTP 200 本身不能证明回答满足任务要求，应用还需要校验内容。这类文本请求直接返回结果，不要用 `/v1/tasks` 轮询。

通过 `GET /v1/models` 核对当前账户可用的模型 ID。这个最小示例不承诺额外的推理、采样、缓存控制或托管工具参数。