@apicity/anthropic

中文文档
0.27.0发布于 2026/11/06MITTypeScriptES Module近 30 天下载 4601

Anthropic / Claude 消息、批量、模型、文件和管理员API的提供者.

中文使用文档
文档目录11 节
  1. 安装
  2. 快速启动
  3. 真实世界实例: 多回合 TypeScript 审查, 系统提示 + 几发正弦
  4. API 参考
  5. 文件
  6. 信件
  7. 模型
  8. 欧斯
  9. 技能
  10. 中间软件
  11. 许可证

@apicity/ 人类

npm dependencies TypeScript

Anthropic / Claude 消息、批量、模型、文件和管理员API的提供者.

运行时间依赖性 :

  • zod@^4.6.6 - 请求在每一个 POST 端点上附加计划 .schema

安装

npm install @apicity/anthropic
# or
pnpm add @apicity/anthropic

快速启动

import { createAnthropic } from "@apicity/anthropic";

const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY! });

真实世界实例: 多回合 TypeScript 审查, 系统提示 + 几发正弦

Anthropic Messages API给你的单个最大的把手是 messages 数组本身: 每个前转弯都是下一转的光滑 . 严格地说 system 触发以锁定输出格式并 一个旋转的交换来显示格式,然后得到一个 确定型单行臭虫审查员退出 claude-sonnet-4-6 没有工具 没有JSON模式 没有微调 每一个征兆,模型身份, 下面的内容块来自 tests/recordings/anthropic_2966493235/messages-code-review_891889396/, (中文(简体) ), 剧组重播 tests/integration/anthropic-messages-code-review.test.ts - 直接记录与 https://api.anthropic.com/v1/messages 与 输入 @apicity/anthropic 客户端.

import { createAnthropic } from "@apicity/anthropic";
import type {
  AnthropicMessageResponse,
  AnthropicTextBlock,
} from "@apicity/anthropic";

const anthropic = createAnthropic({
  apiKey: process.env.ANTHROPIC_API_KEY!,
});

// 1. The `system` field is a hard contract for response shape — Claude
//    treats it as higher-priority instruction than anything in
//    `messages`. Spelling out the exact line format here ("BUG: <one
//    sentence>", no preamble, no Markdown) is what stops the model
//    from wrapping the answer in "Sure! Here's the bug:" or fenced
//    code, which it does by default.
//
// 2. The `messages` array is a *transcript*, not a single prompt. The
//    one-shot user→assistant pair below primes the format on a known
//    bug (off-by-one in a loop bound) so the third turn answers in
//    the same shape on a code snippet the model has not seen before.
//    This is few-shot prompting; no fine-tuning required.
const result: AnthropicMessageResponse = await anthropic.v1.messages({
  model: "claude-sonnet-4-6",
  max_tokens: 256,
  system:
    "You are a senior TypeScript reviewer. Reply with exactly one line " +
    "in the form: 'BUG: <one-sentence summary>'. No preamble, no code, " +
    "no Markdown.",
  messages: [
    {
      role: "user",
      content:
        "Review this:\n```ts\nfunction firstNonEmpty(xs: string[]): string {\n" +
        "  for (let i = 0; i <= xs.length; i++) {\n" +
        "    if (xs[i]) return xs[i];\n" +
        "  }\n  return '';\n}\n```",
    },
    {
      role: "assistant",
      content:
        "BUG: The loop condition `i <= xs.length` reads one past the " +
        "last index, so `xs[xs.length]` is dereferenced as undefined.",
    },
    {
      role: "user",
      content:
        "Now review this one the same way:\n```ts\n" +
        "async function readAll(stream: ReadableStream<Uint8Array>): " +
        "Promise<Uint8Array> {\n" +
        "  const reader = stream.getReader();\n" +
        "  const chunks: Uint8Array[] = [];\n" +
        "  while (true) {\n" +
        "    const { done, value } = await reader.read();\n" +
        "    if (done) break;\n    chunks.push(value);\n  }\n" +
        "  return Buffer.concat(chunks);\n}\n```",
    },
  ],
});

