@cookiemunch/sdk

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

Typed REST client for the Cookie Munch Developer API (/v1)

原始使用文档
文档目录3 节
  1. Methods
  2. Verifying webhooks
  3. Answering rights requests

@cookiemunch/sdk

A small, typed REST client for the Cookie Munch Developer API (the /v1 surface). Authenticate with an API key — the organization is derived server-side from the key, so you never pass an 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 is injectable via createCookieMunch({ ..., fetch }) for tests or custom runtimes; it defaults to the global fetch.

Methods

  • me()
  • sites.list() / create(input) / get(cbid) / delete(cbid) / getConfig(cbid) / putConfig(cbid, config)
  • sites.lintConfig(cbid, config, { replace? }) — consent-button parity: is Accept all pushed harder than Reject all? Returns designWarnings, score, suggestedReject; nothing is saved. Every config read/write also carries 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?}) — pass the draft's updatedAt as baseUpdatedAt so a newer save is a 409 draft_conflict, never overwritten or published unseen; a rollback replaces the draft
  • rightsPage.listDomains(cbid) / addDomain(cbid, hostname) / verifyDomain(cbid, hostname) / removeDomain(cbid, hostname) — the rights page on your own hostname
  • rightsLinks.create(cbid, { subject, rights?, ttlSeconds? }) — a signed, single-use rights-page link for a signed-in person
  • org.mailProvider.get() / set(input) / test(to?) / remove() — your own provider for rights-request mail

These are the most used; the client covers every /v1 operation (see the typed CookieMunchClient).

export() returns the raw CSV string; all other methods return parsed JSON. Any non-2xx response throws a CookieMunchApiError { status, code?, message }. Error code values are catalogued in docs/api/error-codes.md.

Verifying webhooks

verifyWebhookSignature(...) validates an inbound webhook's X-CookieMunch-Signature (HMAC-SHA256 over `${timestamp}.${rawBody}`) and its freshness. It's the authoritative verifier — always matches the server. Fail-closed; returns a boolean.

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
});

Full recipe (Express, edge/WebCrypto, replay protection): docs/api/webhook-signing.md.

Answering rights requests

createRightsHandlers(...) from @cookiemunch/sdk/rights serves the cm-standard-v2 executor contract as Fetch API handlers built from your callbacks. Cookie Munch then finds, exports and erases people in your product.

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)
});

The default store is in memory. A scheduled erasure has no timer in your process: erase runs on Cookie Munch's next poll after executeAfter (every 10 minutes), so production needs a durable store: fileStore(dir), sqlStore({ query }) (table: RIGHTS_STORE_TABLE_SQL) or redisStore(client).

Add completion: { url, secret } (the connection's completionsUrl and completionSecret) and each request that reaches a final status is pushed to Cookie Munch as a signed completion, so it closes in seconds. Close a pending or processing request later with 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 是什么?

Typed REST client for the Cookie Munch Developer API (/v1)

如何安装 @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 和提交记录判断。