Skip to documentation content

Browser API

Control the consent banner from page JavaScript with the window.CookieBeam runtime API.

The CookieBeam loader installs a JavaScript API on the page as window.CookieBeam. The loader itself calls run() with your published configuration, so the methods below are ready to use as soon as the banner script has loaded. Callers that run too early receive an error telling them the runtime has not been initialized.

<script>
  // Show the preferences modal when a "Cookie settings" link is clicked.
  document.getElementById('cookie-settings').addEventListener('click', function () {
    window.CookieBeam.showPreferences();
  });
</script>
MethodDescription
show(createModal?)Show the consent banner. Returns a promise
hide()Hide the consent banner
showPreferences()Open the preferences modal. Returns a promise
hidePreferences()Close the preferences modal
MethodReturnsDescription
validConsent()booleanWhether the visitor has a valid, current consent record
acceptedCategory(category)booleanWhether a category (for example analytics) is accepted
acceptedService(service, category)booleanWhether a specific service within a category is accepted
validCookie(cookieName)booleanWhether the named browser cookie exists and has a non-empty value. This is not a consent check — it consults no category, service, or consent state. Use acceptedCategory to decide whether a cookie's purpose is allowed
getUserPreferences()objectThe full preference snapshot: acceptType, acceptedCategories, rejectedCategories, acceptedServices, rejectedServices
getCookie(field?, cookieName?)unknownRead the consent cookie, or one field of it
getConfig(field?)unknownRead the active runtime configuration, or one field of it
MethodDescription
acceptCategory(categories?, excludedCategories?)Accept one category, an array of categories, 'all', or 'necessary', optionally excluding specific categories
acceptService(service, category)Accept one service, an array of services, or 'all' services within a category
reset(deleteCookie?)Clear the consent state, optionally deleting the consent cookie as well

Gate optional scripts on acceptedCategory before injecting them, or use the script-gating attributes described in the install docs so the runtime blocks them automatically.

Utilities

MethodDescription
setLanguage(code, forceUpdate?)Switch the banner language. Returns a promise resolving to boolean
loadScript(src, attrs?)Load an external script once, resolving to true when it loads. Use it to activate scripts after consent
setCookieData({ value, mode? })Store custom data on the consent record; mode is 'update' or 'overwrite'
eraseCookies(cookies, path?, domain?)Delete cookies by name or pattern, optionally scoped by path and domain
run(config?)Initialize the runtime. The loader already calls this; calling it again with a new config destroys and recreates the runtime
destroy()Tear the runtime down and remove its listeners

Support diagnostics

window.CookieBeamConsent.debugInfo() returns a diagnostics snapshot of the runtime. It is always attached, even when initialization fails. CookieBeam support may ask you to paste its output when troubleshooting an installation.