PrivacyKit

PrivacyKit API Reference

This page documents the public JavaScript API used to read consent state, react to consent changes, and control the dialog flow from application code.


Usage

All public access goes through the global window.PrivacyKit object, regardless of how the script loads.

const api = window.PrivacyKit;
 
if (api?.hasConsent('analytics')) {
  // analytics consent granted
}
 
const unsubscribe = api?.onConsentChanged(consent => {
  console.log('Consent changed', consent);
});

Public methods

openConsentDialog

Opens the PrivacyKit dialog programmatically.

openConsentDialog(): void

onConsentDialogClosed

Subscribes to dialog close events.

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

Returns an unsubscribe function.

getSubscriptionStatus

Returns the most recent subscription status cached by PrivacyKit.

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

onReady

Subscribes to the privacykit:ready lifecycle event when the API becomes available.

onReady(callback: () => void): () => void
window.PrivacyKit?.onReady(() => {
  // Safe to call PrivacyKit API methods here
  const consent = window.PrivacyKit.readConsent();
  // ...
});

Returns an unsubscribe function.


Window namespace

PrivacyKit exposes the full API on window.PrivacyKit. subscriptionStatus is optional and may be null when unavailable.

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


Next.js note

When PrivacyKit is loaded via script tags, call API methods from client components after the script finishes loading.

'use client';
 
import { useEffect } from 'react';
 
export default function Example() {
  useEffect(() => {
    const stop = window.PrivacyKit?.onConsentChanged(consent => {
      console.log(consent);
    });
 
    return () => {
      stop?.();
    };
  }, []);
 
  return null;
}