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

# ConsentService

**Published**: 2026-11-30
**Category**: Services

Shows the site's cookie consent banner and preferences panel, stores the
visitor's answer in `localStorage` for 365 days, logs it, and injects the
site's third-party scripts for the accepted categories. Available as
`lynkow.consent`. In a browser, `createClient()` shows the banner for you
until the visitor answers. UI methods are no-ops on the server.

Access via: `lynkow.consent`

## Methods

**12** methods

### `acceptAll`

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

Accepts every category the site defines: stores and logs the choice as
`accept_all`, injects the scripts of every category, emits
`'consent-changed'` and hides the banner. No-op on the 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
```

Removes the banner, the preferences panel and the injected script
elements, and stops their listeners. A `show()` or `showPreferences()`
still waiting for the configuration is abandoned. Both work again
afterwards, as a remounted component needs. Stored answers are kept.

Returns: `void`

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

---

### `getCategories`

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

Returns the visitor's current consent categories, including any category
the site defines beyond the four this type names.

When no consent has been stored yet, the answer is what the site's
`consentMode` says is granted before anyone answers: only the required
categories under `'opt-in'`, everything under `'opt-out'` and
`'notice-only'`. On the server, always returns the `'opt-in'` defaults,
because no stored choice is readable there.

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(options?: BaseRequestOptions): Promise<CookieConfig>
```

Fetches the site's cookie consent configuration, the one the banner
renders, on the server or in the browser. Kept in memory per locale for
the life of the client, whatever the `cache` option, and concurrent calls
share one request. A call with `fetchOptions` always fetches and stores
nothing. The banner reuses what this returns.

| Parameter | Type | Description |
| --- | --- | --- |
| `options` | `BaseRequestOptions` | `locale` selects the banner language; `fetchOptions` is merged over the client's and bypasses the in-memory copy. |


Returns: `Promise<CookieConfig>`

```typescript
const config = await lynkow.consent.getConfig({
  fetchOptions: { next: { revalidate: 3600 } },
})
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). Removes only the banner this
client mounted, and cancels a `show()` of this client still waiting for
the configuration. No-op on server.

Returns: `void`

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

---

### `logConsent`

```typescript
logConsent(preferences: CookiePreferences, action?: ConsentAction): Promise<void>
```

Records a consent choice in the site's consent log, with the visitor id
stored under `_lkw_vid` in `localStorage`, the page URL and the locale the
banner config was fetched in. The banner methods call it for you. Never
rejects: every error is ignored. Outside a browser the choice is logged
under the visitor id `server`; use `cookies.logConsent()` with your own
`visitorId` there.

| Parameter | Type | Description |
| --- | --- | --- |
| `preferences` | `CookiePreferences` | Category id to accepted flag, for example `{ necessary: true, analytics: false }` |
| `action` | `ConsentAction` | Action to record. Inferred when omitted; pass `'withdraw'` explicitly when revoking a consent. |


Returns: `Promise<void>`

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

---

### `rejectAll`

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

Rejects every optional cookie category. Categories the site marked
`required` stay true. Saves the choice to `localStorage`, logs consent to
the server, and hides the banner. No-op on server.

When this takes back a category that was granted, it is logged as
`'withdraw'` rather than `'reject_all'` and the scripts injected for that
category are removed from the page. Removing them is not an undo: see
setCategories.

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
```

Forgets the visitor's stored answer, removes the injected script elements,
emits `'consent-changed'` with the optional categories off, and shows the
banner again, even under `showOnFirstVisit: false`. Nothing is logged.
No-op on the 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
```

Updates the given categories and keeps the others as they are.
`necessary` and every category the site marks `required` stay `true`.
Stores and logs the choice as `customize`, injects the scripts of accepted
categories and emits `'consent-changed'`. Taking back a granted category
logs `withdraw` and removes its script elements, but code those scripts
already ran keeps running: reload the page to stop it. No-op on the server.

| Parameter | Type | Description |
| --- | --- | --- |
| `categories` | `Partial<ConsentCategories>` | Categories to change, for 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(options?: ConsentShowOptions): void
```

Shows the consent banner, styled from the site's configuration.
`createClient()` already calls it in a browser. Does nothing when the
site's banner is disabled. When the visitor already answered, it injects
the accepted categories' scripts instead. Before an answer, `'opt-in'`
injects nothing, while `'opt-out'` and `'notice-only'` inject every
category at once; `'notice-only'` shows a single acknowledge button.
`showOnFirstVisit: false` hides the banner unless `force` is set. Only one
banner is shown per page: nothing is added while one from this or another
client is on screen, and it is removed once the visitor answers through
any client. Never throws; a failed config fetch shows nothing. No-op on
the server.

| Parameter | Type | Description |
| --- | --- | --- |
| `options` | `ConsentShowOptions` | `force` ignores `showOnFirstVisit`; `config` reuses a configuration you already fetched |


Returns: `void`

```typescript
// A "Cookie settings" link on a site that does not show the banner by itself
lynkow.consent.show({ force: true })
```

---

### `showPreferences`

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

Opens the preferences panel, where the visitor switches each optional
category on or off, starting from their current answer. Saving applies the
choice like setCategories and closes the banner. Under `'notice-only'` the panel
only lists the categories and closes without recording anything. No-op on
the server or while a panel from this or another client is open.

Returns: `void`

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