@bettercms-ai/sdk

中文文档
1.42.1发布于 2026/11/03TypeScriptES Module近 30 天下载 4994

Typed JavaScript/TypeScript client for the BetterCMS Delivery API.

原始使用文档
文档目录12 节
  1. Installation
  2. Quick start
  3. With an API key
  4. TypeScript
  5. Error handling
  6. Draft previews and visual editing
  7. Rich text
  8. API
  9. BetterCMS.site(options)
  10. client.getContent(slug)
  11. client.listContent(options?)
  12. client.listContentAll(options?)

@bettercms-ai/sdk

Typed JavaScript/TypeScript client for the BetterCMS Delivery API.

Installation

npm install @bettercms-ai/sdk
# or
pnpm add @bettercms-ai/sdk
# or
yarn add @bettercms-ai/sdk

Quick start

import { BetterCMS } from "@bettercms-ai/sdk";

const client = BetterCMS.site({ workspace: "my-workspace" });

// Fetch a single page
const page = await client.getContent("home");
console.log(page.entry.title);

// List pages (paginated)
const result = await client.listContent({ page: 1, perPage: 20 });
console.log(result.items);
console.log(`Next page? ${result.hasNextPage}`);

// Iterate all pages automatically
for await (const item of client.listContentAll()) {
  console.log(item.entry.title);
}

// Collect with a limit
const top = await client.listContentAll().all({ limit: 500 });

With an API key

const client = BetterCMS.site({
  workspace: "my-workspace",
  apiKey: process.env.BETTERCMS_API_KEY,
});

TypeScript

This package is written in TypeScript and ships its own type declarations. No separate @types/ package needed. All types from @bettercms-ai/types are re-exported.

Error handling

The SDK throws BetterCMSError on failures. Each error has a semantic code:

import { BetterCMSError, ErrorCodes } from "@bettercms-ai/sdk";

try {
  const page = await client.getContent("does-not-exist");
} catch (err) {
  if (err instanceof BetterCMSError) {
    switch (err.code) {
      case ErrorCodes.CONTENT_NOT_FOUND:
        // 404
        break;
      case ErrorCodes.UNAUTHORIZED:
        // 401
        break;
      case ErrorCodes.FORBIDDEN:
        // 403
        break;
      case ErrorCodes.RATE_LIMITED:
        // 429
        break;
      case ErrorCodes.INTERNAL_ERROR:
        // 5xx
        break;
      case ErrorCodes.NETWORK_ERROR:
        // Network failure
        break;
    }
  }
}

Draft previews and visual editing

A client created with perspective: "drafts" reads unpublished content — through a content:read:draft API key, or through a previewToken minted for one document.

Draft reads also carry stega provenance: invisible zero-width characters appended to every prose value, naming the entry and field it came from. That is what lets the dashboard's canvas (and the <VisualEditing> overlay) map a rendered string back to its editor without guessing at its text. It is on by default for perspective: "drafts" from 1.15.0 — before that it had to be asked for, and a preview build that did not know to ask rendered anonymous strings.

const draft = createClient({
  apiUrl,
  workspace: "my-workspace",
  apiKey: process.env.BETTERCMS_DRAFT_KEY,
  perspective: "drafts", // stega is on
});

// Opt out — e.g. when diffing or checksumming draft content, where the invisible
// characters would show up as differences.
const plain = createClient({
  apiUrl,
  workspace: "my-workspace",
  apiKey: process.env.BETTERCMS_DRAFT_KEY,
  perspective: "drafts",
  stega: false,
});

Published reads never carry provenance, whatever stega is set to: the payload must not reach a production build. Strip it yourself with stripStega(value) if you need the clean string, and decode it with decodeStega(value).

Rich text

A rich-text field arrives as { format, value, html } (or as a bare string, if the field was switched from text). Render html with rich(), or render the structure (value) yourself:

import { rich, plain, portableText, embeds, resolved } from "@bettercms-ai/sdk";

const entry = await client.getEntry<{ body: RichTextValue }>("launch");

rich(entry.data.body);          // HTML for set:html / dangerouslySetInnerHTML: everything drawn
plain(entry.data.body);         // text for <title>, meta description, alt

// Rendering the structure with @bettercms-ai/ui (or your own components):
<PortableText
  value={portableText(entry.data.body) ?? entry.data.body}
  embeds={embeds(entry.data.body) ?? undefined}
  resolved={resolved(entry.data.body, entry) ?? undefined}
/>;

Related posts and author bios are resolved by the server for each response, from the published entries, and never stored. An envelope value carries what it resolved to (value.resolved); a Body stored as a bare block array cannot, so the entry carries the merged map beside data (entry.resolved). resolved(value, entry) returns the value's own map, else the entry's.

API

BetterCMS.site(options)

OptionTypeRequiredDefault
workspacestringYes—
apiKeystringNo—
baseUrlstringNohttps://api.bettercms.ai/api/v1/delivery

Returns a BetterCMSDeliveryClient instance.

client.getContent(slug)

Fetches a single published page by slug.

const content = await client.getContent("about-us");

client.listContent(options?)

Returns a paginated result object.

const result = await client.listContent({ page: 1, perPage: 20 });
// result.items, result.hasNextPage, result.totalPages, ...

client.listContentAll(options?)

Returns an async iterator that transparently follows pagination.

for await (const item of client.listContentAll()) { ... }

// With a limit:
const items = await client.listContentAll().all({ limit: 100 });

版本与发布记录

@bettercms-ai/sdk 共发布 51 个版本,首次发布于 2026/06/30;过去 12 个月发布了 51 个版本。

版本发布日期距今
1.42.1latest2026/11/035 天前
1.42.02026/11/026 天前
1.41.22026/10/2712 天前
1.41.12026/10/2712 天前
1.41.02026/10/2613 天前
1.40.02026/10/2613 天前

关于 @bettercms-ai/sdk 的常见问题

@bettercms-ai/sdk 是什么?

Typed JavaScript/TypeScript client for the BetterCMS Delivery API.

如何安装 @bettercms-ai/sdk?

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

@bettercms-ai/sdk 支持 TypeScript 吗?

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

@bettercms-ai/sdk 支持 ESM 还是 CommonJS?

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

@bettercms-ai/sdk 可以商用吗?

@bettercms-ai/sdk 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@bettercms-ai/sdk 的下载量有多少?

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

@bettercms-ai/sdk 还在维护吗?

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