@affinity-health/sdk

中文文档
2.1.0发布于 2026/11/04MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2394

Official TypeScript SDK for the Affinity API

原始使用文档
文档目录3 节
  1. Install
  2. Use
  3. Build and verify

Affinity TypeScript SDK

Typed server-side client for the Affinity API, for Node.js 20+, Bun, AWS Lambda and other Fetch runtimes. It ships an ES module and a CommonJS build, so import and require() both work. It targets API version 2026-11-01 and sends it in Affinity-Version on every request.

Install

bun add @affinity-health/sdk

For npm, use npm install @affinity-health/sdk.

Use

Keep API keys on the server. Never put them in browser bundles, mobile applications or public environment variables.

import { AffinityClient } from "@affinity-health/sdk";

const affinity = new AffinityClient({ token: process.env.AFFINITY_API_KEY! });
const account = await affinity.account.get();
if (account.mode !== "test") throw new Error("Use a Test key for this example");

for await (const medication of await affinity.medications.list())
  console.log(medication.name, medication.pharmacy.name);

The key sets the organization and the Test or Live mode. Test orders go to a test pharmacy.

  • Writes need an idempotencyKey. Save it with the reviewed order before the first request, and reuse it to retry. The client's automatic retries (2 by default, for 408, 429 and 5xx) reuse it.
  • Lists iterate every page. for await reads each next page; page.data is one page.
  • Errors are typed. Affinity.UnprocessableEntityError carries the issues that name each refused field. Every error extends AffinityError with statusCode, body and requestId.
  • Webhooks verify. verifyAffinityWebhook checks the Affinity-Signature of the raw body.

Read the SDK guide for orders, idempotency, pagination, errors and webhooks, and the API reference for every method. The quickstart reads Test data; the EMR example sends a clinician-reviewed order and receives its webhooks.

Build and verify

Fern generates src/generated from spec/affinity.openapi.json, the Affinity API contract; it runs locally in Docker, with no Fern account. Webhook verification in src/webhook-events.ts is written by hand.

bun install --frozen-lockfile
bun run generate      # regenerate after the contract changes (needs Docker)
bun run check         # build, regenerate-and-compare, types, tests, package and examples
AFFINITY_API_KEY=sk_test_... bun run smoke   # every operation against a real API, Test mode only

bun run smoke checks every response against the contract. It creates a Test practice and a Test order and cancels the order. Set AFFINITY_BASE_URL to call another API, and AFFINITY_SMOKE_WEBHOOK_URL to include webhook endpoints.

版本与发布记录

@affinity-health/sdk 共发布 54 个版本,首次发布于 2026/07/29;过去 12 个月发布了 54 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2026/11/043 天前
2.0.02026/11/044 天前
2.0.0-beta.13next2026/11/034 天前
2.0.0-beta.122026/11/034 天前
2.0.0-beta.112026/11/016 天前
2.0.0-beta.102026/11/016 天前

关于 @affinity-health/sdk 的常见问题

@affinity-health/sdk 是什么?

Official TypeScript SDK for the Affinity API

如何安装 @affinity-health/sdk?

在项目目录运行 npm install @affinity-health/sdk,或使用 pnpm add / yarn add / bun add。

@affinity-health/sdk 支持 TypeScript 吗?

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

@affinity-health/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@affinity-health/sdk 需要什么版本的 Node.js?

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

@affinity-health/sdk 可以商用吗?

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

@affinity-health/sdk 的下载量有多少?

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

@affinity-health/sdk 还在维护吗?

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