SDK
SDK Overview
The ConsentLayer SDK gives you programmatic control over consent state in your app. It reads the cl_consent cookie set by the banner script and provides functions and React components to gate features behind consent.
Base package: @consentlayer/sdk
Core functions
Import from @consentlayer/sdk. No framework dependency — works in any JavaScript environment.
| Function | Description |
|---|---|
hasConsent(category) | Check if a specific category has consent |
getConsentState() | Get the full consent state object |
onConsentChange(callback) | Subscribe to consent changes |
showPreferences() | Re-open the consent preferences modal |
hasConsentCookie() | Check if a consent cookie exists |
getJurisdiction() | Get the detected jurisdiction (GDPR/CCPA) |
React components and hooks
Import from @consentlayer/sdk/react. Requires React 18+.
| Export | Type | Description |
|---|---|---|
ConsentLayerProvider | Component | Wrap your app to provide consent context |
ConsentGate | Component | Conditionally render children based on consent |
useConsent() | Hook | Access consent state and helpers |
useCategoryConsent(category) | Hook | Check a single category reactively |
Quick example
import { ConsentLayerProvider, ConsentGate } from '@consentlayer/sdk/react'
function App() {
return (
<ConsentLayerProvider>
<ConsentGate category="statistics">
<AnalyticsScript />
</ConsentGate>
</ConsentLayerProvider>
)
}The banner script tag must be installed on the page for the SDK to work — the SDK reads consent state from the banner's cookie, it does not render a banner itself.