DEV Community

Roger Jin
Roger Jin

Posted on

Webhook vs API: What’s the Difference?

For more content like this, follow ButterCMS on Twitter and subscribe to our blog.

Communication is a key driver in the digital era. As humans, we want technology to help us communicate faster, easier and with more people. But in order for that to happen, we had to first find a way to make technologies talk to each other.

That’s where APIs and webhooks come into play.

Both webhooks and APIs facilitate syncing and relaying data between two applications. However, both have different means of doing so, and thus serve slightly different purposes.

To clear up any confusion between the two, let’s take a look at the differences a webhook and an API, and what kind of scenario each one is most appropriate for.

Webhook vs API: The Differences In Simple Terms

To put it simply, an API does stuff when you ask it to, while a Webhook does stuff on it's own when certain criteria is met or scenarios takes place. Let’s dig a little deeper.

An API can used from a server to communicate with example.com. Through that communication, the API can List, Create, Edit or Delete items. The API needs to be given instructions, though.

Webhooks on the other hand are automated calls from example.com to a server. Those calls are triggered when a specific event happens on example.com. For example, if a new user signs up on example.com, the automated call may be configured to ask the server to send out a welcome email.

What is a Webhook?

Sometimes webhooks are referred to as a reverse API, but this isn’t entirely true. They don’t run backwards, but instead there doesn’t need to be a request initiated on your end, data is sent whenever there’s new data available.

To setup a webhook all you have to do is register a URL with the company proving the service you’re requesting data from. That URL will accept data and can activate a workflow to turn the data into something useful. In most cases you can even specify the situations in which your provider will deliver you the data.

Webhooks and APIs differ in how they make requests. For instance, APIs will place calls for data whether there’s been a data update response, or not. While webhooks receive calls through HTTP POSTs only when the external system you’re hooked to has a data update.

When to Use a WebHook

Webhooks are commonly used to perform smaller requests and tasks, however there are situations where a webhook is more appropriate than an entire API.

One common scenario is when your app or platform demands real-time updates, but you don’t want to waste your resources. In this instance a webhook would be beneficial.

Another circumstance to use a webhook over an API would be when the API is very poor, or there isn’t an API to begin with. You could create a workaround solution to give you the data your app requires to function.

However, there is a word of caution about webhooks. Since they aren’t used to regularly request data, and only do so when new data is available, there is a chance you could never learn about new updates if the system goes offline for some reason. You’ll also have less control over the total flow of data, as you have to accept the total data amount that’s available with the given update.

Real-Life Webhook Examples

Many apps and tools do rely on webhooks, but primarily for smaller data requests rather than using them to form the backbone of their service. Still, there are plenty of examples of webhooks being used effectively.

  1. The ButterCMS webhook fires anytime anyone publishes a new blog post or updates content in their CMS.
  2. Zapier is essentially one giant webhook. You link up certain apps together and whenever an event occurs in one it triggers an action in the other.
  3. Stripe has a webhook that will automatically send an email to a customer whenever a subscription payment fails to go through.

What is an API?

API stands for Application Programming Interface. An API is a way for applications and platforms to connect with other applications and platforms via a common communication method. For an API to work there’s a request for data, followed by a response to that request. The data is usually delivered in a format like JSON.

APIs tend to be the framework that a lot of existing software and tools rely upon. For example, an application that creates Twitter trend reports could rely upon an API to continually get the freshest data right from Twitter. Most large apps have multiple APIs they integrate with the expand their service offerings, as you’ll see below.

When to Use an API

APIs work incredibly well when you know you’re going to have a constant change in data. There’s no point in using an API if the data you’re requiring is going to be relatively stagnate. For example, if you’re an eCommerce store that regularly has to update its shipping and tracking data, then you’re going to be making constant requests.

Every time you poll the API you’re going to have new data. If your data isn’t constantly updated, then there’s no guarantee there’s going to be data ready for you at the other end. When this happens you’re simply wasting resources. However, if you’re set on using an API you can impose a call limit, which will limit the number of calls you make in a set time period. Some apps even limit the number of calls you make from the get go in order to reduce resource use on their end.

Real-Life API Examples

Like we mentioned above, APIs are everywhere. According to the latest results from ProgrammableWeb there are currently over 17,000 existing APIs. Below you’ll find a few tools that employ the use of APIs for their tools to function:

  1. As an API-first CMS, ButterCMS has its own REST API has predictable, resource-oriented URLs, and uses HTTP response codes to indicate API errors — the capabilities of which are demonstrated by this React Universal Blog build.
  2. Uber also relies on the Google Maps API, Twilio API, Braintree API, and the SendGrid API to help power their app.
  3. Slack has an API that enables you to integrate their messaging features into your third party apps.

Webhooks and APIs Run In Different Circles

It’s not a case of which is better, because there isn’t a blanket circumstance where one method trumps the other. It’s more of a question regarding the purposes of your application and the kind of data you’re requesting.

To use an example, you can think of an API as a text message you send to a friend to get more information about an event they’re hosting. You ask a question, they send a response.

With a webhook, You tell your friend once to text you whenever they’re organizing another event, just to let you know. You put in the initial request, and they continually send you updates when new information arises.

In the end, most applications end up using both APIs and webhooks together to create a system that can communicate the right types of data at the right times.

This was originally posted here.

Top comments (2)

Collapse
 
jorinvo profile image
jorin

❤️ webhooks

Providing a public API has become pretty common for all kinds of services.
Would love to see more businesses allowing 3rd-party devs to register webhooks.
Can't wait to see all the things people come up with once they have the power to design realtime systems.

Collapse
 
thibmaek profile image
Thibault Maekelbergh

This is a really good article, thank you for this ✌️