Skip to main content
Sitecore Documentation
  • Learn
  • Downloads
  • Changelog
  • Roadmap
CDPCloud Portal
Sitecore CDP Developer Documentation
  • Developing with Sitecore CDP
            • Engage.init(settings)
            • Engage.initServer(settings)
            • Engage.initServer.handleCookie(req, res)
            • Engage.getBrowserId()
            • Engage.getGuestId()
            • Engage.updatePointOfSale(pointOfSale)
            • Engage.pageView(eventData[, extensionData])
            • Engage.identity(eventData[, extensionData])
            • Engage.event(type, eventData[, extensionData])
            • Engage.addToEventQueue(type, eventData[, extensionData])
            • Engage.processEventQueue()
            • Engage.clearEventQueue()
            • EngageServer.pageView(eventData, req[, extensionData])
            • EngageServer.identity(eventData, req[, extensionData])
            • EngageServer.event(type, eventData, req[, extensionData])
            • window.Engage.triggerExperiences()
        • Troubleshooting
        • Release notes
  1. Functions
  1. Stream API
  2. Functions
  3. Engage.initServer.handleCookie(req, res)

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() 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.

To set cookies from the server, you must set the forceServerCookieMode attribute to true in the settings object. After you call the initServer.handleCookie() function, the cookie is set in the response header.

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() {
  // ...
};


If you have suggestions for improving this article, let us know!

© Copyright 2026, Sitecore A/S or a Sitecore affiliated company.
All rights reserved.

Privacy policySitecore Trust CenterTerms of use