In this blog post we are going to dive into how to make your Marketcloud app communicate with any other service or app.

First things first, what is a Webhook? A webhook is a way for your application to provide other applications with real time information about events. Webhooks notify other systems about events as they happen, delivering the context data.

To oversimplify, webhooks are HTTP callbacks inside your app.



A very basic example of webhook we have seen implemented with Marketcloud is to notify a slack channel whenever a new order is created, this is very helpful for eCommerce companies that are using slack as a communication system, since all the team gets notified in real time of the new order without the need to check emails.

Marketcloud offers you the possibility to notify other systems whenever some events happen in your application, in our official documentation you can find the list of all possible webhook events.

To test how webhooks deliver data to your webhook endpoint, you can use services like requestb.in, this service will give you a unique URL to which you can make HTTP request, then you can see your request just refreshing the page. Very useful tool!

Once you have generated your webhook testing url with requestb.in it's time to create webhooks on your marketcloud app.

To create webhooks for your marketcloud app, go to your dashboard and open the developer console of your app.

Then click on the "Webhooks" tab and click on "add". 

 


At this point we have to select an event for the webhook and the URL that will be invoked by the webhook.

  • As event, select products.update, something we can easily test from the dashboard
  • ​As url, enter the url given by requestb.in

Click on save to save the webhook.


Now go to your backoffice (you can use the blue "Dashboard" shortcut on the upper right of the screen) and edit one of your products.
When you click on save, the webhook will notify requestb.in of the update you just made and you will be able to see your webhook data.


You will see the webhook data as raw JSON body, to better inspect the given JSON, you can copy and paste it in your editor or use a service like JSONFormatter .

Requestbin is very useful to test and debug webhooks, but you need your own server side code to implement your business logic. You can easily build a webapp able to receive your webhooks, here an example with nodejs and expressjs:


app.post('/webhook',function(request,response,next){

  /*
    If you are not parsing JSON in a middleware,
    you have to do it here
  */
  var event = JSON.parse(request.body);

  res.send(200);
});

Or using PHP:


$inputJSON = file_get_contents('php://input');

/*
  Your event object
*/
$event = json_decode($inputJSON); //convert JSON into array

We have seen how easy it is to create a webhook and extend your store's functionalities to any other service, here's a list of webhooks we have seen implemented in stores built with Marketcloud:

  • Notify new orders to a slack channel
  • Send multiple emails with sendgrid to a store's subscribers when a new product is created
  • Take advantage of services like Zapier and IFTT to create any possible connection