webhooks and api: what differences?
Webhooks are newer concepts and are not yet as well known as APIs.
This article explains what is the difference between a webhook and an API and how to use them ?
To better understand the difference between the two concepts, let’s take an example.
Imagine … you have two assistants in your business.
The two have the same set of responsibilities between them.
The first assistant has all the data at all times.
It gives you all the data you need, when you ask for it.
The second assistant also has all the data available.
But it keeps you up to date with relevant information, without you asking.
Both are able to manage information effectively for you.
But the first gives you the required information on request.
The second brings you relevant information as it appears.
How does this translate into our subject?
- The first assistant is an example of API. It provides data on request.
- The second assistant is an example of a webhook. We let him know what data is relevant from the start, and he keeps us posted without any further action.
With the clear concepts, let’s go straight to the subject.
What are Webhooks?
Webhooks are event-based triggers that help applications communicate with each other.
All of this can be done automatically once it is configured.
They allow you to create specific POST URLs with the required data that can be passed to internal applications and databases for processing.
How to use Webhooks?
The concept of a webhook is simple once you understand that the basic process is to pass data by adding parameters to URLs.
Now how do you create these parameters and pass the URLs to the desired application for processing?
Before we delve into this, let’s understand how webhooks work.
How are Webhooks transmitted between applications?
This is made possible with the HTTP POST request. A POST request is used to transmit data between applications. Data can be sent as a JSON object or in XML format, both of which are accepted by all programming languages for data processing.
On your website, a POST request can be generated in several ways:
- HTML forms
What data can be transmitted in JSON or XML format?
Let’s quickly see what JSON and XML data look like.
Here is a small excerpt from JSON:
And here is an excerpt from XML:
This is an example of how the same data can be transmitted in these two different formats.
As long as the data is based on text, you can use these formats. If you want to transmit image data or other multimedia information, you can transmit file names or URLs to multimedia files in these formats.
How does your application process this data?
Now that the data has been defined and transmitted by the webhook, how does your application know what to do with it?
This brings us back to our example of “assistants”. For the two assistants, we initially mentioned what needs to be done with the information they receive.
Likewise, we need to tell our application what to do with data from the webhook. It is a single task and once configured it will run without any intervention.
The coding part of data usage in the webhook is out of scope for this article, let me give you an example of how the process will work in simple terms.
- The user performs an action that triggers the webhook
- The webhook collects the required data from the user action and converts it to JSON or XML format
- The data is then transmitted to the destination that we specify at the beginning when configuring the webhook for the interface
- Our application receives the data
- Since JSON and XML formats are universally understood by programming languages, they can break data down into unique elements
- Once the data is broken down by element, each element will be mapped to the existing table of values, for example, columns in a database
- Finally, the mapped data is stored in these specific columns
What are the applications of a webhook?
The data is in real time, so there are many applications for webhooks. Your application server can use the data generated by these webhooks to keep track of the number of emails delivered, opened and clicked.
The entire process of using webhooks in any application will be the same regardless of the application:
- There is an application that listens for events and creates a JSON object with the required data
- An intermediate layer that breaks down and maps data according to the dataset
- The final layer where data is stored or sent for further processing