---
title: Registering custom cookies
related:
  - https://docs.kentico.com/k12sp/configuring-kentico/data-protection/adding-cookie-law-consent-to-web-pages.md
  - https://docs.kentico.com/k12sp/configuring-kentico/data-protection/working-with-cookies.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

If your website uses any custom or third-party cookies, we recommend that you register them with an appropriate [cookie level](https://docs.kentico.com/k12sp/configuring-kentico/data-protection/working-with-cookies.md). Unregistered cookies are processed with the _Visitor_ level by default.

When a visitor adjusts their allowed cookie level (for example by interacting with a [cookie consent web part](https://docs.kentico.com/k12sp/configuring-kentico/data-protection/adding-cookie-law-consent-to-web-pages.md)), the system automatically clears all cookies that have a higher level from the visitor's browser. You may also encounter problems with custom or third-party cookies not being stored if your site's **System -> Default cookie level** setting has a lower value than _Visitor_, for example when managing [tracking consent](https://docs.kentico.com/k12sp/configuring-kentico/data-protection/gdpr-compliance/working-with-consents.md).

To register custom cookies, you need to add custom code to your project. Call the **CookieHelper.RegisterCookie** method (available in the **CMS.Helpers** namespace of the Kentico API), with the following parameters:

- **string name** – the name of the cookie that you are registering.
- **int level** – an integer value representing the [cookie level](https://docs.kentico.com/k12sp/configuring-kentico/data-protection/working-with-cookies.md) required to use the cookie. You can access the default level values in the _CookieLevel_ enumeration.

To ensure that your cookies are recognized correctly, call the _RegisterCookie_ method at the beginning of the application's life cycle – either during the initialization of a [custom module class](https://docs.kentico.com/k12sp/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md), or within suitable startup code if you are [using the Kentico API in an external application](https://docs.kentico.com/k12sp/integrating-3rd-party-systems/using-the-kentico-api-externally.md).

## Example

The following example demonstrates how to register a custom cookie named _CustomCookie_ with the _Essential_ cookie level:

1. Open the Kentico web project in Visual Studio (using the **WebSite.sln** or **WebApp.sln** file).
2. Create a [custom module class](https://docs.kentico.com/k12sp/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md).
   - We recommend adding the class into a custom _Class library_ project within the Kentico solution.
3. Override the module's **OnInit** method and call the **CookieHelper.RegisterCookie** method.

   ```csharp

   using CMS;

   using CMS.DataEngine;
   using CMS.Helpers;

   // Registers the custom module into the system
   [assembly: RegisterModule(typeof(CustomInitializationModule))]

   public class CustomInitializationModule : Module
   {
       // Module class constructor, the system registers the module under the name "CustomInit"
       public CustomInitializationModule()
           : base("CustomInit")
       {
       }

       // Contains initialization code that is executed when the application starts
       protected override void OnInit()
       {
           base.OnInit();

           // Registers the "CustomCookie" with the 'Essential' cookie level
           // Ensures that the cookie is preserved for visitors who change their allowed cookie level below 'Visitor'
           CookieHelper.RegisterCookie("CustomCookie", CookieLevel.Essential);
       }
   }

   ```

The _CustomCookie_ is now registered and can be used by visitors who allow only the _Essential_ cookie level (either by interacting with a cookie consent web part or based on your site's default cookie level).

> **Tip:** **Tip – Setting cookies**
>
> You can use the Kentico API to save custom cookies into a user's browser. Call the **CookieHelper.SetValue** method within code where the HTTP context is available (for example in the code of a [custom web part](https://docs.kentico.com/k12sp/developing-websites/developing-web-parts/creating-new-web-parts.md)).
>
> ```csharp title="Example"
>
> // Saves a custom cookie named "CustomCookie" to the current visitor's browser,
> // with "CustomValue" as its value and an expiration time of 1 year
> CookieHelper.SetValue("CustomCookie", "CustomValue", DateTime.Now.AddYears(1));
>
> ```
