Anthropic Node.js
安装 Anthropic Node.js SDK,发送请求并读取模型回复。
本例使用 Anthropic Messages,请先选择支持该接口的模型与档位。
第 1 步 · 安装 SDK
npm install @anthropic-ai/sdk第 2 步 · 设置环境变量
先从 API Keys 取得匹配模型配置的 Key,再从 Models 复制请求 ID,替换下面两个值。
export UOUO_API_KEY="YOUR_API_KEY"
export UOUO_MODEL="YOUR_MODEL_ID"第 3 步 · 运行最小示例
保存为 hello.mjs,然后执行 node hello.mjs。
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
authToken: process.env.UOUO_API_KEY,
baseURL: "https://uouo.cloud",
});
const response = await client.messages.create({
model: process.env.UOUO_MODEL,
max_tokens: 256,
messages: [{ role: "user", content: "Hello" }],
});
for (const block of response.content) {
if (block.type === "text") console.log(block.text);
}第 4 步 · 验证成功
在同一终端运行文件。看到模型回复后,打开 Logs,核对请求时间、模型与 Key,再到 Usage 查看消耗。
常见问题
- 缺少环境变量:在运行文件的同一终端设置 Key 与模型 ID。
- 401:检查 Key 是否完整、有效。
- 404 / Model not found:检查接口协议、请求 ID 与 Key 权限。
- 429:查看错误体,按 排错指南 处理额度或速率问题。