{
  "doc": {
    "id": "api/commands/setcookie",
    "title": "setCookie | Cypress Documentation",
    "description": "Set a browser cookie in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/setcookie.md",
    "version": "e6988a974973e9090ce70406c38cb2b9e0eac9fa",
    "updated_at": "2026-05-15T15:50:22.536Z",
    "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-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "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#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/setcookie#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/setcookie#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about 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)cy.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 |\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, (i.e. the request's host must exactly match the domain of the cookie) |\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 [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\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": 369
    },
    {
      "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 |\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, (i.e. the request's host must exactly match the domain of the cookie) |\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": 287
    },
    {
      "id": "api/commands/setcookie#yields-learn-about-subject-management",
      "doc_id": "api/commands/setcookie",
      "heading": "Yields Learn about subject management",
      "heading_level": 3,
      "content_markdown": "### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\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-learn-about-subject-management"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 52
    },
    {
      "id": "api/commands/setcookie#rules",
      "doc_id": "api/commands/setcookie",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)\n\n*   `cy.setCookie()` requires being chained off of `cy`.\n\n### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#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 [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#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": 101
    },
    {
      "id": "api/commands/setcookie#timeouts-learn-about-timeouts",
      "doc_id": "api/commands/setcookie",
      "heading": "Timeouts Learn about timeouts",
      "heading_level": 3,
      "content_markdown": "### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#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-learn-about-timeouts"
      ],
      "path": "/llm/json/chunked/api/commands/setcookie.json",
      "token_estimate": 55
    },
    {
      "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')cy.setCookie('fakeCookie1', '123ABC')cy.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": 53
    },
    {
      "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
    }
  ]
}