A Webhook is kind of the inverse of an web API. Whereas a web API responds to an HTTP request, a webhook makes an HTTP POST to an API endpoint in response to some event. Webhooks have started appearing as part of online services in the past few years, which make a lot of automation much easier.
I’ve used webhooks in a couple of recent projects, one with Dropbox and one with Chargify (a payment processor a bit like paypal).
In the Dropbox project, I needed to process any new documents uploaded by a user. Without webhooks the process would be possible but much less easy. You’ll need to have a script that polls at regular intervals, looking in Dropbox for any new files. If you setup Dropbox (apps) to send you webhooks, you can be immediately notified of any new files. Not only does that save data (since you don’t have to poll repeatedly), it is also immediate, no need to wait until the next hour or whenever you had set a script to run, furthermore the webhook POST data (called a webhook payload) will contain the information about “what happened” (in this case the user account which has modified files), no need to write code to poll each user for new files.
The other project I did involving webhooks recently was for a client who wanted to be notified when customers signed up through the Chargify payment system, so they could automatically process new signups and send customers the (digital) product that they ordered. So I set up the client’s Chargify account to post a webhook on customer signup. This not only saved the client time, but also delivered the product immediately to the customer, without needing to wait for manual intervention.
There are plenty of other uses for webhooks, but one that I think is particularly powerful but haven’t tried yet is github. We can set github to send a webhook each time code on a certain repo is pushed. Then, we could set the receiving endpoint to update the code on our server, and restart any services. So each time we commit and push new code, it automatically deploys to a server – very convenient. The technical term for this is Continuous Integration. Web services usually send webhooks in JSON format also provide documentation about the exact format and content of the payloads – here is github’s example for a ‘push’ payload notification.
Of course all of this things require you to construct your own API endpoint that webhooks can POST to, and will process the information contained within the payload. Next time I’ll show how I did this for the Chargify project using Python and Flask.