Integrating Vend with CRM 2015 using Webhooks

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 thumb Integrating Vend with CRM 2015 using Webhooks

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

image thumb 2 Integrating Vend with CRM 2015 using Webhooks

  • Select the Type and enter the URL you want Vend to post data to

image thumb 3 Integrating Vend with CRM 2015 using Webhooks

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 thumb Integrating Vend with CRM 2015 using Webhooks

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

clip image002%5B6%5D thumb Integrating Vend with CRM 2015 using Webhooks

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%5B8%5D thumb Integrating Vend with CRM 2015 using Webhooks

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.

This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

Magnetism Solutions Dynamics CRM Blog