// 3. `content` is always an array of typed blocks, never a single
//    string. This is the same shape Claude returns when there are
//    `tool_use`, `thinking`, or `image` blocks in the response —
//    treating it uniformly here means tool-using and thinking
//    workflows drop in without restructuring the read path.
const text = result.content
  .filter((b): b is AnthropicTextBlock => b.type === "text")
  .map((b) => b.text)
  .join("")
  .trim();

console.log(text);
// → "BUG: The reader is never released via `reader.releaseLock()`
//    (especially on error), leaving the stream permanently locked."

console.log(
  `model=${result.model} stop=${result.stop_reason} ` +
    `in=${result.usage.input_tokens} out=${result.usage.output_tokens}`,
);
// → "model=claude-sonnet-4-6 stop=end_turn in=268 out=33"

页:1

  • 这个 system 即时作为软但强的制约 而不是第一次 克劳德把它重新应用为 对话会延长, 格式不会在10个回合后漂移 单一用户信息指令的方式。 把它当作一串 短规则; 通过为 [{ type: "text", text, cache_control: { type: "ephemeral" } }] 以重用时标记为可缓存 同样的漫长的系统会引发许多请求.
  • 几枪打出假枪 assistant 转身最便宜 将输出格式锁定的可靠方式 tools 或者说 结构化输出 API. 克劳德不区分转弯 它实际上生成和转动你写的—— 两者是平等的 权威上下文。 继续转弯 短而准确; 如果 您的系统规则字符换字符( 如 a) 系统说不该有 模型 将树篱.
  • result.content 是一个 (AnthropicTextBlock | AnthropicToolUseBlock | AnthropicThinkingBlock | …)[] 歧视工会。 打开过滤器 b.type === "text" 读取前 .text打开; 打开 b.type === "tool_use" 读取前 .input打开; 打开 b.type === "thinking" 读取前 .thinking克劳德可以把它们分开 反应——例如主要文本块,然后是思维块,然后是 工具 使用- 所以总是太过紧张 永远不要假设 content[0]。 。 。 .
  • usage.input_tokens 包括 整个 记录记录 转来转去 10个回合的谈话 给系统带来好处 每次都是历史的转折 将一个长的系统分期摊还 ( > 1024令牌) 通过许多呼叫,标记它 cache_control: ephemeral 看着 usage.cache_read_input_tokens 接到电话 2–N (英语).
  • 丢出错误 AnthropicError 与 status,解析 body,以及 上游地区 errorType ("校正错误"), "速率 限制 错误","超载 错误",. 与 withRetry 从 @apicity/anthropic 页:1 429 财务报告和审定财务报表 529 overloaded_error, (中文(简体) ), 克劳德在持续负载下返回 与 withFallback 翻转到 claude-haiku-4-5 对于非关键路径.
  • 在克劳德兼容的网关上发出同样的呼声 baseURL (和a) fetch 如果后端使用不同的包装 认证信头比 x-api-key改为: createAnthropic工厂的 要求的形状是上层的,所以任何说 Anthropic Messages 有线格式——Bedrock 代理,第三方 网关,本地取笑——工作时没有触摸到呼叫站点.

API 参考

24个端点跨出5个组. 每个方法都映射出上游的 URL 路径 .

文件

DELETE anthropic.v1.files.del

DELETE https://api.anthropic.com/v1/files/{fileId}

成本等级 : expensive

上游区域

