# Cypress Documentation
# Source: https://raw.githubusercontent.com/cypress-io/cypress-documentation/main/docs/api/commands/setcookie.mdx
# Path: docs/api/commands/setcookie.mdx

---
title: 'setCookie | Cypress Documentation'
description: Set a browser cookie in Cypress.
sidebar_label: setCookie
---

<ProductHeading product="app" />

# setCookie

Set a browser cookie.

## Syntax

```javascript
cy.setCookie(name, value)
cy.setCookie(name, value, options)
```

### Usage

<Icon name="check-circle" color="green" /> **Correct Usage**

```javascript
cy.setCookie('auth_key', '123key') // Set the 'auth_key' cookie to '123key'
```

### Arguments

<Icon name="angle-right" /> **name _(String)_**

The name of the cookie to set.

<Icon name="angle-right" /> **value _(String)_**

The value of the cookie to set.

<Icon name="angle-right" /> **options _(Object)_**

Pass in an options object to change the default behavior of `cy.setCookie()`.

| Option     | Default                                                     | Description                                                                                                                                                                                                           |
| ---------- | ----------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `log`      | `true`                                                      | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log)                                                                                                                                   |
| `domain`   | Hostname of the current URL                                 | The domain the cookie is visible to                                                                                                                                                                                   |
| `expiry`   | 20 years into the future                                    | When the cookie expires, specified in seconds since [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time).                                                                                                            |
| `hostOnly` | `false`                                                     | Whether the cookie is a host-only cookie, (i.e. the request's host must exactly match the domain of the cookie)                                                                                                       |
| `httpOnly` | `false`                                                     | Whether the cookie is an HTTP only cookie                                                                                                                                                                             |
| `path`     | `/`                                                         | The cookie path                                                                                                                                                                                                       |
| `secure`   | `false`                                                     | Whether the cookie is a secure cookie                                                                                                                                                                                 |
| `timeout`  | [`responseTimeout`](/app/references/configuration#Timeouts) | Time to wait for `cy.setCookie()` to resolve before [timing out](#Timeouts)                                                                                                                                           |
| `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`. |

<HeaderYields />

`cy.setCookie()` yields a cookie object with the following properties:

- `domain`
- `expiry` _(if specified)_
- `hostOnly` _(if specified)_
- `httpOnly`
- `name`
- `path`
- `sameSite` _(if specified)_
- `secure`
- `value`

## Examples

### Name Value

#### Set a cookie

```javascript
cy.getCookies().should('be.empty')
cy.setCookie('session_id', '189jd09sufh33aaiidhf99d09')
cy.getCookie('session_id').should(
  'have.property',
  'value',
  '189jd09sufh33aaiidhf99d09'
)
```

## Rules

<HeaderRequirements />

- `cy.setCookie()` requires being chained off of `cy`.

<HeaderAssertions />

- `cy.setCookie()` will only run assertions you have chained once, and will not
  [retry](/app/core-concepts/retry-ability).

<HeaderTimeouts />

- `cy.setCookie()` should never time out.

:::caution

Because `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.

:::

## Command Log

**_Set a cookie on the browser for testing_**

```javascript
cy.getCookies().should('be.empty')
cy.setCookie('fakeCookie1', '123ABC')
cy.getCookie('fakeCookie1').should('have.property', 'value', '123ABC')
```

The commands above will display in the Command Log as:

<DocsImage
  src="/img/api/setcookie/set-cookie-on-browser-for-testing.png"
  alt="Command Log setcookie"
/>

When clicking on `setCookie` within the command log, the console outputs the
following:

<DocsImage
  src="/img/api/setcookie/see-cookie-properties-expiry-domain-and-others-in-test.png"
  alt="Console Log setcookie"
/>

## History

| Version                                    | Changes                                                                                                                                       |
| ------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| [12.7.0](/app/references/changelog#12-7-0) | Support `hostOnly` option for a given domain.                                                                                                 |
| [5.0.0](/app/references/changelog#5-0-0)   | Removed `experimentalGetCookiesSameSite` and made `sameSite` property always available.                                                       |
| [4.3.0](/app/references/changelog#4-3-0)   | Added `sameSite` property when the [experimentalGetCookiesSameSite](/app/references/configuration#Experiments) configuration value is `true`. |
| [0.16.0](/app/references/changelog#0-16-0) | `cy.setCookie()` command added                                                                                                                |

## See also

- [`cy.clearCookie()`](/api/commands/clearcookie)
- [`cy.clearCookies()`](/api/commands/clearcookies)
- [`cy.getCookie()`](/api/commands/getcookie)
- [`cy.getCookies()`](/api/commands/getcookies)
- [`Cypress.Cookies.debug()`](/api/cypress-api/cookies)