Webhooks are an essential part of the modern world of digital intercommunication. Sure, there are various ways applications communicate with each other. However, webhooks tend to be the fastest and most efficient method. Moreover, with Web3 starting to revolutionize Web2, Web3 webhooks are what we will focus on in this guide. By the end of this article, you will know how to easily establish webhook communication among decentralized applications (dapps) and other traditional services, such as SMS notifications. Essentially, you will learn how to use Web3 webhooks to listen to smart contract events and trigger desired actions. Also, since smart contracts are automated, you can easily automate even more complex functionalities using proper webhooks.
Fortunately, the Web3 tech stack has come a long way in the last couple of years. It now offers several reputable tools to help you work with Web3 webhooks and other Web3 feats easily. With Moralis’ single workflow, you can cover full stack Web3 development in a single place. However, working with Web3 webhooks is just a cherry on top. Thanks to the ultimate Moralis SDK, this “Firebase for crypto” platform enables you to create next-gen cross-chain dapps. From Web3 authentication to handling off-chain data and on-chain data, Moralis has got your back(end). Also, with Web3 webhooks, Moralis enables you to expand the functionality of your dapps by including other services.
Before we take a closer look at how to use the best Web3 backend platform to work with webhooks for Web3, we must get you all up to speed. Thus, we will start our discussion by answering the “what are webhooks?” question.
What are Webhooks?
To properly understand Web3 webhooks, you need to know what traditional webhooks are and what makes them so important. In short, a webhook is an HTTP request. The latter gets triggered by an event in a source system (sender). Then the triggered request is sent to a destination system (receiver). Furthermore, the request often comes with payload data. Since webhooks are automated, they get sent out automatically when triggered by the events.
Webhooks clearly offers a way for two systems to communicate with each other. Basically, the source system “speaks” to the destination system via HTTP requests when events occur. The source then shares information (payload data) regarding the triggering events. When the systems are applications, webhooks offer apps to send automated information or messages to other apps. Hence, webhooks are particularly common in SaaS platforms (GitHub, Shopify, Stripe, etc.), which support different types of events. For example, this is how Twilio routes phone calls to your number, how Slack can notify you about new WooCommerce orders, and how PayPal informs your accounting app when you receive payments. That said, it’s quite clear that knowing how to use webhooks to automatically push data between apps is quite essential.
A Webhook Example
Let’s look at an example to solidify your understanding of webhooks further. Let’s say you subscribe to a streaming service. The latter wants to send you an email every month when it charges your credit card. Accordingly, the streaming service subscribes to the banking service to send it a webhook when your card is charged for its service. In this case, the banking service is the source, and the charge on your credit card is an event trigger. The webhook is sent to the streaming service’s emailing service (the destination in this example). As a result, every time the event is processed, you receive a notification. Since the banking system’s webhooks include info about the event, you can receive all the details about your payment. Moreover, all of this is automated.
Webhooks in Web3
Now that you know what webhooks are, you are ready to focus on Web3 webhooks. In this case, they serve the same purpose – cross-application communication. Furthermore, Web3 webhooks are also a way for users to receive notifications when a particular event occurs in your dapp. However, since Web3 is decentralized and powered by programmable blockchains (e.g., Ethereum), things are a bit different. Thus, Web3 webhooks need to work with events executed by Web3 contracts.
Like in Web2, webhooks in Web3 enable us to replace the need to continuously poll the database, checking if the state has changed. Instead, the event that interests us will automatically trigger related data to send. As you can imagine, that’s a lot more efficient. So, whenever a smart contract event occurs related to the sender, the latter sends associated data to the webhook URL of the receiver. Furthermore, the receiver application can then send a callback message. This message contains an HTTP status code, letting the sender know that data went through successfully.
Web3 Webhooks and Moralis
This is where you will learn how to set up Web3 webhooks with Moralis. The simplest way to use these webhooks is via Moralis’ cloud functions. However, Moralis’ webhooks also enable you to write your server-side logic in your own environment with any tools you wish to use. This is particularly useful if you want to use a language other than JavaScript, host it yourself for improved testing capabilities, or if you require a specialized library or technology not available with Moralis’ cloud functions. When using the Moralis server, currently available webhooks, besides cloud functions, are the following:
- “beforeSave”
- “afterSave”
- “beforeDelete”
- “afterDelete”
We’ll take a closer look at each of the above webhooks as we move forward.
Note: At the time of writing, you can’t set custom webhooks for the special classes such as “_User” and “_Installation”. Moreover, refer to the “Initial Moralis Setup – Your Gateway to Web3 Webhooks” section below to start using Moralis. This is where you’ll learn to access your Moralis dashboard, which is the prerequisite to using webhooks from Moralis.
Creating Custom Webhooks with Cloud Functions
As mentioned above, cloud function webhooks are great when you want to use a specialized technology not available using cloud functions from Moralis. Moreover, here are the parameters that a webhook cloud function request contains:
- “master” – This parameter is “true” if you use the master key and “false” otherwise.
- “user” – When set, it contains the Moralis user who made the request. It comes in Moralis’ “REST API” format. Moreover, this parameter is not set when the master key is used.
- “installationId” – If this parameter is available, it provides the “installationId” that made the request.
- “Params” – This is a JSON object, which contains the parameters passed to the function. For example: { “foo”: “bar” }.
- “functionName” – This parameter refers to the name of the relevant cloud function.
In order to respond to the webhook cloud function requests, you get to send a JSON object with the key “error” or “success” set:
- “error” – This key will send the error message that you want to return.
- “success” – This key will send back any data your client expects. If the client doesn’t require any data, it will send “true”.
Whenever you want to create cloud function Web3 webhooks, you need to start by writing the function’s code on your own server. Then, you’ll be able to activate a webhook from the Moralis database’s dashboard:
Web3 Webhooks – “beforeSave”
When you use “beforeSave”, the latter will send to your webhook these parameters:
- “master” – This parameter is “true” if you use the master key and “false” otherwise.
- “user” – When set, it contains the Moralis user who made the request. It comes in Moralis’ “REST API” format. Moreover, this parameter is not set when the master key is used.
- “installationId” – If this parameter is available, it provides the “installationId” that made the request.
- “object” – This parameter contains the Moralis object (in Moralis’ “REST API” format). For example: { “className”: “TestObject”, “foo”: “bar” }.
- “triggerName” – This parameter is “beforeSave” for this webhook.
As with the cloud function webhooks, to respond to “beforeSave” requests, you need to send a JSON object with the key “error” or “success”. However, in addition, the “error” key in this case also cancels the save request, which means that the code won’t store the object in the Moralis database.
You can also return a JSON object (see the format below) to override the values that you want to save for the object:
{
"className": "AwesomeClass",
"existingColumn": "sneakyChange",
"newColumn": "sneakyAddition"
}
Web3 Webhooks – “afterSave”
With webhooks, you can also run some code after saving an object. This is where “afterSave” webhooks come into play. The parameters sent to your webhook for this request are the same as for “beforeSave”. The only difference is in the “triggerName” parameters, which is “afterSave” in this case. Furthermore, “afterSave” triggers don’t require any response.
Web3 Webhooks – “beforeDelete”
When you want to run some code before deleting an object, you can use “beforeDelete” webhooks. These types of requests send the same set of parameters to webhooks as “beforeSave” and “afterSave”. Again, the only different parameter is the “triggerName” – “beforeDelete”. Moreover, just like with the above Web3 webhooks, to respond to a “beforeDelete” request, you need to send a JSON object with the “error” or “success” key. Of course, in this case, if an “error” is returned, it will cancel the delete. Hence, the object remains in the Moralis database.
Web3 Webhooks – “afterDelete”
You should use the “afterDelete” webhook when you want to run some code after deleting an object. The parameters sent to your webhook are the same as those for other triggers. Though, the “triggerName” is “afterDelete”. Nonetheless, just like with the “afterSave”, “afterDelete” Web3 webhooks require no response.
Moralis’ Webhooks in Action – Example Project
To learn more about how to work with Moralis’ Web3 webhooks, we recommend you take on our example project on that topic. As such, make sure to use the video below to follow our in-house expert’s lead. He’ll show you how to get blockchain SMS notifications. However, in order to get Web3 events with SMS messages, you will need to complete the initial Moralis setup (see the next section).
Note: In the above video tutorial, the expert uses the old Moralis admin UI. So, in case you want your Moralis admin panel to match the one in the video, switch to our legacy UI:
Initial Moralis Setup – Your Gateway to Web3 Webhooks
- Create your free Moralis account or log in to your existing one:
- In your Moralis admin panel, create a new dapp:
- Select your environment (testnets are best for example projects):
- Network selection (we recommend “Polygon Mumbai”):
- Choose the city closest to your location:
- Name your dapp:
Once your dapp is live, you can access your Moralis database, where you can create new Web3 webhooks. Start by clicking on the “Settings” button for the above-created dapp:
Then, select the “Database” tab, where you need to click on the “Access Database” button:
Finally, inside your Moralis database dashboard, click on “Webhooks” from the side menu, followed by a click on “Create a Webhook”:
Summary of Web3 Webhooks – The Ultimate Guide to Blockchain Webhooks
Today, you had a chance to learn what Web3 webhooks are and how to use them with Moralis. As such, we took a closer look at the “beforeSave”, “afterSave”, “beforeDelete”, “afterDelete”, and cloud function webhooks. We also gave you a chance to practice using webhooks following our tutorial. Lastly, we also covered the initial Moralis setup, which you must complete to use Moralis’ webhooks.
If the above example project was not your first rodeo with Web3 development, you might be ready to tackle your own projects. However, if you need some more practice, we encourage you to visit the Moralis blog and the Moralis YouTube channel. Aside from countless example projects, these are great avenues to learn more about blockchain tech. Some of the latest articles show you how to create an AR metaverse NFT, how to build a Web3 augmented reality, Web3 blog dapp (Web3 Medium), social media dapp, and a Web3 role-playing game. Also, it explores what you need to know about an Ethereum dapp API, Polygon dapp API, Binance dapp API, Ethereum NFT API, and The Merge. Essentially, all this free content can help you become a blockchain developer for free.
However, if you want to become a Web3 developer fast and with confidence, taking a more professional approach usually works better. If that sounds interesting to you, make sure to consider enrolling in Moralis Academy.