> For the complete Lynkow documentation index in agent-friendly format, see [llms.txt](/llms.txt).

# ConsentService

**Publié le** : 2026-08-20
**Catégorie** : Services

# ConsentService

High-level consent management service with built-in banner UI and preferences modal.

Accessible via `lynkow.consent`. This is a hybrid service:

- **API methods** (`getConfig`, `logConsent`) work everywhere (server + browser)
- **UI methods** (`show`, `hide`, `acceptAll`, `rejectAll`, `showPreferences`, etc.)
only work in the browser and are no-ops on the server

Consent choices are persisted in `localStorage` under the `_lkw_consent` key
(compatible with tracker.js) and expire after 365 days. When consent is granted
for a category, any third-party scripts configured for that category are
automatically injected into the page.

Emits a `'consent-changed'` event (via the SDK event emitter) and a
`'lynkow:consent:update'` CustomEvent on `document` whenever consent changes.

Access via: `lynkow.consent`

## Methods

**12** methods

### `acceptAll`

```typescript
acceptAll(): void
```

Accepts all cookie categories (necessary, analytics, marketing, preferences),
saves the choice to `localStorage`, logs consent to the server, injects all
accepted third-party scripts, and hides the banner. No-op on server.

Returns: `void`

```typescript
// Wire up a one-click "Accept All" button in your own UI
document.getElementById('accept-cookies').addEventListener('click', () => {
  lynkow.consent.acceptAll()
})
```

---

### `destroy`

```typescript
destroy(): void
```

Clean up every DOM resource this service owns: removes the banner
and preferences modal, removes injected third-party scripts, and
stops the theme observer. Call when unmounting the SDK (e.g. inside
a React `useEffect` cleanup) so the page can be navigated without
leaking listeners. No-op on server.

Returns: `void`

```typescript
useEffect(() => {
  lynkow.consent.show()
  return () => lynkow.consent.destroy()
}, [])
```

---

### `getCategories`

```typescript
getCategories(): ConsentCategories
```

Returns the user's current consent categories. If no consent has been
stored yet, returns default values (only `necessary: true`, all others `false`).
On the server, always returns defaults.

Returns: `ConsentCategories`

```typescript
// Read current preferences to conditionally load a third-party widget
const prefs = lynkow.consent.getCategories()
if (prefs.marketing) {
  loadChatWidget()
}
```

---

### `getConfig`

```typescript
getConfig(): Promise<CookieConfig>
```

Fetches the cookie consent configuration from the API. The result is
cached in memory for the lifetime of the service instance (not TTL-based).
Works on both server and browser.

Transient failures (HTTP 429, HTTP 503, and network errors) are
automatically retried with backoff, according to the client `retry`
config (enabled by default; disable with `retry: false` in
`createClient`).

Returns: `Promise<CookieConfig>`

```typescript
const config = await lynkow.consent.getConfig()
if (config.enabled) {
  console.log('Categories:', config.categories.map((c) => c.id))
}
```

---

### `hasConsented`

```typescript
hasConsented(): boolean
```

Checks whether the user has already made a consent choice (accepted,
rejected, or customized). Returns `false` if no consent is stored or
if the stored consent has expired (after 365 days).

Returns: `boolean`

```typescript
// Only show the consent banner if the user hasn't already decided
if (!lynkow.consent.hasConsented()) {
  lynkow.consent.show()
}
```

---

### `hide`

```typescript
hide(): void
```

Hide and remove the consent banner from the DOM. Does not affect
stored consent preferences (the user will not be re-prompted on the
next page load if they already chose). No-op on server or if the
banner is not currently shown.

Returns: `void`

```typescript
// Close the banner after the user clicked "Accept all" via custom UI:
lynkow.consent.hide()
```

---

### `logConsent`

```typescript
logConsent(preferences: CookiePreferences, action?: "accept_all" | "reject_all" | "customize" | "withdraw"): Promise<void>
```

Logs the user's consent preferences to the server for GDPR audit trail.
Automatically generates or retrieves a persistent visitor ID from `localStorage`.
Errors are silently swallowed to avoid disrupting the user experience.
Works on both server and browser.

| Parameter | Type | Description |
| --- | --- | --- |
| `preferences` | `CookiePreferences` | Consent preferences as a record of category IDs to booleans<br>  (e.g. `{ necessary: true, analytics: true, marketing: false }`) |
| `action` | `"accept_all" \| "reject_all" \| "customize" \| "withdraw"` | Optional explicit action type. If omitted, the action is inferred<br>  from the preferences (all true = `'accept_all'`, all false = `'reject_all'`,<br>  mixed = `'customize'`). `'withdraw'` must be passed explicitly. |


Returns: `Promise<void>`

```typescript
await lynkow.consent.logConsent(
  { necessary: true, analytics: true, marketing: false },
  'customize'
)
```

---

### `rejectAll`

```typescript
rejectAll(): void
```

Rejects all optional cookie categories (analytics, marketing, preferences).
Only `necessary` remains true (cannot be disabled). Saves the choice to
`localStorage`, logs consent to the server, and hides the banner.
No third-party scripts are injected. No-op on server.

Returns: `void`

```typescript
// Reject all non-essential cookies from a custom banner
document.getElementById('reject-cookies').addEventListener('click', () => {
  lynkow.consent.rejectAll()
})
```

---

### `reset`

```typescript
reset(): void
```

Resets all consent choices by clearing `localStorage`, removing any
previously injected third-party scripts, and re-showing the consent
banner. Useful for providing a "manage cookies" link that lets users
change their preferences. No-op on server.

Returns: `void`

```typescript
// Add a "Manage cookies" link in the footer to let users re-choose
document.getElementById('manage-cookies').addEventListener('click', () => {
  lynkow.consent.reset()
})
```

---

### `setCategories`

```typescript
setCategories(categories: Partial<ConsentCategories>): void
```

Sets specific consent categories, merging with the current state.
The `necessary` category is always forced to `true` regardless of input.
Saves the choice, logs to server, and injects scripts for accepted categories.
No-op on server.

| Parameter | Type | Description |
| --- | --- | --- |
| `categories` | `Partial<ConsentCategories>` | Partial consent categories to update. Unspecified categories<br>  retain their current value. Example: `{ analytics: true, marketing: false }` |


Returns: `void`

```typescript
// Save custom preferences from a preferences form
lynkow.consent.setCategories({
  analytics: true,
  marketing: false,
  preferences: true,
})
```

---

### `show`

```typescript
show(): void
```

Shows the consent banner in the browser. If the user has already consented
(choices stored in `localStorage`), the banner is skipped and accepted
third-party scripts are injected directly instead. Respects the site's
theme (light/dark/auto) and position settings. No-op on server.

When theme is `'auto'`, a MutationObserver watches for site theme changes
and updates the banner colors in real-time.

Returns: `void`

```typescript
// Show the consent banner on page load (skips if already consented)
const lynkow = createClient({ siteId: '...' })
lynkow.consent.show()
```

---

### `showPreferences`

```typescript
showPreferences(): void
```

Open the preferences modal so the user can toggle individual consent
categories (analytics, marketing, preferences). The `necessary`
category is always checked and disabled. Pre-populates checkboxes
with the user's current consent state. No-op on server or if the
modal is already mounted.

Returns: `void`

```typescript
<button onClick={() => lynkow.consent.showPreferences()}>
  Cookie settings
</button>
```