This page documents the public JavaScript API used to read consent state, react to consent changes, and control the dialog flow from application code.
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);
});Reads the current consent cookie and returns a normalized consent object.
readConsent(): {
analytics: boolean;
marketing: boolean;
preferences: boolean;
} | nullReturns null when no valid consent cookie exists.
Evaluates consent for a single category or expression.
hasConsent(expression?: string): booleanSubscribes to consent updates and returns an unsubscribe function.
onConsentChanged(callback: (consent: {
analytics: boolean;
marketing: boolean;
preferences: boolean;
} | null) => void): () => voidReturns an unsubscribe function.
Opens the PrivacyKit dialog programmatically.
openConsentDialog(): voidSubscribes to dialog close events.
onConsentDialogClosed(callback: () => void): () => voidReturns an unsubscribe function.
Returns the most recent subscription status cached by PrivacyKit.
getSubscriptionStatus(): {
status: string | null;
billingInterval: string | null;
subscriptionEnd: string | null;
trailingEnd: string | null;
} | nullSubscribes to the privacykit:ready lifecycle event when the API becomes available.
onReady(callback: () => void): () => voidwindow.PrivacyKit?.onReady(() => {
// Safe to call PrivacyKit API methods here
const consent = window.PrivacyKit.readConsent();
// ...
});Returns an unsubscribe function.
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,
};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;
}