@cookiemunch/sdk

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

Cookie Munch 开发者 API (/v1) 的 REST 客户端

中文使用文档
文档目录3 节
  1. 方法
  2. 正在校验webhooks
  3. 答复权利请求

@cookiemunch/ sdk (英语)

为 Cookie Munch 开发者 API (the /v1 (原始内容存档于2018-10-21). space. 使用 API 密钥进行认证——组织从密钥中衍生出服务器侧, 所以你从不通过 orgId。 。 。 .

import { createCookieMunch, CookieMunchApiError } from '@cookiemunch/sdk';

const fc = createCookieMunch({
  apiKey: process.env.COOKIEMUNCH_API_KEY!, // "fck_…"
  baseUrl: 'https://api.cookiemunch.net',     // "/v1" is appended automatically
});

const sites = await fc.sites.list();
const site = await fc.sites.create({ domain: 'example.com' }); // cbid auto-generated
const stats = await fc.consent.stats(site.cbid, { from: 0, to: Date.now() });

try {
  await fc.sites.get('does-not-exist');
} catch (e) {
  if (e instanceof CookieMunchApiError) console.error(e.status, e.message);
}

fetch 可通过 createCookieMunch({ ..., fetch }) 测试或定制 运行时间; 它默认为全局 fetch。 。 。 .

方法

  • me()
  • sites.list() / create(input) / get(cbid) / delete(cbid) / getConfig(cbid) / putConfig(cbid, config)
  • sites.lintConfig(cbid, config, { replace? }) ——同意-按钮对等:接受所有比拒绝所有更难接受吗? 回返 designWarnings, (中文(简体) ), score, (中文(简体) ), suggestedReject; 什么都没有保存。 每个配置读入/写入 designWarnings。 。 。 .
  • consent.stats(cbid, {from?,to?}) / log(cbid, {from?,to?,limit?}) / export(cbid, {from?,to?}) / receipt(cbid, stamp)
  • dsar.list() / create(input) / advance(id, toStatus)
  • vendors.list() / create(input)
  • ropa.list() / create(input)
  • webhooks.list() / create(input)
  • rightsPage.get(cbid) / put(cbid, page, {baseUpdatedAt?}) / patch(cbid, patch, {baseUpdatedAt?}) / dryRun(cbid, page, {mode?}) / getPublished(cbid) / publish(cbid, {baseUpdatedAt?}) / versions(cbid) / rollback(cbid, version, {baseUpdatedAt?}) - 通过草案 updatedAt 作为 baseUpdatedAt 因此,更新的储蓄是409 draft_conflict,从不覆盖或未公开;回滚取代草稿
  • rightsPage.listDomains(cbid) / addDomain(cbid, hostname) / verifyDomain(cbid, hostname) / removeDomain(cbid, hostname) ——您自己的主机名上的权利页面
  • rightsLinks.create(cbid, { subject, rights?, ttlSeconds? }) ——签注人单用权页面链接
  • org.mailProvider.get() / set(input) / test(to?) / remove() - 您自己的权利请求邮件提供者

这些是使用最多的;客户端覆盖每个 /v1 操作(见类型) CookieMunchClient) (中文(简体) ).

export() 返回原始 CSV 字符串; 所有其他方法返回已解析的 JSON。 任意 非-2xx 响应抛出 a CookieMunchApiError { status, code?, message }错误 code 数值在 docs/api/error-codes.md。 。 。 .

正在校验webhooks

verifyWebhookSignature(...) 验证输入的 webhook 的 X-CookieMunch-Signature (HMAC-SHA256 完毕) `${timestamp}.${rawBody}`和它的新鲜。 这是权威 验证器——总是匹配服务器. 未关闭; 返回布尔值 .

import { verifyWebhookSignature } from '@cookiemunch/sdk';

const ok = verifyWebhookSignature({
  payload: rawBody,                          // exact request body bytes (a string)
  signature: headers['x-cookiemunch-signature'],
  timestamp: headers['x-cookiemunch-timestamp'],
  secret: process.env.CM_WEBHOOK_SECRET!,    // the whsec_… shown once on create
});

全食谱( Express, 边缘/ WebCrypto, 重放保护) : docs/api/webhook-signing.md。 。 。 .

答复权利请求

createRightsHandlers(...) 从 @cookiemunch/sdk/rights 服务于 cm-标准v2执行合同 获取 API 处理器从您的召回。 Cookie Munch随后发现,输出并抹去你产品中的人.

import { createRightsHandlers } from '@cookiemunch/sdk/rights';

export const { GET, POST } = createRightsHandlers({
  key: process.env.CM_EXECUTOR_KEY!,           // the secretKey you registered
  idSecret: process.env.CM_REQUEST_ID_SECRET!,  // signs request ids; 32+ characters
  lookup: ({ emails, identity }) => findUserId(emails, identity),
  access: async (r) => ({ status: 'completed', result: await exportUser(r.subjectId) }),
  erase: async (r) => { await deleteUser(r.subjectId); },
  store: fileStore('/var/lib/myapp/rights'), // import it too; or sqlStore({ query }) / redisStore(client)
});

默认商店在记忆中. 预定的去除过程没有定时器 : erase 运行 Cookie Munch的下一场民调之后 executeAfter (每10分钟),所以生产需要耐用 store数字 : fileStore(dir), (中文(简体) ), sqlStore({ query }) (表: RIGHTS_STORE_TABLE_SQL)或(或) redisStore(client)。 。 。 .

添加 completion: { url, secret } (连接是) completionsUrl 财务报告和审定财务报表 completionSecret和每个 cookie Munch作为签名的完成器被推到最后状态的请求中,然后关闭 秒数。 关闭 a pending 或者说 processing 稍后请求 await handlers.complete(requestId, outcome)。 。 。 .

版本与发布记录

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

版本发布日期距今
0.21.1latest2026/11/064 天前
0.21.02026/11/064 天前
0.20.12026/11/065 天前
0.20.02026/11/065 天前
0.19.02026/11/055 天前
0.18.02026/11/056 天前

关于 @cookiemunch/sdk 的常见问题

@cookiemunch/sdk 是什么?

Cookie Munch 开发者 API (/v1) 的 REST 客户端

如何安装 @cookiemunch/sdk?

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

@cookiemunch/sdk 支持 TypeScript 吗?

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

@cookiemunch/sdk 支持 ESM 还是 CommonJS?

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

@cookiemunch/sdk 可以商用吗?

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

@cookiemunch/sdk 的下载量有多少?

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

@cookiemunch/sdk 还在维护吗?

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