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.
set(name, value) set(options)
This method requires one of the following:
name OptionalA string with the name of the cookie.
value OptionalA string with the value of the cookie.
Or
options OptionalAn object containing:
domain OptionalA string containing the domain of the cookie. Defaults to null.
expires OptionalA timestamp, given as Unix time in milliseconds, containing the expiration date of the cookie. Defaults to null.
nameA string with the name of a cookie.
partitioned OptionalA 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 OptionalA string containing the path of the cookie. Defaults to /.
sameSite OptionalOne of the following SameSite values: "strict", "lax", or "none".
valueA 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().
A Promise that resolves with undefined when setting the cookie completes.
SecurityError DOMException
Thrown if the origin can not be serialized to a URL.
TypeErrorThrown if setting the cookie with the given name and value or options fails.
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.
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.
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);
}
| Specification |
|---|
| Cookie Store API> # dom-cookiestore-set> |
| 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 |
87 | 87 | 140 | 73 | 18.4 | 87 | 140 | 62 | 18.4 | 14.0 | 87 | 18.4 |
partitioned_option |
114 | 114 | 140 | 100 | No | 114 | 140 | 76 | No | 23.0 | 114 | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/set