There has been a lot of talk around decoupled Drupal. In fact, a majority of the tweets I see related to Drupal these days have got something to do with decoupling. If decoupled Drupal is a phenomenon today, almost the entire credit goes to APIs. APIs enable developers to sync data between applications, making it possible to integrate them. But APIs are not the magical solution to every integration and data sync problem that you may face and, in several scenarios, using an API is time-consuming and wasteful as compared to a webhook. In this article, we will discuss the main differences between APIs and webhooks and learn how to make the best use of both.
The Difference between Webhooks and APIs (ELI5)
Letâs say your friend is about to receive a very important parcel for you.
API approach: You have your friendâs number, but they donât have yours. To find out if they have received the parcel yet, youâll have to call them. You may end up calling more than once till they have finally received the package.
Webhook approach: Your friend has your number. As soon as they receive the parcel, they can call you up and let you know.
APIs are used to make calls to an application asking for data or to give instructions to perform a task. The application, in turn, has no way of informing you if there is any task that needs to be performed or if there is new data available.
Webhooks, on the other hand, do not need instructions. They run automatically based on a pre-defined event. Whenever the event occurs, a call is triggered with the required data from the application to your server.
When to use an API
If you know that your data is changing and updating constantly, use an API. APIs provide much greater control as compared to webhooks. You can decide how much data you want and when. Increasing and decreasing the paging size at your discretion is one of the best features that an API offers. Also, if itâs a mostly one-way interaction and you want data to be available to you only when you query for it, you donât need to think twice before opting for an API-based solution.
However, as we have already seen, APIs are at a disadvantage when it comes to real-time data. Also, if your data does not get updated constantly and regularly, then there may not be new data available for every API call you make.
When to use a Webhook
Webhooks are a must if you need real-time data. As soon as there is some change in the application (a new message, new data, even an error), a webhook sends an HTTP POST notification to your server and it can perform the required task to deal with the data. Webhooks should also be preferred to API polling if your data is not updated at regular intervals. Making unnecessary API calls when you do not know if there will be new data available can be avoided if we use webhooks.
But webhooks arenât perfect either. If the system which sends updates to your webhook goes offline for some reason, you will never find out in time. You also have less control over the data as you have to accept the data that is sent to you.
|Application sends a request to receive data.||Data is sent to the application automatically based on a pre-defined trigger.|
|Application cannot know if there is any new data until a request is sent. Multiple requests need to be sent till the data is actually available.||As soon as new data is available, it will be automatically sent to the application.|
|If there is any problem with the application or server that is hosting the data, we will immediately find out as there wonât be a response or it will be corrupt.||There is no way to find out if the application or server hosting the data has gone offline.|
|Greater control over size of data.||Less control over the size of data.|
APIs and webhooks do not have to be mutually exclusive. Effective communication is a two-way process and the same is true for applications. An efficient application would make the best of both worlds and use a combined API+webhook approach. If we incorporate webhooks to complement APIs, we will have real-time data as well as control. So, the next time you hear the term âdecoupled Drupalâ, do remember to suggest using webhooks along with APIs for a happy decoupling experience!