PrivacyKit

CookieKit API Reference

Public JavaScript API for CookieKit utility methods and global namespace usage.

Delivery Modes

CookieKit supports two usage styles:

  1. Module import from the SDK package.
  2. Global namespace access through window.CookieKit.

Use module imports when your bundler supports package imports. Use window.CookieKit when loading CookieKit from an external script source.

Module Usage
import {
  readConsent,
  hasConsent,
  onConsentChanged,
  openConsentDialog,
  onConsentDialogClosed,
  getSubscriptionStatus,
} from 'cookiekit';
Global Namespace Usage
const api = window.CookieKit;

if (api?.hasConsent('analytics')) {
  // analytics consent granted
}

const unsubscribe = api?.onConsentChanged(consent => {
  console.log('Consent changed', consent);
});

Public Methods

readConsent
readConsent(): {
  analytics: boolean;
  marketing: boolean;
  preferences: boolean;
} | null

Returns null when no valid consent cookie exists.

hasConsent
hasConsent(expression?: string): boolean

Expression rules:

  1. Omitted expression: requires all categories.
  2. Single category: analytics, marketing, or preferences.
  3. AND expression: analytics+marketing.
  4. OR expression: analytics|marketing.
onConsentChanged
onConsentChanged(callback: (consent: {
  analytics: boolean;
  marketing: boolean;
  preferences: boolean;
} | null) => void): () => void

Returns an unsubscribe function.

openConsentDialog
openConsentDialog(): void
onConsentDialogClosed
onConsentDialogClosed(callback: () => void): () => void

Returns an unsubscribe function.

getSubscriptionStatus
getSubscriptionStatus(): {
  status: string | null;
  billingInterval: string | null;
  subscriptionEnd: string | null;
  trailingEnd: string | null;
} | null

Window Namespace Shape

window.CookieKit = {
  readConsent,
  hasConsent,
  onConsentChanged,
  openConsentDialog,
  onConsentDialogClosed,
  getSubscriptionStatus,
  subscriptionStatus,
};

subscriptionStatus is optional and may be null when unavailable.

Related Events

  1. cookiekit:consent-changed
  2. cookiekit:open
  3. cookiekit:dialog-closed

Next.js Note

'use client';

import { useEffect } from 'react';

export default function Example() {
  useEffect(() => {
    const stop = window.CookieKit?.onConsentChanged(consent => {
      console.log(consent);
    });

    return () => {
      stop?.();
    };
  }, []);

  return null;
}