Creating a Next.js Serverless Function for Sitecore Hook Integration

Learn how to create a serverless function for Sitecore hook integration and improve automation.

April 9, 2025

By John Flores

Search Platforms and Automating for Efficiency

When a business process involves multiple steps, complexity and time requirements naturally increase. To streamline workflows, we often turn to automation—not just to reduce manual effort but also to minimize reliance on individuals and even specific tools.

A great example we’ve encountered is a client with a search platform that requires continuous updates from their website, pulling in everything from standard pages to related content. Managing this manually can be tedious and error-prone, so how do we solve this efficiently?

Comparing Search Solutions: Web Crawling vs. API Push

Many search platforms offer different solutions depending on the level of complexity you require. We’ve applied these approaches using both Sitecore Search and Coveo. Here are some common approaches we’ve encountered and implemented:

  • Web Crawling – Setting up a web crawler is one of the simplest solutions. You define which parts of the page should be extracted as necessary data, making it a relatively straightforward process with minimal coding. Platforms like Sitecore Search and Coveo offer built-in crawling capabilities.
  • API Push – Depending on the platform, this method may involve multiple steps and APIs, but when configured correctly, it becomes a powerful tool. Most platforms provide APIs for managing indexed content. For example, Sitecore Search offers Ingestion APIs that allow you to create, update, or delete index documents directly from your source.

Both approaches have their advantages. To keep the data up to date, web crawling relies on periodic scans, which, while effective, can become resource-intensive depending on cost and frequency. On the other hand, an API push offers greater control and efficiency, as it allows updates to be sent only when necessary.

API Push for Efficiency

For our use case, the API push method is the better option since we only want to send updates when changes occur. To achieve this, we’ll create a serverless function that performs a GraphQL query and pushes the updated data to the platform’s API push source. This function is triggered whenever a user publishes a specific item in our content tree. Interested in setting up the Sitecore Webhook? Check out this blog.

Setting Up an API Push Source in Sitecore Search Platform

To get started, we first need to create an API push source. In the Sitecore Search Platform, go to the Source tab and add a new source, just as you would for any other source type. Below is a screenshot showing what the platform looks like when navigating to the Sources page, especially if you haven’t set up any sources yet.

 

User interface displaying the 'Sources' page with an 'Add Source' prompt

Next, select the appropriate CONNECTOR for your needs. In this case, choose the API push option. After filling in the required fields, you'll be directed to the next page. Finally, publish the source, and you’ll be ready to use the Ingestion APIs. Below is a screenshot of the form you'll see when adding a new source, along with the various CONNECTOR options available.

Add Source form with fields for source name, description, and connector selection dropdown showing options like API Crawler, API Push, and Feed Crawler

Creating Serverless Functions for Sitecore Webhooks in Next.js

If you are using Pages Router in your Next.js Project then you can easily create your serverless functions within the \src\pages\api directory. Our goal is to create a function that queries data and pushes it based on the specifications in your platform's documentation.

Since the Sitecore Webhook uses a POST request, ensure your serverless function is set up to handle POST if you're restricting allowed methods. Additionally, retrieve the API key with the ingestion scope from the Sitecore Search Platform under Developer Resources > API Access.

Creating the Documents

First, we need to understand the required headers: Content-Type and Authorization.

  • Content-Type — application/json
  • Authorization — <api-key> which can be found in Developer Resources > API Access

Next, let's look at the endpoint format, which you'll use by filling in the necessary values with the POST method:

https://<base-url>/ingestion/v1/domains/<domainID>/sources/<sourceID>/entities/<entity>/documents?locale=<locale>

Here are the missing values and where to find them:

  • <base-url> — This is available in Developer Resources > API Access in CEC.
  • <domainID> — You can find this ID in the domain name drop-down in CEC.
  • <sourceID> — This refers to the ID of the source where the document will be added. You'll find it in the Administration > Sources > Source Information section of CEC.
  • <entity> — This typically refers to the index document type. Although it's a bit flexible, we generally use content as the value.
  • <locale> — You can locate the correct format in Administration > General Settings > Locale in CEC.

Here's an example of the payload you would send to the API:

{
  "document": {
    "fields": {
      "description": "Add your description text here",
      "title": "Your first pushed document"
    },
    "id": "document1"
  }
}

If successful, you will receive a 200 response, and the response body will look like this:

{
    "enqueued": true
}

Updating a Document

In order to update a document you will just need to know the document id with the payload being similar to how you create a document.

https://<base-url>/ingestion/v1/domains/<domainID>/sources/<sourceID>/entities/<entity>/documents/[id]?locale=<locale>

If successful, you will get a 200 response and the body looks like:

{
   "enqueued": true,
   "incremental_update_id": "string"
}

Verifying the Sitecore Webhook Functionality with Vercel

There’s nothing too complicated here, aside from seeing the changes applied on CEC, what we want to check is whether the Sitecore Webhook is working. We can simply check the logs and see if the API is triggered right after a publish. You can also simply debug whether it is working or not from there as well.

Leveraging Serverless Functions with Sitecore Webhooks and the Ingestion API

You can start by creating a serverless function that triggers on publish, but the possibilities don’t end there. Sitecore offers additional webhooks you can use, allowing you to create serverless functions that respond to various events. Check out the Sitecore Documentation on Webhook Events for more details.

Additionally, you can explore the different ways to use the Ingestion API by referring to the official Sitecore Search documentation.

John Headshot

John Flores

Front-End Developer | Sitecore Technology MVP

John is a Senior Front-End Developer who is passionate about design and development. Outside of work, John has wide range of hobbies, from his plant collection, being a dog daddy, and a foodie. John is also a Sitecore Technology MVP!