---
title: "Consent cookie schema"
description: "Field-by-field reference for the CookieBeam consent cookie, covering both the V1 baseline and V2 extended schema."
url: "https://cookiebeam.com/docs/api-reference/consent-cookie"
lang: "en"
last_updated: "2026-11-25"
---

# Consent cookie schema

Field-by-field reference for the CookieBeam consent cookie, covering both the V1 baseline and V2 extended schema.

CookieBeam persists each visitor's consent state in a first-party cookie (default name: `cb_cookie`). The cookie value is URL-encoded JSON. Two schema versions exist: V1 (the original) and V2 (the current default). Every new record is V2 with `schemaVersion: 2`. Records without a `schemaVersion` field are V1 and are auto-upgraded to V2 when parsed.

You normally don't need to read the cookie directly. Use the [Browser API](https://cookiebeam.com/docs/api-reference/browser-api) instead. This page is the field-level reference for when you're debugging, building a server-side integration, or writing a custom reader.

For the conceptual overview of what consent records mean and how they're used, see [The consent record](https://cookiebeam.com/docs/concepts/consent-record).

## V1 fields

These fields form the baseline. They're present on every consent record, V1 or V2.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `categories` | `string[]` | Yes | Accepted category IDs. At most 50 entries, each 1–100 characters. |
| `revision` | `number` | Yes | Configuration revision the choice was made against. Range: 0–1,000,000. A mismatch with the banner's current revision triggers re-consent. |
| `data` | `unknown` | Yes | Custom data stored via [`setCookieData()`](https://cookiebeam.com/docs/api-reference/browser-api#utilities). Default: `null`. |
| `consentId` | `string` | Yes | When the banner's [consent ID setting](https://cookiebeam.com/docs/configure/consent-id) is on and the visitor's choice qualifies, a per-consent identifier (`cb1_` prefix + 32 hex characters). When the setting is off (the default), absent, or the choice does not qualify, the literal string `'anonymous'`. A changed choice creates a new identifier; only repeating the same choice keeps the existing one. A returning visitor's existing cookie may still carry a per-visitor UUID from core versions before 0.3.87, and banners pinned to an older core version continue to write one, until the banner is rebuilt on 0.3.87 or later. |
| `consentTimestamp` | `string` (ISO 8601) | Yes | **Rewritten on every save.** Anchors the expiry check. See [Timestamp semantics](#timestamp-semantics). |
| `lastConsentTimestamp` | `string` (ISO 8601) | Yes | When the most recent save happened. Falls back to `consentTimestamp` on older records that predate the field. |
| `languageCode` | `string` | Yes | Banner language when consent was given. 1–20 characters. |
| `services` | `Record<string, string[]>` | No | Map of category IDs to accepted service IDs within each category. |

## V2 fields

V2 extends V1 with these fields. All are backward-compatible: a V1 reader that ignores them still gets a working consent record.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `schemaVersion` | `2` | Yes | Must be `2`. Records without it are treated as V1. |
| `acceptType` | `'all' \| 'custom' \| 'necessary'` | Yes | How the visitor chose: accepted everything, made a custom selection, or accepted only necessary. |
| `jurisdictions` | `string[]` | No | Jurisdiction tags recorded with this consent. |
| `gpcRespected` | `boolean` | No | `true` when a Global Privacy Control signal shaped this record. Stays as a historical marker even if the visitor later changes their choice through the banner. |
| `usOptOutChoices` | [`UsOptOutChoices`](#usoptoutchoices) | No | Durable US opt-out choices. See below. |
| `isPending` | `boolean` | No | Marks a pending record. For opt-in defaults (`collectedVia: 'default_optin'`), `validConsent()` returns `false`. For deferred machine defaults (`deferMachineDefaults` on, with `isDefaultState: true` and `collectedVia` of `default_optout`, `default_notice`, or `hidden_autogrant`), `validConsent()` returns `true`: the granted categories are in effect, but the GTM template skips the snapshot. |
| `isDefaultState` | `boolean` | No | Machine-written record (opt-out default, notice default, hidden auto-grant). Does **not** invalidate consent (scripts for granted categories still run), but the banner keeps auto-showing until the visitor makes an explicit choice. |
| `collectedVia` | [`CollectionMethod`](#collectedvia-values) | No | How the record was collected. |
| `firstConsentTimestamp` | `string` (ISO 8601) | No | When the visitor's first-ever consent was recorded. Absent means `consentTimestamp` is the first save. See [Timestamp semantics](#timestamp-semantics). |
| `consentMode` | [`ConsentModeSnapshot`](#consent-mode-snapshot) | No | Consent Mode state snapshot for the GTM template. Written only on explicit choices. |
| `purposeConsent` | `object` | No | Purpose-level consent evidence. Present only on banners with the purpose consent feature enabled. |

## UsOptOutChoices

Persisted US opt-out panel choices. Each field is a boolean where `true` means the visitor has **opted out** of that right.

| Field | Meaning |
| --- | --- |
| `saleSharing` | CCPA/CPRA sale and sharing of personal information. |
| `targetedAdvertising` | State-law targeted advertising opt-out (Colorado, Virginia, Connecticut, and others). |
| `sensitiveData` | CPRA limit-use-of-sensitive-personal-information. |
| `profiling` | Profiling opt-out (Colorado, Virginia, Connecticut). |

**The polarity is inverted from the UI.** In the opt-out panel, a checked toggle means the right is *allowed*. In the cookie, `true` means it's *blocked*. The conversion happens once, in the confirm handler. A field left `undefined` means the visitor never made a deliberate choice for that right; treat it as fail-safe (keep blocking), not as consent.

## collectedVia values

| Value | Meaning |
| --- | --- |
| `banner` | The visitor clicked a button on the consent banner. |
| `preferences` | The visitor saved choices in the preferences modal. |
| `api` | Consent was set through the JavaScript API (`acceptCategory()`). |
| `default_optin` | Machine-written default in opt-in mode (always `isPending: true`). |
| `default_optout` | Machine-written default in opt-out mode. |
| `default_notice` | Machine-written default in notice mode. |
| `hidden_autogrant` | Machine-written auto-grant from a regional rule that hides the banner. Grants `defaultCategories` (or read-only categories when no defaults are configured). |

Machine-written records (`default_*` and `hidden_autogrant`) always have `isDefaultState: true`. Distinguishing them by `collectedVia` tells you which consent model produced the default.

## Consent Mode snapshot

Written on explicit choices. When Consent Mode is enabled and the consent cookie is the storage backend, the runtime also refreshes the snapshot on page load for returning visitors whose stored choice is still valid. The GTM Consent Mode template reads this snapshot at Consent Initialization, before the full CookieBeam runtime loads.

| Field | Type | Description |
| --- | --- | --- |
| `v` | `number` | Contract version of the snapshot format. Currently `1`. The GTM template skips snapshots whose `v` is present and not `1`. Absent on records written by core 0.3.62 and earlier (treated as version 1). |
| `revision` | `number` | The `config.revision` the record was written under. |
| `expiresAt` | `number` | Epoch milliseconds when the record expires. Integer because the GTM sandbox has no `Date` parser. |
| `state` | `ConsentModeV2State` | The 7 Consent Mode v2 keys, each `'granted'` or `'denied'`. |

The 7 keys: `ad_storage`, `ad_user_data`, `ad_personalization`, `analytics_storage`, `functionality_storage`, `personalization_storage`, `security_storage`.

The snapshot is written on explicit visitor choices. When Consent Mode is enabled and the banner uses cookie storage (not localStorage only), the runtime refreshes the snapshot silently on page load for returning visitors with a valid stored choice (same revision, same timestamps, no new event). The refresh is skipped when the record has no existing snapshot and the banner's consent lifetimes differ across regions, or when another tab has changed the cookie since this page loaded. It's never written on a machine-written default. The GTM template skips records that lack it. See [Consent Mode v2](https://cookiebeam.com/docs/concepts/consent-mode-v2) for what each key controls.

## Timestamp semantics

Three timestamp fields serve different purposes:

| Field | Updated when | Purpose |
| --- | --- | --- |
| `consentTimestamp` | Every consent save (not `setCookieData`) | Expiry anchor. The runtime compares it against `expiresAfterDays` (or the regional `consentExpiryDays`) to decide whether the record is stale. `setCookieData()` does not update this field. |
| `lastConsentTimestamp` | Every save (including `setCookieData`) | Calendar time of the latest write. Falls back to `consentTimestamp` on records that predate the field. |
| `firstConsentTimestamp` | First save only | When the visitor's first consent was recorded. Absent on the very first save (where `consentTimestamp` already is the first time). Carried forward on every later save. |

In the [lifecycle callbacks](https://cookiebeam.com/docs/api-reference/events-reference), these surface as:

- `cookie.firstConsentAt`: resolved from `firstConsentTimestamp ?? consentTimestamp`
- `cookie.lastConsentAt`: resolved from `lastConsentTimestamp ?? consentTimestamp`

The expiry check always uses `consentTimestamp`, not `firstConsentTimestamp`. A visitor who consented a year ago but changed their choice yesterday has a fresh expiry window from yesterday's save.

## Validity rules

The runtime considers a stored record valid when all of these hold:

1. `categories` is a non-empty array of strings.
2. `consentId` is a non-empty string.
3. `consentTimestamp` is a valid ISO 8601 timestamp.
4. The timestamp is not in the future.
5. The record hasn't expired: `consentTimestamp` is within the configured `expiresAfterDays` (or the regional `consentExpiryDays` override).
6. `revision` matches the current configuration revision.
7. `isPending` is not `true` for opt-in defaults. When `deferMachineDefaults` is configured, a pending record with `isDefaultState: true` and a granting `collectedVia` (`default_optout`, `default_notice`, `hidden_autogrant`) is valid: `validConsent()` returns `true` and the granted categories are in effect.
8. Every `readOnly` category in the current configuration is present in `categories`. A record missing a read-only category is treated as stale.

A record that fails any check is treated as stale. The banner auto-shows (when `autoShow` is on), and the visitor's current categories revert to the pre-consent state for their consent model.

## Reading the cookie

### Through the API

```js
// Full parsed record
var record = CookieBeam.getCookie();

// Individual fields
var cats = CookieBeam.getCookie('categories');
// getCookie('consentId') returns the cb1_ identifier when one
// exists and the banner's consent ID setting still allows it;
// otherwise undefined. The raw cookie field holds 'anonymous'
// when there is no identifier.
var type = CookieBeam.getCookie('acceptType');

// Validity check
var isValid = CookieBeam.validConsent();
```

See [Browser API](https://cookiebeam.com/docs/api-reference/browser-api) for the full method reference.

### Raw cookie parsing

If you need to read consent server-side or outside the runtime:

```js
// Browser
var raw = document.cookie
  .split('; ')
  .find(function (c) { return c.startsWith('cb_cookie='); });
if (raw) {
  var decoded = decodeURIComponent(raw.split('=').slice(1).join('='));
  var record = JSON.parse(decoded);
  console.log(record.categories);
}
```

```js
// Node.js (Express)
var cookieValue = req.cookies['cb_cookie'];
if (cookieValue) {
  var record = JSON.parse(decodeURIComponent(cookieValue));
}
```

The cookie name defaults to `cb_cookie` but is configurable via `cookie.name` in the [configuration](https://cookiebeam.com/docs/api-reference/config-reference#cookie). Use the same name your banner is published with.

### localStorage fallback

When `cookie.useLocalStorage` is `true`, the consent record lives in `localStorage` under the key `cb_consent_{bannerId}` instead of a browser cookie. When `storage.enabled` is `true`, the runtime writes to both the cookie and `localStorage` (cookie first, `localStorage` as a fallback that the runtime reads if the cookie is missing on a later visit). The localStorage value is the same JSON (not URL-encoded). The API methods work the same regardless of the storage backend, but server-side code can't read localStorage.

## Cookie attributes

The cookie itself (not its JSON payload) is written with these HTTP attributes, all configurable via the [`cookie`](https://cookiebeam.com/docs/api-reference/config-reference#cookie) configuration section:

| Attribute | Default | Notes |
| --- | --- | --- |
| `Max-Age` | 182 days (~6 months) | Set by `expiresAfterDays`. Regional rules can override via `consentExpiryDays`. |
| `Path` | `/` | Set by `path`. |
| `Domain` | *(current host)* | Empty by default (host-only cookie). Set to `example.com` for subdomain sharing. See [Cross-domain consent](https://cookiebeam.com/docs/configure/cross-domain-consent). |
| `Secure` | `true` | Set by `secure`. |
| `SameSite` | `Lax` | Set by `sameSite`. |

## Example record

A V2 record after a visitor accepts analytics and preferences through the preferences modal:

```json
{
  "schemaVersion": 2,
  "categories": ["necessary", "analytics", "preferences"],
  "revision": 3,
  "data": null,
  "consentId": "cb1_8a3f29b7c6d1e0f45a2b7c9d3e6f1a2b",
  "consentTimestamp": "2026-11-12T14:30:00.000Z",
  "lastConsentTimestamp": "2026-11-12T14:30:00.000Z",
  "languageCode": "en",
  "acceptType": "custom",
  "collectedVia": "preferences",
  "consentMode": {
    "v": 1,
    "revision": 3,
    "expiresAt": 1763020200000,
    "state": {
      "ad_storage": "denied",
      "ad_user_data": "denied",
      "ad_personalization": "denied",
      "analytics_storage": "granted",
      "functionality_storage": "granted",
      "personalization_storage": "granted",
      "security_storage": "granted"
    }
  }
}
```

The `consentMode.state` snapshot lets the GTM template restore these signals at Consent Initialization on the next page load, before the full runtime has initialized.

A machine-written opt-out default looks different:

```json
{
  "schemaVersion": 2,
  "categories": ["necessary", "analytics", "preferences", "marketing"],
  "revision": 3,
  "data": null,
  "consentId": "anonymous",
  "consentTimestamp": "2026-11-12T14:30:00.000Z",
  "lastConsentTimestamp": "2026-11-12T14:30:00.000Z",
  "languageCode": "en",
  "acceptType": "all",
  "isDefaultState": true,
  "collectedVia": "default_optout"
}
```

No `consentMode` snapshot (machine-written defaults don't get one), all categories granted, and `isDefaultState: true` tells the banner to keep auto-showing until the visitor makes an explicit choice.
