{
  "doc": {
    "id": "api/commands/setcookie",
    "title": "cy.setCookie()",
    "description": "Set a browser cookie in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/setcookie.md",
    "version": "29f95bf8bb06f320986f3749f5bf09a35a409eab",
    "updated_at": "2026-09-04T10:49:54.630Z",
    "headings": [
      {
        "id": "api/commands/setcookie#setcookie",
        "text": "setCookie",
        "level": 1
      },
      {
        "id": "api/commands/setcookie#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/setcookie#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/setcookie#name-value",
        "text": "Name Value",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#set-a-cookie",
        "text": "Set a cookie",
        "level": 4
      },
      {
        "id": "api/commands/setcookie#cookie-scope-host-only-vs-domain-cookies",
        "text": "Cookie scope (host-only vs. domain cookies)",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#preserving-a-cookie-across-tests",
        "text": "Preserving a cookie across tests",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/setcookie#requirements",
        "text": "Requirements",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#timeouts",
        "text": "Timeouts",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/setcookie#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/setcookie#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/setcookie#syntax",
      "doc_id": "api/commands/setcookie",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\ncy.setCookie(name, value)\ncy.setCookie(name, value, options)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.setCookie('auth_key', '123key') // Set the 'auth_key' cookie to '123key'\n```\n\n### Arguments\n\n**name _(String)_**\n\nThe name of the cookie to set.\n\n**value _(String)_**\n\nThe value of the cookie to set.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.setCookie()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `domain` | Hostname of the current URL | The domain the cookie is visible to. By default the resulting cookie is **not** host-only; it is matched for this host _and all of its subdomains_. To restrict the cookie to a single exact host, pass `hostOnly: true`. See [Cookie scope](#Cookie-scope-host-only-vs-domain-cookies). |\n| `expiry` | 20 years into the future | When the cookie expires, specified in seconds since [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time). |\n| `hostOnly` | `false` | Whether the cookie is a host-only cookie. When `true`, the cookie is scoped to one exact host (the request's host must exactly match the cookie's domain) and is not attached to requests for sibling or parent hosts. See [Cookie scope](#Cookie-scope-host-only-vs-domain-cookies). |\n| `httpOnly` | `false` | Whether the cookie is an HTTP only cookie |\n| `path` | `/` | The cookie path |\n| `secure` | `false` | Whether the cookie is a secure cookie |\n| `timeout` | [`responseTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `cy.setCookie()` to resolve before [timing out](#Timeouts) |\n| `sameSite` | `undefined` | Cookie's SameSite value. If set, should be one of `lax`, `strict`, or `no_restriction`. Pass `undefined` to use the browser's default. Note: `no_restriction` can only be used if the `secure` flag is set to `true`. |\n\n### Yields\n\n`cy.setCookie()` yields a cookie object with the following properties:\n\n*   `domain`\n*   `expiry` _(if specified)_\n*   `hostOnly` _(if specified)_\n*   `httpOnly`\n*   `name`\n*   `path`\n*   `sameSite` _(if specified)_\n*   `secure`\n*   `value`\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 439
    },
    {
      "id": "api/commands/setcookie#arguments",
      "doc_id": "api/commands/setcookie",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**name _(String)_**\n\nThe name of the cookie to set.\n\n**value _(String)_**\n\nThe value of the cookie to set.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.setCookie()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `domain` | Hostname of the current URL | The domain the cookie is visible to. By default the resulting cookie is **not** host-only; it is matched for this host _and all of its subdomains_. To restrict the cookie to a single exact host, pass `hostOnly: true`. See [Cookie scope](#Cookie-scope-host-only-vs-domain-cookies). |\n| `expiry` | 20 years into the future | When the cookie expires, specified in seconds since [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time). |\n| `hostOnly` | `false` | Whether the cookie is a host-only cookie. When `true`, the cookie is scoped to one exact host (the request's host must exactly match the cookie's domain) and is not attached to requests for sibling or parent hosts. See [Cookie scope](#Cookie-scope-host-only-vs-domain-cookies). |\n| `httpOnly` | `false` | Whether the cookie is an HTTP only cookie |\n| `path` | `/` | The cookie path |\n| `secure` | `false` | Whether the cookie is a secure cookie |\n| `timeout` | [`responseTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `cy.setCookie()` to resolve before [timing out](#Timeouts) |\n| `sameSite` | `undefined` | Cookie's SameSite value. If set, should be one of `lax`, `strict`, or `no_restriction`. Pass `undefined` to use the browser's default. Note: `no_restriction` can only be used if the `secure` flag is set to `true`. |\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 360
    },
    {
      "id": "api/commands/setcookie#yields",
      "doc_id": "api/commands/setcookie",
      "heading": "Yields",
      "heading_level": 3,
      "content_markdown": "### Yields\n\n`cy.setCookie()` yields a cookie object with the following properties:\n\n*   `domain`\n*   `expiry` _(if specified)_\n*   `hostOnly` _(if specified)_\n*   `httpOnly`\n*   `name`\n*   `path`\n*   `sameSite` _(if specified)_\n*   `secure`\n*   `value`\n",
      "section": "api",
      "anchors": [
        "yields"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 47
    },
    {
      "id": "api/commands/setcookie#examples",
      "doc_id": "api/commands/setcookie",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### Name Value\n\n#### Set a cookie\n\n```\ncy.getCookies().should('be.empty')\ncy.setCookie('session_id', '189jd09sufh33aaiidhf99d09')\ncy.getCookie('session_id').should(\n  'have.property',\n  'value',\n  '189jd09sufh33aaiidhf99d09'\n)\n```\n\n### Cookie scope (host-only vs. domain cookies)\n\nWhen you call `cy.setCookie()` without `hostOnly`, the `domain` defaults to the hostname of the current URL, but the cookie is stored as a **domain cookie**, not a host-only cookie. Internally Cypress rewrites the domain to its leading-dot form (e.g. `.app.example.com`), mirroring how real browsers store a cookie that omits the `Domain` attribute's host-only flag. As a result the cookie matches that host **and all of its subdomains**.\n\nThis matters for [`cy.request()`](/llm/markdown/api/commands/request.md#Cookies), which pulls matching cookies from the cookie jar by domain match before sending a request. Because the default cookie is a domain cookie, it can be attached to a `cy.request()` aimed at a _different_ host that domain-matches it, for example a sibling host on the same registrable domain. This is working as designed, but can be surprising when you set a convenience cookie for your app and see it appear on an unrelated request.\n\n```\ncy.visit('https://app.example.com')\n\n// Default: domain cookie, matches app.example.com AND its subdomains\ncy.setCookie('cookieconsent_status', 'dismiss')\n\n// Host-only: matches ONLY the exact host it was set on\ncy.setCookie('cookieconsent_status', 'dismiss', { hostOnly: true })\n```\n\nIf a cookie is unexpectedly attached to a `cy.request()` for another host on the same registrable domain, set `hostOnly: true` to keep it scoped to a single exact host.\n\n### Preserving a cookie across tests\n\nBy default, Cypress [clears all cookies before each test](/llm/markdown/app/core-concepts/test-isolation.md) to keep tests independent. If you have a known cookie value that every test requires — such as a cookie-consent acceptance or a feature flag — re-set it in a `beforeEach` hook so it is present at the start of every test:\n\n```\n// cypress/support/e2e.js\nbeforeEach(() => {\n  cy.setCookie('cookieConsent', 'accepted')\n})\n```\n\nFor cookies that are generated by the server after a login flow, use [`cy.session()`](/llm/markdown/api/commands/session.md) instead, which caches and restores the full browser context (cookies, localStorage, and sessionStorage) across tests.\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 436
    },
    {
      "id": "api/commands/setcookie#cookie-scope-host-only-vs-domain-cookies",
      "doc_id": "api/commands/setcookie",
      "heading": "Cookie scope (host-only vs. domain cookies)",
      "heading_level": 3,
      "content_markdown": "### Cookie scope (host-only vs. domain cookies)\n\nWhen you call `cy.setCookie()` without `hostOnly`, the `domain` defaults to the hostname of the current URL, but the cookie is stored as a **domain cookie**, not a host-only cookie. Internally Cypress rewrites the domain to its leading-dot form (e.g. `.app.example.com`), mirroring how real browsers store a cookie that omits the `Domain` attribute's host-only flag. As a result the cookie matches that host **and all of its subdomains**.\n\nThis matters for [`cy.request()`](/llm/markdown/api/commands/request.md#Cookies), which pulls matching cookies from the cookie jar by domain match before sending a request. Because the default cookie is a domain cookie, it can be attached to a `cy.request()` aimed at a _different_ host that domain-matches it, for example a sibling host on the same registrable domain. This is working as designed, but can be surprising when you set a convenience cookie for your app and see it appear on an unrelated request.\n\n```\ncy.visit('https://app.example.com')\n\n// Default: domain cookie, matches app.example.com AND its subdomains\ncy.setCookie('cookieconsent_status', 'dismiss')\n\n// Host-only: matches ONLY the exact host it was set on\ncy.setCookie('cookieconsent_status', 'dismiss', { hostOnly: true })\n```\n\nIf a cookie is unexpectedly attached to a `cy.request()` for another host on the same registrable domain, set `hostOnly: true` to keep it scoped to a single exact host.\n",
      "section": "api",
      "anchors": [
        "cookie-scope-host-only-vs-domain-cookies"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 283
    },
    {
      "id": "api/commands/setcookie#preserving-a-cookie-across-tests",
      "doc_id": "api/commands/setcookie",
      "heading": "Preserving a cookie across tests",
      "heading_level": 3,
      "content_markdown": "### Preserving a cookie across tests\n\nBy default, Cypress [clears all cookies before each test](/llm/markdown/app/core-concepts/test-isolation.md) to keep tests independent. If you have a known cookie value that every test requires — such as a cookie-consent acceptance or a feature flag — re-set it in a `beforeEach` hook so it is present at the start of every test:\n\n```\n// cypress/support/e2e.js\nbeforeEach(() => {\n  cy.setCookie('cookieConsent', 'accepted')\n})\n```\n\nFor cookies that are generated by the server after a login flow, use [`cy.session()`](/llm/markdown/api/commands/session.md) instead, which caches and restores the full browser context (cookies, localStorage, and sessionStorage) across tests.\n",
      "section": "api",
      "anchors": [
        "preserving-a-cookie-across-tests"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 128
    },
    {
      "id": "api/commands/setcookie#rules",
      "doc_id": "api/commands/setcookie",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements\n\n*   `cy.setCookie()` requires being chained off of `cy`.\n\n### Assertions\n\n*   `cy.setCookie()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).\n\n### Timeouts\n\n*   `cy.setCookie()` should never time out.\n\nBecause `cy.setCookie()` is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 88
    },
    {
      "id": "api/commands/setcookie#timeouts",
      "doc_id": "api/commands/setcookie",
      "heading": "Timeouts",
      "heading_level": 3,
      "content_markdown": "### Timeouts\n\n*   `cy.setCookie()` should never time out.\n\nBecause `cy.setCookie()` is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.\n",
      "section": "api",
      "anchors": [
        "timeouts"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 51
    },
    {
      "id": "api/commands/setcookie#command-log",
      "doc_id": "api/commands/setcookie",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Set a cookie on the browser for testing_**\n\n```\ncy.getCookies().should('be.empty')\ncy.setCookie('fakeCookie1', '123ABC')\ncy.getCookie('fakeCookie1').should('have.property', 'value', '123ABC')\n```\n\nThe commands above will display in the Command Log as:\n\nWhen clicking on `setCookie` within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 56
    },
    {
      "id": "api/commands/setcookie#history",
      "doc_id": "api/commands/setcookie",
      "heading": "History",
      "heading_level": 2,
      "content_markdown": "## History\n\n| Version | Changes |\n| --- | --- |\n| [12.7.0](/llm/markdown/app/references/changelog.md#12-7-0) | Support `hostOnly` option for a given domain. |\n| [5.0.0](/llm/markdown/app/references/changelog.md#5-0-0) | Removed `experimentalGetCookiesSameSite` and made `sameSite` property always available. |\n| [4.3.0](/llm/markdown/app/references/changelog.md#4-3-0) | Added `sameSite` property when the [experimentalGetCookiesSameSite](/llm/markdown/app/references/configuration.md#Experiments) configuration value is `true`. |\n| [0.16.0](/llm/markdown/app/references/changelog.md#0-16-0) | `cy.setCookie()` command added |\n",
      "section": "api",
      "anchors": [
        "history"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 75
    }
  ]
}