---
title: "Engage.initServer.handleCookie(req, res)"
slug: "engage-initserver-handlecookie"
abstract: "Describes the server-side initServer.handleCookie() function in the Sitecore Engage SDK (Sitecore CDP Stream API)."
---
# Engage.initServer.handleCookie(req, res)

The asynchronous `initServer.handleCookie()` function creates the browser ID cookie on the server side and includes the cookie in the response header. To access this function, you must first call the [`initServer()`](https://doc.sitecore.com/cdp/en/developers/api/stream-api/sitecore-engage-sdk-reference/functions/engage-initserver.html "Engage.initServer(settings)") function. After you call `initServer.handleCookie()`, you receive the cookie on the client side in the response header.

> [!tip]
> In production, we recommend that you call the `initServer.handleCookie()` function in a middleware and use try-catch blocks to handle errors.

sidebar. Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `req` | object | The HTTP request. |
| `res` | object | The HTTP response. |

Example 4. Next app

Here's an example of how to use the `initServer.handleCookie()` function in a Next app to [set cookies from the server](https://doc.sitecore.com/cdp/en/developers/api/integrating-with-sitecore-cdp/understanding-integration-details/cookies.html "Cookies").

To set cookies from the server, you must set the `forceServerCookieMode` attribute to `true` in [the settings object](https://doc.sitecore.com/cdp/en/developers/api/stream-api/sitecore-engage-sdk-reference/objects/settings-object.html "Settings object"). After you call the `initServer.handleCookie()` function, the cookie is set in the response header.

```javascript
import { initServer } from "@sitecore/engage";

const engageSettings = {
    // ...
    includeUTMParameters: true
};

const engageServer = initServer(engageSettings);

export async function getServerSideProps({ req, res }) {
  await engageServer.handleCookie(req, res);
  return {
    props: {},
  };
};

export default function Home() {
  // ...
};

```