> ## Documentation Index
> Fetch the complete documentation index at: https://docs.katla.app/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript API

> Control consent programmatically using the KatlaConsent JavaScript API.

The widget script exposes `window.KatlaConsent` for programmatic access to consent state.

## Prerequisites

* The [consent widget](/widget) script installed on your page

## Reading consent state

```javascript theme={null}
KatlaConsent.hasConsent()              // boolean
KatlaConsent.getConsent()              // { type, categories }
KatlaConsent.getAllowedCategories()    // string[]
```

## Checking specific cookies or categories

```javascript theme={null}
KatlaConsent.isCategoryAllowed('analytics')   // boolean
KatlaConsent.isCookieAllowed('_ga')           // boolean
```

## Setting consent programmatically

```javascript theme={null}
KatlaConsent.acceptAll()
KatlaConsent.acceptCategories(['analytics', 'marketing'])
KatlaConsent.rejectAll()
KatlaConsent.optOutOfSale()  // CCPA: opt out of sale/sharing
```

## Listening for changes

```javascript theme={null}
KatlaConsent.onConsentChange = (consent) => {
  console.log('Consent updated:', consent)
}
```

## Google Consent Mode

If you use Google Analytics or Google Ads and want to signal consent state via Google Consent Mode v2, use `setupGoogleConsentMode()` from the SDK:

```javascript theme={null}
import { setupGoogleConsentMode } from '@katla.app/sdk';

const cleanup = setupGoogleConsentMode();
```

This sets default consent to `denied` for all Google parameters, then automatically updates them when the visitor makes a consent choice. See the [Google Consent Mode guide](/google-consent-mode) for full details.

## Regulation and GPC

```javascript theme={null}
KatlaConsent.getRegulation()  // 'gdpr' or 'ccpa'
KatlaConsent.isGPCEnabled()   // true if browser has GPC enabled
```

## Properties

| Property     | Type       | Description                     |
| ------------ | ---------- | ------------------------------- |
| `siteId`     | `string`   | Your site's UUID                |
| `categories` | `string[]` | All available cookie categories |

## Methods

| Method                         | Returns          | Description                                           |
| ------------------------------ | ---------------- | ----------------------------------------------------- |
| `acceptAll()`                  | `void`           | Accept all cookie categories                          |
| `rejectAll()`                  | `void`           | Reject all non-functional cookies                     |
| `acceptCategories(categories)` | `void`           | Accept specific categories                            |
| `optOutOfSale()`               | `void`           | CCPA: opt out of sale/sharing of personal information |
| `hasConsent()`                 | `boolean`        | Whether visitor has made a consent choice             |
| `getConsent()`                 | `object \| null` | Current consent state (`{ type, categories }`)        |
| `getAllowedCategories()`       | `string[]`       | Currently allowed categories                          |
| `isCategoryAllowed(category)`  | `boolean`        | Check if a specific category is allowed               |
| `isCookieAllowed(name)`        | `boolean`        | Check if a specific cookie is allowed                 |
| `isGPCEnabled()`               | `boolean`        | Whether browser GPC signal is active                  |
| `getRegulation()`              | `string`         | Active regulation (`'gdpr'` or `'ccpa'`)              |
