Lets say for the sake of argument that your business exposes an API. Well done! This is the first step into joining the integration playing field of today’s interconnected world. You offer valuable data and features for free, as part of a platform or you monetise the API itself. Either way, customers like it and consume it on a regular base.

Chances are high that you have regular updates on your data. Most likely customers get these updates regularly and some might even poll for it. Most developers know that polling is not that effective. A push mechanism is usual far more resource friendly. In other words, you inform your customers every time something new arrives or change occurs. One of the push mechanisms in the online world is called webhooks, so, lets dive into this topic or check out the video

Automated services

Sites like IFTTT and Zapier are online platforms build on top the trigger -> action mechanism. One service can expose some fixed triggers and the other can react on it or the other way around. In fact, most services provide both. Services can be either online tools (Google Drive, Trello, Github) or hardware (e.g. your smartphone, Philips Hue lights, Google’s router Onhub). Lets check out some trigger examples:

  • GMail – “a mail has been received” (with subject “x” or from contact “y”)
  • Dropbox “a file has been added”

and some action examples:

  • Google – “create a contact in your Google account”
  • Github –  “Create a new issue”.

Out of the box you can already do really cool stuff with it like “turn on my smarphone’s wireless connection instead of mobile data connection when I come home” or, more businesslike , “When a contact is created in Salesforce, post a message in a Slack channel”. The pattern used to connect these services is called “Webhooks”.

Webhooks

Wikipedia offers an explanation about the pattern here. I would like to give you my own explantation as well: A Webhook is a pattern which requires in / output to be predefined and a shared secret to allow communication between them. Also, it includes having a retry policy, should the communication fail (hey, it’s the web we’re talking about after all.).

Asp.net webhooks

Both “ends”, the receiver and sender side, can be arranged with Asp.net webhooks. A NuGet package exists for most receivers like Slack, Github, Mailchimp, … But there’s also, and that’s where it gets intesting, a “Generic Json” receiver. This means you can hook into actions from all senders in the wonderful world of IFTTT and Zapier!

Your Api as a receiver

In a few steps you have your api connected as a receiver.

  1. Create a Web Api Project in Visual Studio.
  2. Install the correct receiver package “Microsoft.AspNet.WebHooks.Receivers.[Receivername]”
  3. Add a configuration setting “MS_WebHookReceiverSecret_[Receivername]”
  4. In the startup.cs file, add the following line : “config.InitializeReceive[Receivername]WebHooks();”
  5. Create a class which inherits from “Webhookhandler”.
  6. Override method ExecuteAsync.

Beneath you can find a recap on how to create a close integration with Slack with a fun factor, asking your “Eightball wizard” a question, which he will answer.

An example of a Generic Json receiver setup “Card created in trello > post that card data to custom API”

Your api as a sender

The sender in asp.net webhooks is used when you want to enable third party api’s, which are listed in a subscription list to listen to specific actions (methods) of your service.

Because this is a bit overkill when you just want your api to send data directly to only one service, in this case Zapier or IFTTT, I investigated how to do this in Zapier.

That was also quite easy.

  1. Create a zap in Zapier.
  2. Choose “Webhooks” as action, Zapier will provide you a unique URL.
  3. POST to this URL via e.g. a httpclient.

Beneatch a recap on how to create a card on Trello when search is launched in your products catalog api.

All info on Asp.net webhooks can be found here: https://docs.asp.net/projects/webhooks/en/latest/