Docs

Install & initialize

AdminUpdated Sep 15, 2026

Install & initialize

@cookiemunch/sdk is a small, fully-typed REST client over the /v1 developer API. It has zero required dependencies beyond a fetch implementation, works in Node, Deno, Bun, and the browser, and mirrors every route documented in the REST API section.

Install

npm install @cookiemunch/sdk
# or
pnpm add @cookiemunch/sdk
# or
yarn add @cookiemunch/sdk

Initialize

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

const client = createCookieMunch({
  apiKey: process.env.COOKIEMUNCH_API_KEY!,
  baseUrl: 'https://api.cookiemunch.net', // the /v1 prefix is appended automatically
});

const sites = await client.sites.list();

CookieMunchOptions

Field

Type

Required

Description

apiKey

string

yes

Your fck_… API key (see authentication for how to issue one).

baseUrl

string

yes

Your Cookie Munch server origin, e.g. https://api.cookiemunch.net. /v1 is appended internally — don't include it.

fetch

typeof fetch

no

Inject a custom fetch (for tests, non-standard runtimes, or request instrumentation). Defaults to the global fetch.

Note: the SDK never accepts or sends an orgId. Every request is authenticated by the API key, and the server derives the org from that key — this makes cross-org access structurally impossible from client code.

Error handling

Every non-2xx response throws CookieMunchApiError:

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

try {
  await client.sites.get('unknown-cbid');
} catch (err) {
  if (err instanceof CookieMunchApiError) {
    console.error(err.status, err.message, err.code);
    // 404 "site not found" undefined
  }
  throw err;
}

Property

Type

Description

status

number

The HTTP status code.

message

string

The server's error field, or a generic request failed with status N if the body wasn't JSON.

code

string | undefined

The server's code field, when present (e.g. banner_in_use, cbid_not_in_org).

Injecting fetch (testing / non-browser runtimes)

import { createCookieMunch } from '@cookiemunch/sdk';
import { vi } from 'vitest';

const mockFetch = vi.fn(async () => new Response(JSON.stringify({ orgId: 'org_1', plan: 'free', keyPrefix: 'fck_test' }), { status: 200 }));

const client = createCookieMunch({ apiKey: 'fck_test', baseUrl: 'http://localhost:8787', fetch: mockFetch as unknown as typeof fetch });

await client.me();

Next steps

Was this page helpful?
Install & initialize