# Azion `Cookies` library

import DocButton from '~/components/webkit/DocButton.vue';

The Azion **Cookies** library provides utility functions to get and set cookies in an HTTP request and response. This library is useful for handling cookies in web applications, ensuring ease of use and consistency.

<DocButton href="/en/documentation/products/azion-lib/overview/" label="Go to Azion Libraries Overview" kind="secondary" size="medium" />

---

## Usage

### `getCookie`

Retrieves the value of a specific cookie from the HTTP request.

**Parameters**:

| Parameter         | Type                | Description                                                                 |
|-------------------|---------------------|-----------------------------------------------------------------------------|
| `req`             | `Request`           | The HTTP request object.                                                    |
| `key`             | `string` (optional) | The name of the cookie to retrieve. If not provided, it returns all cookies as an object. |
| `prefixOptions`   | `CookiePrefix` (optional) | The prefix for the cookie (`'host'` or `'secure'`).                         |

**Returns**:

| Type                                | Description                                                                 |
|-------------------------------------|-----------------------------------------------------------------------------|
| `string \| undefined \| Record<string, string>` | The cookie value, or an object of all cookies if no key is provided.          |

**Example**:

```typescript
import { setCookie } from 'azion/cookies';
import type { CookieOptions } from 'azion/cookies';

const options: CookieOptions = { maxAge: 3600 };
const res = setCookie(response, 'my-cookie', 'cookie-value', options);
```

### `setCookie`

Sets a cookie in the HTTP response.

**Parameters**:

| Parameter | Type | Description |
|-----------|------|-------------|
| `res` | `Response` | The HTTP response object. |
| `name` | `string` | The name of the cookie. |
| `value` | `string` | The value of the cookie. |
| `options` | `CookieOptions` (optional) | Additional options for setting the cookie. |

The `CookieOption` type used for the `options` parameter is an object with the following properties:

| Option | Type | Description |
|--------|------|-------------|
| `maxAge` | `number` | Optional. The maximum age of the cookie, in seconds. |
| `expires` | `Date` | Optional. The expiration date of the cookie. |
| `path` | `string` | Optional. The path on the server for which the cookie will be sent. |
| `domain` | `string` | Optional. The domain for which the cookie is valid. |
| `secure` | `boolean` | Optional. If true, the cookie will only be transmitted over secure HTTPS. |
| `httpOnly` | `boolean` | Optional. If true, the cookie is inaccessible to JavaScript's Document.cookie API. |
| `sameSite` | `'Strict' \| 'Lax' \| 'None'` | Optional. Controls how the cookie is sent with cross-site requests. |

**Example**:

```typescript
import { setCookie } from 'azion/cookies';
import type { CookieOptions } from 'azion/cookies';

const options: CookieOptions = { maxAge: 3600 };
const res = setCookie(response, 'my-cookie', 'cookie-value', options);
```