const res = await anthropic.delete.v1.files.del({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.files.content

GET https://api.anthropic.com/v1/files/{fileId}/content

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.files.content({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.files.list

GET https://api.anthropic.com/v1/files

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.files.list({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.files.retrieve

GET https://api.anthropic.com/v1/files/{fileId}

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.files.retrieve({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.files

POST https://api.anthropic.com/v1/files

成本等级 : expensive

上游区域

const res = await anthropic.post.v1.files({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

信件

DELETE anthropic.v1.messages.batches.del

DELETE https://api.anthropic.com/v1/messages/batches/{batchId}

成本等级 : expensive

上游区域

const res = await anthropic.delete.v1.messages.batches.del({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.messages.batches.list

GET https://api.anthropic.com/v1/messages/batches

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.messages.batches.list({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.messages.batches.results

GET https://api.anthropic.com/v1/messages/batches/{batchId}/results

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.messages.batches.results({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.messages.batches.retrieve

GET https://api.anthropic.com/v1/messages/batches/{batchId}

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.messages.batches.retrieve({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.post.stream.v1.messages

POST https://api.anthropic.com/v1/messages

成本等级 : expensive

上游区域

const res = await anthropic.post.stream.v1.messages({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.messages.batches.cancel

POST https://api.anthropic.com/v1/messages/batches/{batchId}/cancel

成本等级 : expensive

上游区域

const res = await anthropic.post.v1.messages.batches.cancel({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.messages

POST https://api.anthropic.com/v1/messages

成本等级 : expensive

上游区域

const res = await anthropic.v1.messages({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.messages.batches

POST https://api.anthropic.com/v1/messages/batches

成本等级 : expensive

上游区域

const res = await anthropic.v1.messages.batches({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.messages.countTokens

POST https://api.anthropic.com/v1/messages/count_tokens

成本等级 : expensive

上游区域

const res = await anthropic.v1.messages.countTokens({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

模型

GET anthropic.v1.models.list

GET https://api.anthropic.com/v1/models

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.models.list({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.models.retrieve

GET https://api.anthropic.com/v1/models/{modelId}

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.models.retrieve({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

欧斯

GET anthropic.api.oauth.usage

GET https://api.anthropic.com/api/oauth/usage

成本等级 : expensive

上游区域

const res = await anthropic.api.oauth.usage();

资料来源: packages/provider/anthropic/src/anthropic.ts

技能

DELETE anthropic.v1.skills.del

DELETE https://api.anthropic.com/v1/skills/{skillId}

成本等级 : expensive

上游区域

const res = await anthropic.delete.v1.skills.del({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

DELETE anthropic.v1.skills.versions.del

DELETE https://api.anthropic.com/v1/skills/{skillId}/versions/{version}

成本等级 : expensive

上游区域

const res = await anthropic.delete.v1.skills.versions.del({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.skills.list

GET https://api.anthropic.com/v1/skills

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.skills.list({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.skills.retrieve

GET https://api.anthropic.com/v1/skills/{skillId}

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.skills.retrieve({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

GET anthropic.v1.skills.versions.list

GET https://api.anthropic.com/v1/skills/{skillId}/versions

成本等级 : expensive

上游区域

const res = await anthropic.get.v1.skills.versions.list({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.skills.create

POST https://api.anthropic.com/v1/skills

成本等级 : expensive

上游区域

const res = await anthropic.post.v1.skills.create({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

POST anthropic.v1.skills.versions.create

POST https://api.anthropic.com/v1/skills/{skillId}/versions

成本等级 : expensive

上游区域

const res = await anthropic.post.v1.skills.versions.create({ /* ... */ });

资料来源: packages/provider/anthropic/src/anthropic.ts

中间软件

import { createAnthropic, withRetry } from "@apicity/anthropic";

const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY! });
const listModels = withRetry(anthropic.get.v1.models.list, { retries: 3 });

部分 辣点 单相机.

许可证

麻省理工学院——见 自由主义。 。 。 .

版本与发布记录

@apicity/anthropic 共发布 90 个版本,首次发布于 2026/05/10;过去 12 个月发布了 90 个版本。

版本发布日期距今
0.27.0latest2026/11/06今天
0.26.02026/11/051 天前
0.25.02026/11/033 天前
0.24.12026/11/024 天前
0.24.02026/11/014 天前
0.23.12026/11/015 天前

关于 @apicity/anthropic 的常见问题

@apicity/anthropic 是什么?

Anthropic / Claude 消息、批量、模型、文件和管理员API的提供者.

如何安装 @apicity/anthropic?

在项目目录运行 npm install @apicity/anthropic,或使用 pnpm add / yarn add / bun add。

@apicity/anthropic 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@apicity/anthropic 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@apicity/anthropic 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apicity/anthropic 可以商用吗?

@apicity/anthropic 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apicity/anthropic 的下载量有多少?

根据 npm 官方下载统计,@apicity/anthropic 在 2026/10/06 – 2026/11/04 这 30 天内被下载 4,601 次,日均约 153 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apicity/anthropic 还在维护吗?

最新版本 0.27.0 发布于 2026/11/06(今天);至今共发布 90 个版本,其中过去 12 个月发布了 90 个;首次发布于 2026/05/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。