﻿![]()

#### 

Webhooks are great to connect services. Our custom service can be notified about some **events** which happened on external service. This is de facto a standard way of integration in a cloud-base infrastructure. And **Sitecore Send provides** this mechanism as well.

#### Where can I place webhooks in Sitecore Send?

Webhooks can be added to the Sitecore Send Automation as an **Action**.

![Webhook action select in automation](https://www.brimit.com/-/media/images/blog/sitecore-send-6-webhook/webhook-in-automation.png)

Then the URL is needed to be added:
![add Webhook URL](https://www.brimit.com/-/media/images/blog/sitecore-send-6-webhook/webhook-url.png)

As mentioned in the description, the endpoint should accept POST HTTP request with webhook payload.

#### How can I implement a custom webhook?

In this example a simple 'log' webhook will be implemented via Node.js [express](https://www.npmjs.com/package/express) module.

##### Webhook implementation

The endpoint logic is very simple:

```
import express from 'express';

const app = express();
app.use(express.json());

const port = 3002;

app.post('/log', (req, res) => {
    console.log(req.body, JSON.stringify(req.body));
    return res.send({success: true});
});

app.listen(port, () =>
    console.log(`server is listening on ${port}.`),
);
```

The key is `app.post('/log', ...)` to handle POST request on `/log` endpoint. Inside the callback we just log to console the body of request.

The full code sources can be found [here](https://github.com/izharikov/sitecore-send-webhook).

##### Webhook publication

To use the webhook in Sitecore Send we need to publish it. 

The following publishing options are available:

- publish it to some cloud-based platform: Azure, AWS, Google Cloud Platform, etc.
- publish local server using e.g. [**Ngrok**](https://ngrok.com)

Let's use **Ngrok** for simplicity:

###### Start the server

```
cd [webhook folder]
npm run start
```

###### Run Ngrok

```
cd [ngrok location]
.\ngrok http 3002
```

Then the URL needs to be copied:
![published url](https://www.brimit.com/-/media/images/blog/sitecore-send-6-webhook/ngrok-public-url.png)

So the final URL for the webhook is the following:

`https://3dc4-31-11-239-239.ngrok-free.app/log`

##### Configure Webhook in Sitecore Send

Then 'Post a webhook' action needs to be added to the automation:
![configure the webhook](https://www.brimit.com/-/media/images/blog/sitecore-send-6-webhook/sitecore-send-action-webhook.png)

*Explanation*. The Automation 'Reactivation' is configured to be triggered when the tag 'Reactivation' is added to **existing** member of specified list. Then the email should be sent. So before sending the email the 'Log' webhook is added.

##### Results

The results of webhook work can be observed in the terminal:
![webhook results log](https://www.brimit.com/-/media/images/blog/sitecore-send-6-webhook/webhook-results.png)

The payload received from Sitecore Send:

```
{
   "UserId":"f4deea67-ec17-4d30-854f-59cced186e59",
   "StepId":"5fd8ee40-20f6-11ee-9d8a-9bf853ab0b87",
   "AutomationId":"d3091761-8e4b-4d1a-8aa5-7c26bb6f5494",
   "Event":{
      "Id":"f288653d-8290-478f-8a94-665bc7a0bc19",
      "Ab":null,
      "UserId":"f4deea67-ec17-4d30-854f-59cced186e59",
      "SessionId":null,
      "EventName":"PROFILE_CHANGED",
      "Timestamp":"2023-07-12T21:06:53.7094041Z",
      "PingContext":null,
      "LinkContext":null,
      "BounceContext":null,
      "DeviceContext":null,
      "WebsiteContext":null,
      "ContactContext":{
         "Id":"f469d169-c38f-4717-b616-d7ebfd24194c",
         "Name":"John Oliver",
         "Mobile":null,
         "MailingListId":"426efbad-a306-4cf2-9071-1a847285785e",
         "MailingListMemberId":"f469d169-c38f-4717-b616-d7ebfd24194c",
         "Tags":null,
         "CustomFields":{

         },
         "FieldsDefinition":{
            "TestFieldName":{
               "Id":"c7f4bfdf-ce98-41f5-87b3-3c5cdcb5e08e",
               "Name":"TestFieldName",
               "Type":0,
               "DateFmt":null,
               "FallBackValue":"",
               "IsRequired":false
            }
         },
         "EmailAddress":"gogoh793581234@lukaat.com",
         "ContactToken":"gogoh793581234@lukaat.com",
         "ContactType":0
      },
      "LocationContext":null,
      "PurchaseContext":null,
      "CampaignContext":null,
      "AddToCartContext":null,
      "ComplaintContext":null,
      "ExitIntentContext":null,
      "ProductViewContext":null,
      "LeadGenerationContext":null,
      "ContactChangedContexts":[
         {
            "FieldName":"[membertags]",
            "NewFieldValue":[
               "reactivation"
            ],
            "OldFieldValue":[

            ]
         }
      ],
      "Properties":{
         "OptedInSource":"2",
         "OptedInFrom":"10.201.0.252",
         "OptedInOn":"2023-07-12T21:06:52.133Z",
         "UpdatedOn":"2023-07-12T21:06:55.150Z",
         "Tags":"[\"reactivation\"]"
      }
   }
}
```

#### Data scheme

Payload JSON schema can be found [here](https://gist.github.com/izharikov/1bccfd8979023204aab9e4d005e1a622).

#### Use cases

Webhook can be used in the following cases:

- You can setup custom **notifications** in Sitecore Send with a webhook.
- Data synchronization with some other storage or integration with another service.
- Logging & debugging

#### Links

[Sitecore Send Webhooks Documentation](https://docs.moosend.com/users/moosend/en/create-an-automation-to-post-a-webhook.html)

#### Sitecore Send Series

- [Abandoned Cart with Sitecore Send](https://www.brimit.com/blog/sitecore-send-1-abandoned-cart)
- [Sitecore Send Tracking HTTP API](https://www.brimit.com/blog/sitecore-send-2-http-api)
- [Sitecore Send REST API](https://www.brimit.com/blog/sitecore-send-3-rest-api)
- [Sitecore Send Integration with Sitecore Forms](https://www.brimit.com/blog/sitecore-send-4-forms-integration)
- [Sitecore Send - Dynamic Transactional Emails (with Mustache)](https://www.brimit.com/blog/sitecore-send-5-dynamic-transactional-emails)
- [Sitecore Send - Webhooks](https://www.brimit.com/blog/sitecore-send-6-webhooks)

###### Author

[!\[Igor Zharikov - Senior Sitecore Developer, Sitecore MVP\](https://www.brimit.com/-/jssmedia/feature/blogs/authors/igor-zharikov-2.jpg?h=700&amp;iar=0&amp;w=700&amp;hash=292A84E8CB3E5CE57CA743385CFB1464)
Igor Zharikov
Sitecore MVP / Senior Sitecore Developer](https://www.brimit.com/blog/author?authors=Igor%20Zharikov)

#### More on Sitecore

[!\[How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2024/vercel_cover-image.png)
#Guides#How-toDXPE-commerce
##### How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects
Optimize and accelerate the development and deployment of complex multisite Sitecore projects.
Alexei Vershalovich on July 17, 2024](https://www.brimit.com/blog/how-vercel-will-help-you-save-effort-when-deploying-sophisticated-sitecore-projects)

[!\[Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2023/sitecore-mentoring---cover-image.png)
#How-toDXP
##### Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story
How to participate in the Sitecore Mentor program and help younger colleagues jump-start a career in Sitecore development.
Sergey Baranov on October 2, 2023](https://www.brimit.com/blog/training-up-tomorrows-sitecore-mvps)

[!\[Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2022/headless/adobestock_456986731.jpg)
#How-toDXPE-commerce
##### Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)
Discover how a headless CMS can benefit organizations that use Sitecore.
Daniil Raschupkin, Palina Trokhautsava on September 15, 2022](https://www.brimit.com/blog/going-headless-part-2-when-a-headless-cms-is-your-best-bet-if-you-have-sitecore)

![](https://bat.bing.net/action/0?ti=187017043&amp;tm=gtm002&amp;Ver=2&amp;mid=732bb349-5874-46e8-991a-cbf1caf8718a&amp;bo=2&amp;gtm_tag_source=1&amp;pi=0&amp;lg=en-US&amp;sw=800&amp;sh=600&amp;sc=24&amp;nwd=1&amp;tl=Sitecore%20Send%20-%20Webhooks&amp;kw=sitecore%20send,moosend,webhooks,ngrok,express,nodejs&amp;p=https%3A%2F%2Fwww.brimit.com%2Fblog%2Fsitecore-send-6-webhooks&amp;r=&amp;lt=295&amp;evt=pageLoad&amp;sv=2&amp;asc=D&amp;cdb=AQAY&amp;rn=992300)