cookies.set()

Sets a cookie with the given cookie data. This method is equivalent to issuing an HTTP Set-Cookie header during a request to a given URL. The call succeeds only if the extension has permissions for the given URL specified in its manifest, and a page with that URL would have permissions to create a cookie with the given parameters.

Syntax

browser.cookies.set(
  details,               // object
  function(cookie) {...} // optional function
)

Parameters

details
object. Details about the cookie being set.
url
string. The request-URI to associate with the setting of the cookie. This value can affect the default domain and path values of the created cookie. If host permissions for this URL are not specified in the manifest file, the API call will fail.
nameOptional
string. The name of the cookie. Empty by default if omitted.
valueOptional
string. The value of the cookie. Empty by default if omitted.
domainOptional
string. The domain of the cookie. If omitted, the cookie becomes a host-only cookie.
pathOptional
string. The path of the cookie. Defaults to the path portion of the url parameter.
secureOptional
boolean. Whether the cookie should be marked as Secure. Defaults to false.
httpOnlyOptional
boolean. Whether the cookie should be marked as HttpOnly. Defaults to false.
expirationDateOptional
number. The expiration date of the cookie as the number of seconds since the UNIX epoch. If omitted, the cookie becomes a session cookie.
storeIdOptional
string. The ID of the cookie store in which to set the cookie. By default, the cookie is set in the current execution context's cookie store.
callbackOptional
function. The function is passed the following arguments:
cookieOptional
cookies.Cookie. Contains details about the cookie that's been set. If setting failed for any reason, this will be "null", and runtime.lastError will be set.

Browser compatibility

EdgeFirefoxChromeOpera
Basic support?45.0Yes33
Firefox
Basic support48.0

Examples

var cookieData = {
    domain: "example.com",
    name: "foo",
    value: "bar",
    httpOnly: false,
    url: "https://example.com/",
    expirationDate: 1833062400
};

function onSet(cookie) {
  if (chrome.runtime.LastError) {
    console.error(chrome.runtime.LastError);
  } else {
    console.log(cookie);
  }
}

chrome.cookies.set(cookieData, onSet);

Acknowledgements

This API is based on Chromium's chrome.cookies API. This documentation is derived from cookies.json in the Chromium code.

Document Tags and Contributors

 Contributors to this page: wbamberg, kmaglione
 Last updated by: wbamberg,