> ## Documentation Index
> Fetch the complete documentation index at: https://docs.requestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# rq.cookies (Cookies)

> Read the cookies of the request's host, and read, set and remove cookies in the cookie jar from Requestly scripts.

`rq.cookies` is the list of cookies that the host of the current request had when your script started. `rq.cookies.jar()` gives you the cookie jar, where you can read, set and remove cookies for a URL.

> [!NOTE]
>
> `rq.cookies` is a copy taken when the script starts. A cookie that your script sets or removes through `rq.cookies.jar()` does not appear in or disappear from `rq.cookies` while the script runs. Read it again from the jar with `jar.get()` or `jar.getAll()`. Adding to or removing from `rq.cookies` itself changes only your script's copy; the jar is not touched.

> [!NOTE]
>
> Using cookie objects in scripts requires the Requestly desktop app, version 2611.5.2 or later. [Download it here](https://requestly.com/downloads).

## Cookies of the current request

The list is empty when the request's URL is not `http` or `https`, or when you have not allowed scripts to use the cookies of its host. To allow a host, add it in the **Domains allowlist** tab of the cookie manager.

### `rq.cookies.get(name)`

**Type:** `string | undefined`

The value of the cookie with this name, decoded. Names are matched ignoring case.

```jsx theme={null}
const session = rq.cookies.get("session_id");
```

### `rq.cookies.has(name)`, `rq.cookies.count()`

`has(name)` returns whether a cookie with this name exists, ignoring case. `count()` returns how many cookies there are.

### `rq.cookies.one(name)`

**Type:** `Cookie | undefined`

The cookie with this name. See [Cookie](#cookie).

### `rq.cookies.all()`, `each(fn)`, `map(fn)`, `filter(fn)`, `find(fn)`, `reduce(fn, initial)`, `idx(index)`

Read the cookies as `Cookie` objects, in order. `all()` returns an array. `filter` and `find` take a function that receives each cookie and its index.

```jsx theme={null}
const httpOnly = rq.cookies.filter((cookie) => cookie.httpOnly);
```

### `rq.cookies.toObject()`

**Type:** `Record<string, string>`

An object of cookie names to decoded values. The names are in lower case.

### `rq.cookies.toString()`

The cookies as a `Cookie` header value: `name=value` pairs joined by `; `, with each value as stored (encoded).

## Cookie

A cookie has these members. A member is set only when the cookie was given it, so a cookie that does not say `secure` reads `undefined`.

| Member | Type | Meaning |
| - | - | - |
| `name` | `string` | The name. |
| `value` | `string` | The value, stored URI-encoded. `a b` is stored as `a%20b`. |
| `expires` | `Date` | When the cookie expires. |
| `maxAge` | `number` | The `Max-Age`, in seconds. |
| `domain`, `path` | `string` | Where the cookie applies. |
| `secure`, `httpOnly` | `boolean` | The flags of the cookie. |
| `hostOnly`, `session` | `boolean` | Whether the cookie is for one host only, and whether it is a session cookie. |
| `extensions` | `{ key, value }[]` | Other attributes, such as `Priority=HIGH`. |

`cookie.valueOf()` returns the decoded value. `cookie.toString()` returns the `Set-Cookie` line, for example `c1=a%20b; Expires=Sat, 02 May 2009 23:38:25 GMT; Path=/; Secure`. `cookie.toJSON()` returns the cookie as an object. `cookie.expiry` is `{ type: "session" }` or `{ type: "absolute", date }`, as the jar keeps it.

## The cookie jar

`rq.cookies.jar()` returns the jar. Each method takes a URL, and the host of the URL must be one you allowed scripts to use. A host you have not allowed rejects with an error. Every method returns a promise and also takes an optional callback, `(err, result)`.

### `jar.get(url, name)`

Resolves the decoded value of the cookie, or `null` when the jar has no such cookie or the cookie has expired.

```jsx theme={null}
const token = await rq.cookies.jar().get("https://api.example.com", "token");
```

### `jar.getAll(url, options?)`

Resolves a cookie list, with the same members as `rq.cookies`. Cookies that have expired are left out. HttpOnly cookies are included unless you pass `{ http: false }`. Secure cookies are included for an `https` URL, or when you pass `{ secure: true }`.

```jsx theme={null}
const cookies = await rq.cookies.jar().getAll("https://api.example.com");
console.log(cookies.count(), cookies.toObject());
```

### `jar.set(url, name, value)`, `jar.set(url, cookie)`, `jar.set(url, setCookieLine)`

Stores a cookie and resolves it as a `Cookie`. The value is stored URI-encoded. You can pass:

* a name and a value;
* a cookie object, such as `{ name, value, path, secure, httpOnly, expires, maxAge, domain, extensions }`;
* a `Set-Cookie` line, such as `"sid=abc; Path=/; HttpOnly"`.

A cookie without a `path` gets the path of the URL. A `maxAge` sets the expiry to that many seconds from now, and takes precedence over `expires`; a `maxAge` of 0 or less is a cookie that has already expired. A cookie whose `domain` the URL's host is not in is refused, and so is a cookie with no name.

A name passed with an empty value is read as a `Set-Cookie` line, not as a name. To set a cookie with an empty value, pass a line such as `"sid="`.

```jsx theme={null}
await rq.cookies.jar().set("https://api.example.com", {
  name: "session",
  value: "abc 123",
  httpOnly: true,
});
```

### `jar.unset(url, name)`

Removes the cookie with this name. It does nothing when there is none.

### `jar.clear(url)`

Removes every cookie the jar holds for the host of the URL.

## Related Documentation

* [rq.response](https://docs.requestly.com/api-client/rq-api-reference/rq-response.md)
* [rq.sendRequest](https://docs.requestly.com/api-client/rq-api-reference/rq-send-request.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.