Integrating Vend with CRM 2015 using Webhooks

Ahmed Anwar, 08 October 2015

Concept of Webhooks

Vend has its own REST APIs that can be called using .NET or any other platform. For example retrieving all customers by calling the /api/customers end point. However due to certain limitations, the Vend team introduced a new concept of accessing data via Webhooks.

Webhooks are not like a typical API method where you invoke an endpoint to retrieve data. Webhooks monitor an event to occur, and when it happens it calls a URL using an HTTP POST containing the updated record data. For example, if there is a Webhook registered to Customers and a customer is created or updated, the Webhook calls a pre-defined URL and sends a JSON object in an HTTP POST request. Webhooks can only post data to URLs that exist on the internet, meaning that you can’t define a URL of http://localhost/.

Because Webhooks post data in near real time, they are more useful than running a synchronization tool on a defined schedule to poll for data changes.

image

To build an integration between Vend and CRM using Webhooks, you will need to expose a web application on the internet for Vend to communicate with.

Adding a Webhook

  1. Sign in to your Vend account and append /setup/api to the end of the URL in the browser. For example: https://<your subdomain name>.vendhq.com/setup/api
  2. Click on Add Webhook
  3. image
  4. Select the Type and enter the URL you want Vend to post data to

image

Note: the URL should point to your published application on the internet.

Reading Webhook Payload

When a new record is created or updated, the Webhook is triggered and it posts three keys in the post body. One of the keys named payload contains encoded JSON with attributes of the record.

This is an example of the POST body message:

clip_image002


To read the above message in MVC use the following code snippet:

clip_image002[6]

In the code above, I used Web API MVC project and used the [FromBody] attribute to access the POST body message coming from the Webhook. After reading the payload that contains all information about the created/updated record, I deserialized it using Newton namespace and from here I have an in-memory object and I can do whatever I want.

clip_image002[8]

Notice that I have used the UpsertRequest from the Microsoft Dynamics CRM SDK. This is a good choice because Webhooks do not tell you whether the record has been created or updated. Hence, you need to make sure to create an Alternate Key in CRM.