Introducing Webhooks

Webhooks are simple callbacks that are rapidly growing in popularity with a lot of developers. Webhooks work in a very similar way to Lambda functions; they are invoked when a particular event is fired by an application on the web. This makes them highly applicable to a variety of web development use cases where, rather than having a traditional API that polls for data on a frequent basis, you use a Webhook to get data at real time.


With most APIs there's a request followed by a response, whereas in the case of Webhooks, they simply send the data whenever it's available.

The way a Webhook works is quite simple! To use a Webhook, you register a URL with the Webhook provider, for example IFTTT or Zapier. The URL is a place within your application that will accept the data and do something with it. In some cases, you can tell the provider the situations when you'd like to receive data. Whenever there's something new, the Webhook will send it to your URL.

Here's a common example of a Webhook--open up any of your GitHub pages from your browser. There, in your repository's Setting page, you will see a Webhooks section as shown in the following image. You can always add a new Webhook to your repository by selecting the Add webhook option. There, you can provide a URL that will receive an event each time you either push or commit code into your repository and so on. On the event getting triggered, GitHub sends a POST request to the URL with details of the subscribed events. You can also specify which data format you would like to receive the request in, for example, JSON or x-www-form-urlencoded. In this way, you can easily create open ended integrations with other arbitrary web services without having to spin up any new infrastructure or manage any complex code integrations as well.

This is the exact same case that we are going to explore and learn about in this chapter. We will learn how to effectively use Webhooks to hook into third-party services and sites, and trigger some Lambda action on it's behalf. Since we have already taken up the topic of GitHub, let us look at how Lambda, Webhooks and a GitHub repository can be tied together functionally with minimal efforts.