banner image

加入 GetBlock 的 TOKEN2049!与我们的 CBDO 会面,成为首批构建我们亚洲基础设施的人之一。

What are Webhooks: Meaning and Examples 

Vance Wood

Vance Wood

August 1, 2024

8 分鐘閱讀

article cover

Web services can communicate with each other in many different ways, and webhooks are one of them. They are specific API endpoints that monitor a system, be it a web server or mobile application, and provide feedback when a specific event occurs in it. That’s what are webhooks used for in software development, as they enable developers to automate many processes.

As blockchain is, essentially, a network of interconnected computers, Web3 developers can use webhooks to track what’s happening in blockchain and perform an action when a certain event occurs. For example, a new block creation or a transaction between certain addresses. Due to the importance of the topic, we have webhooks explained in this introductory article.

Let’s see how webhooks work and how they can be applied in different contexts.

What are webhooks

They are automated HTTP POST request messages from one application to another that are triggered by a specific event. For example, System A sends it after an event occurs in it, while System B is notified and can respond if necessary. The events of interest are specified for each webhook, either via specific webhook creation applications like Zapier or manually using coding.

’Webhooks_meaning_and_how_they_work_POST_request’

Here, System A is a webhook source (server), as it sends a webhook when an event occurs in it, while System B is a webhook destination (client), as it obtains information about this event once it happens.

Webhooks meaning

Thus, their functionality can be summarized in the following points.

  • It sends a request to a source to begin tracking its state.
  • It monitors the source system state and triggers when an event occurs.
  • It responds to the destination only after the occurrence, not earlier.

That’s what do webhooks do: send an automated response to the destination application after receiving its request, but only after a specified event occurs. It makes them highly efficient when combining the functionalities of several applications.

How do webhooks work?

So let’s see their principle of work, advantages, and understand what is a webhook in practice.

Compared to traditional API connections, a destination system (client) pings a source system (server) regularly and obtains a response after each ping. This method is called polling, and while it can be useful to understand the current state of the system, it’s very consuming to use it when it’s necessary to catch some specific event.

’API_polling_webhook_functionality_comparison’

Source: Zoho

That’s where webhooks have a certain advantage, as they require fewer transactions and use less computational resources. Using them, a source system is monitored and reports just when some event happens, without the necessity of regular connection.

Webhooks integrations

As webhooks can be used to integrate data between different applications, they are a perfect solution to connect them in case one app needs some data from another. For example, a payment service connected to the online shop needs to notify the shop’s server when a customer makes a purchase, so the shop can proceed with updates.

What are webhooks in Discord

Discord channels have built-in webhooks support to connect with external apps and obtain updates about their state right into its channels.

Here is a quick algorithm of how to use webhooks in Discord:

  1. Open your server settings, select Integrations, and click Create Webhook.
  2. Specify its name, customize the avatar, and select the channel of destination.
  3. Click Copy Webhook URL to obtain an endpoint link of your webhook.
  4. Use a free or paid integration app, such as Zapier, to connect an endpoint to the application of interest.

After the webhook URL is created and set up, a user will be updated about changes in websites or applications that are connected to it. Using Discord is a good way how to test webhooks, as it’s very easy and doesn’t require any coding. Examples are social media activities, game achievements and rewards, GitHub updates for developers, form submissions, and so on.

Webhooks example

To better understand what are webhooks and how do they work, let’s overview how they can be created in general. Besides Discord, other messengers like Slack enable them, too. For establishing communication between two applications, however, apps like Zapier, Zendesk, or Hubspot are needed. They have thousands of the most popular apps, such as GitHub, Facebook, LinkedIn, and others, which can be connected to your application.

If the desired source or destination application is absent, it’s necessary to establish a connection manually. Most of such apps have special user-friendly interfaces where the app of interest can be added easily if it supports webhooks.

Another way how to create webhooks includes direct incorporation of their code in the application during development. Below is a quick code example using JavaScript, using the Stripe service.

const stripe = require('stripe')('sk_test_z6Wgj3W5n3eYSLEKPRJ4OrE900vpjOnFhP');
const webhookEndpoint = await stripe.webhookEndpoints.create({
 
 enabled_events: ['charge.succeeded', 'charge.failed'],
  url: 'https://example.com/my/webhook/endpoint',
});

