HTTP Webhook Strategy - Integration with Azure Service Bus

See the example integration with Azure Service Bus through webhooks.

Thanks to HTTP webhook strategy, you can choose the event service that enables communication between Emporix and external systems. This article serves as an example demonstrating how you can connect Azure Service Bus to receive events notifications from Emporix. Follow the steps to set up the connection on both sides. Adjust the actual implementation to your needs. Azure Service Bus is a messaging service that allows for communication between decoupled systems, so it can serve as a solution to connect Emporix with some other systems in your company through the webhooks functionality.

Preparing Azure infrastructure

The integration between Emporix and Azure Service Bus in this example will use Azure Function that will act as a consumer of webhook events coming from Emporix. The Azure Function will receive the HTTP request, validate HMAC signature and send the request body to the given queue.

Azure Service Bus

To create Azure Service Bus, go to the Azure Portal.

  1. Create a Service Bus called webhooking and a queue called webhook.

  2. Save the Connection String that is present in the Shared access policies section.

Azure function

  1. Create a Function App.

In this example, the function uses NodeJS and JavaScript.

  1. Create the function locally and then deploy it to Azure.

  1. Create a new project:

func new --template "Http Trigger" --name MyHttpTrigger2
  1. Modify the generated code.

    a. Add a new dependency to package.json:

    "@azure/service-bus": "^7.0.0"

    b. Execute npm install to apply the change.

    c. Provide the required information in the function's code so that the function sends the requests to Azure Service Bus. Let's take a look and analyze the function’s code:

    const { app } = require('@azure/functions');
    const { ServiceBusClient } = require("@azure/service-bus");
    const crypto = require('crypto');
    
    const connectionString = "FILL_IT" // i
    const queueName = "webhook"; // ii
    
    const secretKey = 'password123'; // iii
    
    app.http('MyHttpTrigger2', {
        methods: ['GET', 'POST'],
        authLevel: 'anonymous',
        handler: async (req, context) => {
    
            const serviceBusClient = new ServiceBusClient(connectionString);
            const sender = serviceBusClient.createSender(queueName);
        
            const body = JSON.stringify(req.body);
            context.log(body);
            const hmac = crypto.createHmac('sha256', secretKey);
            hmac.update(body);
        
            const computedHmac = hmac.digest('base64');
        
            
            if (computedHmac !== req.headers['emporix-event-signature']) {
                context.res = {
                    status: 401,
                    body: "HMAC validation didn't pass.",
                };
            }
        
            try {
                const messageBody = req.body;
                const message = {
                    body: messageBody,
                };
        
                await sender.sendMessages(message); // iv
                context.log("Message successfully sent");
                context.res = {
                    status: 200,
                    body: "Message sent to the queue",
                };
            } finally {
                await sender.close();
                await serviceBusClient.close();
            }
        }
    });

\

Pay attention to the marked lines and provide relevant information:

i. connectionString constant - replace the value with the connection string to your Azure Service Bus instance. Find it in the Shared access policies section in Azure Portal.

ii. queueName constant - provide the value of the queue you created. Here, it equals webhook just as the queue we created.

iii. secretKey constant - provide the secret phrase for HMAC validation. You need to specify the same value in the webhooks HTTP configuration in the later steps. Here, we defined it as password123.

Find more information on how to configure HMAC validation in HTTP Webhook Strategy - HMAC Configuration.

iv. when HMAC validation passes, the message body is sent with the request to the queue.

  1. Deploy the function to Azure by calling the command:

func azure functionapp publish <APP_NAME>

Configuring Emporix webhooks

Now, prepare Commerce Engine.

  1. Configure webhooks to send the events as HTTP POST requests. Use the following curl and provide relevant information in the placeholders:

curl -L 'https://api.emporix.io/webhook/{{tenant}}/config' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {{token}}' \
-d '{
    "code": "http",
    "active": true,
    "provider": "HTTP",
    "configuration": {
        "secretKey": "password123",
        "destinationUrl": "{{destinationUrl}}"
    }
}'

Placeholders to fill in:

  • tenant - name of your production tenant

  • token - your generated Emporix access token with relevant scopes

  • destinationUrl - link to your Azure Function

  1. Subscribe to a chosen event. In this example, we subscribe to product creation event (remember to replace the values in the placeholders):

curl -L -X PATCH 'https://api.emporix.io/webhook/{{tenant}}/event-subscriptions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {{token}}' \
-d '[
  {
    "eventType": "product.created",
    "action": "SUBSCRIBE",
    "fieldsToSubscribe": [
      "name"
    ],
    "fieldsToUnsubscribe": [
      "description"
    ]
  }
]
 '

Creating product

The integration is ready. To see it in action, let's create a product.

Create a product with the following curl request providing your relevant details in the placeholders:

curl -L 'https://api.emporix.io/product/{{tenant}}/products/' \
-H 'Content-Language: en' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {{token}}' \
-d '{
    "name": "Test product for documentation",
    "code": "unique",
    "description": "product description",
    "published": false
}'

After the product is created the event is sent. The webhook service consumes the event and sends it as HTTP request to the specified destinationUrl, which is the Azure Function. The Azure Function receives the request and sends it to Azure Service Bus Queue.

Last updated

Was this helpful?