ConsentLayer
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.

FunctionDescription
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+.

ExportTypeDescription
ConsentLayerProviderComponentWrap your app to provide consent context
ConsentGateComponentConditionally render children based on consent
useConsent()HookAccess consent state and helpers
useCategoryConsent(category)HookCheck 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.