What is a Webhook?

Posted on by Isaac Noboa | Updated:
Reading Time: 5 minutes

Webhooks are an essential building block in our modern cloud ecosystem. They are used to feed real-time data from one app into another by responding to a triggered event and are used for all kinds of notification and automation systems.

Webhook Overview

What problems do webhooks solve? For example, say you’re home waiting for a package to be delivered. To know whether the package arrived, you can get up and look out your front door or window. If the package is not here, you can check back later.

In the server world, the process of checking at regular intervals to get new information is called polling, and for a long time, it was the most common method to communicate updates between two machines. The disadvantage of polling is finding a balance between resource usage and timeliness for critical events that don’t happen continuously. In our example, you’ll end up spending a lot of your energy checking whether the package arrived with nothing to show for it most of the time.

Webhooks excel at pushing, which is a resource-efficient notification method that has the server send notifications to let you know about newly occurring events. To alleviate our delivery headache, you can ask the delivery company to ring your doorbell (in other words, send a notification), so you only need to get up once: when the package arrives. 

While webhooks are great at monitoring data in real-time, they have one subtle but critical disadvantage. Since webhooks only operate once new information is available, a service that went down and isn’t reacting to any updates looks the same as a service working well that just doesn’t have any news to send. 

To mitigate this, combining webhooks with other Application Programming Interface (API) tools based on polling is the way to go. Having a redundant setup improves reliability and lets you enjoy the advantages of both. Since webhooks are taking care of the most time-sensitive updates, polling intervals can be reduced to save on bandwidth while still keeping track of any downtime issues. 

Webhook Mechanics

Webhooks are user-created HTTP callbacks. A user will create a URL and add that URL to a service that implements webhooks. When this service wants to send information to you, it will make an HTTP POST request to the URL you’ve provided. The data in this POST request can be sent in whichever format best suits the information being transmitted. The most common format used for this purpose is JSON because of its flexibility and how widely it is supported, even though in some cases, XML or HTML form encoding is preferred.

The actual data sent can be as short as a few characters, such as the following JSON snippet.

'{"data":"New event detected"}'

What’s important is that the data is sent in the expected format and arrives exactly when needed. 

A webhook is just one of many tools in an API. Sometimes webhooks are called Reverse APIs, assuming that APIs operate typically by sending requests to a server and then waiting for a response (using the polling process we discussed earlier). While the term “reverse API” may imply information flows in the opposite direction, information still flows from the service to you. A webhook is just another tool that handles this information flow in real-time.

Webhook Examples

Let’s look at some webhook examples and what you can build with them.

GitHub

GitHub lets you set up webhooks as integrations for specific events. You can have one of your servers receive a notification every time someone pushes a change to your repository and automatically use it to build and test changes. Those suggested changes can be deployed as part of a Continuous Integration/Continuous Delivery system or CI/CD. 

Dropbox

Similar to GitHub webhooks, Dropbox lets you receive notifications any time a change is made to a file or folder, as well as the users who have made those changes. This enables you to automate many tasks on the publishing process, such as automatically converting files to PDF once changed. 

Slack

For things that might require a human eye, Slack allows you to configure webhooks that post to a specific channel whenever new information arrives. This is especially useful for alert and monitoring systems or other events that require immediate attention. 

Webhook Integration Tips

Below are a few tips that will allow you to integrate webhooks effectively.

Respond Quickly

The main advantage of webhooks is how they can transmit information in real-time, so it’s important to acknowledge you’ve received a webhook request as soon as possible. If the received data requires in-depth processing, consider using a queue or a separate thread.

Handle Errors Correctly

If there’s a problem with the transmitted data, raising an error code is vital to ensure any missed updates are sent again. Even if you’re processing the received data separately, you can parse this information beforehand to make sure it’s valid (also known as a sanity check).

Monitor the Load

Webhook notifications arrive whenever an event has occurred, so it is possible to receive more alerts than your server can handle in a short timeframe. Most services let you configure a speed limit to prevent this problem. In contrast, some others will require you to carefully optimize your system to continue to receive notifications as usual. 

Secure Your Connections

Using an SSL certificate to encrypt data as it travels is already a standard, but since the webhook URL will be listening to any incoming requests, unauthorized actors can try and send unexpected data into this webhook. To avoid that, most services include a secret authentication token with each request, which works like a password to make sure that the request is one you actually want to receive. 

Webhook Testing Tips

Most services that provide a webhook interface in their API also have extensive documentation on how to use it. Getting familiar with how to browse documentation and how to use the examples it provides is a critical skill for developing web services and applications. You can get started with the Liquid Web API, which lets you automate most of the actions you can manually take on your account control panel. Practice goes a lot further than theory, so looking directly at the way your webhooks behave is very useful for development. 

Occasionally, looking at the traffic helps get a better feel of how the data is organized. Some services like GitHub will let you view the full content and respond to any past requests. If this feature is not available on another service, you can use a third-party tool like NGROK to capture and view this data. 

You can use cURL to mimic your service’s behavior and paste data into a new webhook request for active testing. This method becomes very useful for troubleshooting edge cases that may not happen often.

In the following example, we see a very basic webhook request made using cURL to a webhook URL created in Slack, with the following options: 

  • -X to specify the type of request (POST).
  • -H to include a header, which specifies the type of content (in this case, JSON data).
  • --data to send the actual data, which is the text “Hello, World!”

We can see the server replied with ok, and if we check the Slack channel where we created this application, we can see that the notification went through correctly. 

Conclusion

Webhooks are a tool that lets you receive updates about events or notifications through regular HTTP requests without the need to spend resources on constant status updates. They are a simple but powerful building block in many different systems that automate important tasks. 

Are you ready to start building your own automated solutions for all of your business needs? Start testing with one of our small, affordable fully-managed VPSs, a dedicated server solution, or contact one of our Solutions experts for help architecting your infrastructure.

Avatar for Isaac Noboa

About the Author: Isaac Noboa

Former support technician, budding software developer, and general tinkerer. If there's one thing Isaac enjoys more than understanding how systems work, it's helping others do the same.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article