Here, it utilizes the Stripe API key example. To work properly, it must contain the API key, URL endpoint, and a list of enabled events that specify what should happen to trigger a webhook. It can include user authorisation, transaction, payment, sending a message, or other app operations.

Therefore, there are two primary ways how to make webhooks: using specialized apps with user-friendly interfaces or coding them manually, which is more time-consuming but enables more customization.

Webhooks in blockchain

While it’s not a full-size webhooks-for-dummies instruction, let’s cover essential information about how they can be utilized in blockchain app development.

As blockchain is a decentralized system of interconnected nodes, webhooks can be used to track a desired event that can occur in the chain. Rather than constantly pinging the API endpoint to track it, it’s possible to create a webhook that constantly monitors the network and responds when it happens.

Want to know more about webhooks in blockchain? Check our short guide!

Below, one can see the difference. The illustration is about our tool, GetBlock Tracker, used to listen to the blockchain and notify a user when some specific event occurs. Now, it’s in development, so stay updated!

’GetBlock_RPC_API_comparison_to_webhooks’’GetBlock_Tracker_webhooks_management’

Source: GetBlock

Webhooks can be integrated into the Web3 application using RPC endpoints, tracking specific events in the blockchain and triggering only after they occur.

How to use webhooks to listen to the blockchain events

Using webhooks for blockchain tracking isn’t different from any other usage, but requires blockchain methods when specifying trigger events. Let’s see how does webhooks work in Web3 development.

For example, there is a web3.js library that has methods to connect to EVM chains and listen to their state. Using these methods, it’s possible to track transactions, monitor block creations, estimate gas prices, and retrieve other crucial information that can be utilized for webhook creation with Ethereum methods. The library @solana/web3.js has similar tools for the Solana blockchain using Solana methods.

As each blockchain has its own set of methods, most of them have specific libraries. With them, Web3 developers can build webhooks that trigger actions when a specific event occurs in the network, adding flexible and efficient functionalities in their dApps. Let’s compare webhooks with the simple API endpoint usage in the table below.

Simple API connection Webhook connection
Sends requests to the server manually and obtain responses immediately. Obtains response from the server (source) only after a specified event occurs.
Requires more computational resources to send requests regularly. Requires much less computational resources and much easier to scale.
Easier to implement, as it’s the client who needs to send API requests to the server. Harder to implement, as it requires both client (destination) and server to support webhooks
Ideal to obtain data on demand. Ideal to obtain real-time updates.

To manage webhooks and API connections properly in blockchain, it’s important to have a stable node endpoint that will be used for connecting your app with blockchain. That’s where the GetBlock node service can help, with its free plan for personal use and several node types offered for blockchain apps on different scales.

Are webhooks secure?

This approach is generally more secure than others, which requires a constant connection or regular pinging to receive updates. However, webhooks send responses that trigger action in another application, so that’s where their vulnerability lies.

It’s potentially possible to forge this response and, for example, trigger a payment to steal money. Therefore, it’s important to verify both the webhook's source and the receiving application. The breach can happen on both sides, so both source and destination must have signatures that will be validated.

To understand how to secure webhooks using signatures, see the diagram below.

’Securing_webhooks_to_avoid_webhooks_security_breach’

Source: Hookdeck

Both sides of webhook-based communication should be protected by signatures, and the transaction can occur only when each of them validates it. Signatures use SHA256 encryption, securing webhooks from intrusions. Therefore, the transaction is protected from both sides and, even if intercepted, cannot be used to hack the application.

Conclusion

A webhook is an HTTP request which is sent by the source system to a destination system when a specific event occurs in it. These systems can be any software entity including websites, apps, and blockchains.

This webhook tutorial can be used as an introduction to their deployment in software development, including Web3 activities. Check our development guides, and stay updated!

常问问题

  • How to use webhooks?

    plus
  • What are webhooks in blockchain development?

    plus
  • How to secure webhooks?

    plus
Vance Wood

Vance Wood

August 1, 2024

8 分鐘閱讀

twittertwittertelegramtelegramLinkedinLinkedin