CookieStore: set() method

Baseline 2025 *
Newly available

Since June 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

* Some parts of this feature may have varying levels of support.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Service Workers.

The set() method of the CookieStore interface sets a cookie with the given name and value or options object.

Syntax

js
set(name, value)
set(options)

Parameters

This method requires one of the following:

name Optional

A string with the name of the cookie.

value Optional

A string with the value of the cookie.

Or

options Optional

An object containing:

domain Optional

A string containing the domain of the cookie. Defaults to null.

expires Optional

A timestamp, given as Unix time in milliseconds, containing the expiration date of the cookie. Defaults to null.

maxAge Optional

A number representing the number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both expires and maxAge are set, the set() call fails with a TypeError. Defaults to null.

name

A string with the name of a cookie.

partitioned Optional

A boolean value that defaults to false. If set to true, the set cookie will be a partitioned cookie. See Cookies Having Independent Partitioned State (CHIPS) for more information.

path Optional

A string containing the path of the cookie. Defaults to /.

sameSite Optional

One of the following SameSite values: "strict", "lax", or "none".

value

A string with the value of the cookie.

Note: While the values can be set here and will be used internally, some browsers will only return name and value options from CookieStore.get() and CookieStore.getAll().

Return value

A Promise that resolves with undefined when setting the cookie completes.

Exceptions

SecurityError DOMException

Thrown if the origin can not be serialized to a URL.

TypeError

Thrown if:

  • Both the expires and maxAge properties are set.
  • Setting the cookie with the given name and value or options fails in any other way.

Examples

This example sets a cookie by passing a name and value of "cookie1" and "cookie1-value", respectively. The other properties of the cookie are set with default values, as defined in the options parameter.

The code first waits for the cookie to be set: as this operation can fail, the operation is performed in a try...catch block and any errors are logged to the console. It then gets and logs the cookie that was just set.

js
async function cookieTest() {
  // Set cookie: passing name and value
  try {
    await cookieStore.set("cookie1", "cookie1-value");
  } catch (error) {
    console.log(`Error setting cookie1: ${error}`);
  }

  // Get the cookie and log its values
  const cookie = await cookieStore.get("cookie1");
  console.log(cookie);
}

This example sets a cookie by passing an options object with name, value, expires, and partitioned.

The code first waits for the cookie to be set: as this operation can fail, the operation is performed in a try...catch block and any errors are logged to the console. It then gets and logs the cookie that was just set.

js
async function cookieTest() {
  const day = 24 * 60 * 60 * 1000;
  const cookieName = "cookie2";
  try {
    // Set cookie: passing options
    await cookieStore.set({
      name: cookieName,
      value: `${cookieName}-value`,
      expires: Date.now() + day,
      partitioned: true,
    });
  } catch (error) {
    log(`Error setting ${cookieName}: ${error}`);
    console.log(error);
  }

  // Log the new cookie
  const cookie = await cookieStore.get(cookieName);
  console.log(cookie);
}

Specifications

Specification
Cookie Store API
# dom-cookiestore-set

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
set
Chrome – Full support
Chrome 87 (Release date: 2020-11-17)
footnote Full support
Edge – Full support
Edge 87 (Release date: 2020-11-19)
footnote Full support
Firefox – Full support
Firefox 140 (Release date: 2025-06-24)
footnote Full support
Opera – Full support
Opera 73 (Release date: 2020-12-09)
footnote Full support
Safari – Full support
Safari 18.4 (Release date: 2025-03-31)
footnote Full support
Chrome Android – Full support
Chrome Android 87 (Release date: 2020-11-17)
footnote Full support
Firefox for Android – Full support
Firefox for Android 140 (Release date: 2025-06-24)
footnote Full support
Opera Android – Full support
Opera Android 62 (Release date: 2021-02-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Samsung Internet – Full support
Samsung Internet 14 (Release date: 2021-04-17)
footnote Full support
WebView Android – Full support
WebView Android 87 (Release date: 2020-11-17)
footnote Full support
WebView on iOS – Full support
WebView on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
maxAge option
Chrome – Full support
Chrome 145 (Release date: 2026-02-10)
footnote Full support
Edge – Full support
Edge 145 (Release date: 2026-02-14)
footnote Full support
Firefox – Full support
Firefox 148 (Release date: 2026-02-24)
footnote Full support
Opera – Full support
Opera 129 (Release date: 2026-03-18)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 303549
Chrome Android – Full support
Chrome Android 145 (Release date: 2026-02-10)
footnote Full support
Firefox for Android – Full support
Firefox for Android 148 (Release date: 2026-02-24)
footnote Full support
Opera Android – Full support
Opera Android 96 (Release date: 2026-03-10)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 303549
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 145 (Release date: 2026-02-10)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 303549
partitioned option
Chrome – Full support
Chrome 114 (Release date: 2023-05-30)
footnote Full support
Edge – Full support
Edge 114 (Release date: 2023-06-02)
footnote Full support
Firefox – Full support
Firefox 140 (Release date: 2025-06-24)
footnote Full support
Opera – Full support
Opera 100 (Release date: 2023-06-29)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 114 (Release date: 2023-05-30)
footnote Full support
Firefox for Android – Full support
Firefox for Android 140 (Release date: 2025-06-24)
footnote Full support
Opera Android – Full support
Opera Android 76 (Release date: 2023-06-26)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 23 (Release date: 2023-10-18)
footnote Full support
WebView Android – Full support
WebView Android 114 (Release date: 2023-05-30)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.