How to block cookies using the API
Updated over a week ago

If your website sets cookies from the backend, you will need to use the JS Callback and API described here to retrieve the cookie whitelist that your visitors have consented to and selectively deliver those cookies or not.

Step 1: Retrieve the visitor’s UUID from the frontend

Before using the API, you'll need to use the below JS Callback to retrieve visitor’s unique user identity (UUID) from the frontend.

JS Callback

getUpdatedCookieWhitelistByTermly(data)

When is this called?
This callback method is triggered when your visitor gives consent to the use of cookies via the consent banner or through the cookie preference center installed on your website.

How do I use this?

You can store the whitelist (data.cookieWhitelist) in your frontend code, and use it to adjust cookie delivery logic for your website visitors.

Store the visitor’s UUID (data.UUID) in order to call the Termly RESTful API for a specific visitor.

Visit this demo to view an example of how to use the JS callback.

Whitelist format:

{ 
 cookies:
 [
 { 'name': 'cookie_name', 'provider': 'termly.io', 'type': 'http_cookie' },
 { 'name': 'cookie_name', 'provider': 'termly.io', 'type': 'http_cookie' }
 ] 
 categories: ['essential'],
 uuid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

UUID format:

{ uuid: '104697b6-5b10-4aa3-8f8e-52ac2af5ef37' }

Step 2: Get the visitor’s cookie preferences via API from the backend

After retrieving the UUID from the JS Callback, use the cookie_whitelist endpoint to get a specific visitor’s consent preferences:

GET cookie_whitelist

https://app.termly.io/api/v1/cookie_whitelist


This endpoint allows you to retrieve cookies that your visitor has consented to. This will also retrieve cookies in the Essential category.

Sample Code:

curl -H 'Accept: application/json' -H "Authorization: ${API_KEY}"
https://termly.io/api/v1/cookie_whitelist?uuid={UUID}
{ 
 cookies:
 [
 { 'name': 'cookie_name', 'provider': 'termly.io', 'type': 'http_cookie' },
 { 'name': 'cookie_name', 'provider': 'termly.io', 'type': 'http_cookie' }
 ] 
 categories: ['essential'],
 uuid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

See more at Termly's API documentation.

Did this answer your question?