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.
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.
Note: the URL should point to your published application on the internet.
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:
To read the above message in MVC use the following code snippet:
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.
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.