November 25, 2022

Moralis Webhooks – An Introduction to Web3 Webhooks

Table of Contents

With Moralis’ enterprise-grade Streams API, you can avoid hassles such as building complex data pipelines, setting up abstractions, connecting to RPC nodes, etc. Instead, you receive a seamless workflow to set up Moralis webhooks for real-time wallet notifications, monitoring assets, token sales, and much more! So, how does the Streams API work?

Bullet list showing how Moralis' Streams API works.

The accessibility of the steps in the above image is demonstrated below, where you will find the complete code for setting up a Web3 stream programmatically via Moralis’ JavaScript SDK. The stream monitors wallet activity based on an address, making sure you receive Moralis webhooks for all transactions: 

import Moralis from 'moralis';
import { EvmChain } from "@moralisweb3/common-evm-utils";

Moralis.start({
  apiKey: 'YOUR_API_KEY',
});

const stream = {
  chains: [EvmChain.ETHEREUM, EvmChain.POLYGON], // List of blockchains to monitor
  description: "monitor Bobs wallet", // Your description
  tag: "bob", // Give it a tag
  includeNativeTxs: true, // select the events to monitor
  webhookUrl: "https://YOUR_WEBHOOK_URL", // Webhook URL to receive events
}

const newStream = await Moralis.Streams.add(stream);
const { id } = newStream.toJSON(); // { id: 'YOUR_STREAM_ID', ...newStream }

// Now we attach Bobs address to the stream
const address = "0x68b3f12d6e8d85a8d3dbbc15bba9dc5103b888a4";

await Moralis.Streams.addAddress({ address, id });

Overview

Applications, platforms, and other systems have several ways to communicate. However, webhooks are one of the most efficient alternatives. Webhooks are essential in our highly connected online space, ensuring that decoupled systems can share data. Within the decentralized web, webhooks are generally referred to as ”Web3 webhooks”, which play an equally important role in enabling communications between decentralized applications (dapps) and blockchain networks. Since decentralized webhooks play a vital role within the blockchain space, developers require efficient tools to set them up. If this sounds exciting and you want to learn more about the intricacies of these webhooks, join us in this article as we dive deeper into this topic! 

Along with exploring the ins and outs of blockchain webhooks, the article additionally features a tutorial for setting them up with Moralis’ Streams API. By the end of this article, you will know how to set up Web3 streams to receive Moralis webhooks and monitor various blockchain events! Furthermore, the Streams API is one of Moralis’ many Web3 APIs facilitating a more seamless blockchain development experience. If you are serious about getting into Web3 development, you should also explore the other interfaces. For example, check out the EVM API, allowing you to create EVM-compatible dapps in no time! If this sounds exciting, check out our guide on how to build a Web3 app

Nevertheless, no matter if you want to set up Moralis webhooks or develop dapps in general, sign up with Moralis. With a Moralis account, you access a more seamless developer experience, enabling you to fully access the power of blockchain technology! 

Unlock the power of blockchain with Moralis.

What are Web3 Webhooks? 

This section will start by exploring conventional webhooks, as this is necessary to adequately grasp the concept of Web3 webhooks. In short, webhooks are HTTP requests sent to a destination system (receiver) triggered by particular events in a source system (sender).

In an exceedingly interconnected online space, systems can not function optimally in isolation. Consequently, there is a high demand for intercommunication solutions, allowing online systems to exchange data efficiently. Systems and platforms have several ways to communicate; however, webhooks are some of the most efficient and effective alternatives. 

When utilizing webhooks, a source system communicates with a destination system via HTTP requests that are delivered to a URL when given events occur. Moreover, the source system generally shares additional event details through so-called ”request payloads”. 

Now, with a more profound understating of traditional webhooks, let us look at what they entail in a Web3 context. Accordingly, we will answer the question, ”what are Web3 webhooks?” below! 

Much like conventional webhooks, Web3 webhooks have the same purpose, to enable cross-system intercommunications. However, since the decentralized web is built on programmable blockchains, you can imagine that there are slight differences. Blockchain developers use Web3 webhooks within the crypto space to listen to various events occurring on-chain. In turn, they receive real-time request payloads associated with those activities. Some prominent examples are asset transfers, a smart contract emitting an event, certain wallets performing an action, etc. 

Graph showing how Moralis webhooks work.

As you can imagine, blockchain webhooks play an essential role within the Web3 ecosystem, allowing dapps and blockchains to communicate. Consequently, it is beneficial for Web3 developers to have efficient tools for setting up webhooks quickly to push data between systems, which is where Moralis’ Streams API enters the picture! 

Moralis’ Streams API – Easiest Way to Create Moralis Webhooks 

Since dapps and other Web3 projects are blockchain-based, they require on-chain data to work optimally. However, from a conventional perspective, it has been a relatively cumbersome task to query blockchain data. Why? Because it requires developers to build abstractions, run buggy RPC nodes, set up complex data pipelines, etc. Fortunately for you, this is no longer the case, as you can utilize tools like Moralis’ Web3 Streams API to set up Moralis webhooks! 

Moralis Streams API announcement.

With the Streams API, you can now easily set up Web3 streams to receive Moralis webhooks in only minutes. As such, it has never been easier to stream on-chain data into the backend of dapps and other Web3 projects. What’s more, thanks to Moralis’ cross-chain capabilities, the Streams API is compatible with multiple networks. This includes Ethereum, BNB Chain, Polygon, Avalanche, and many more. 

You can set up streams to receive Moralis webhooks on these networks and other layer-2 (L2) platforms whenever:

  • Someone partakes in your token sales.
  • An asset is swapped, staked, received, sent, etc. 
  • A battle initiates in your blockchain game.
  • Or any other smart contract event triggers based on your filters.

So how does the Streams API work? 

  1. Provide an address. If the provided address is a smart contract, provide the topic of the event you want to receive information about. 
  2. Apply filters determining when to receive Moralis webhooks. 
  3. Select the chain(s) you are looking to monitor.
  4. Specify your webhook URL. 
  5. Receive Moralis webhooks when events trigger on-chain matching your filters. 

The following section applies each step in a practical example to make the above more understandable. If this sounds exciting, join us as we show you how to create streams to receive Moralis webhooks in no time! 

How to Set Up Moralis Web3 Webhooks 

With a better understanding of blockchain webhooks and the Streams API, let’s move on to the central part of this article. As such, we’ll show you how to set up Moralis webhooks. If you follow along from here, you will learn how to stream on-chain data directly into the backend of all future blockchain projects! 

You primarily have two options for creating Web3 streams: programmatically with the JavaScript SDK or via Moralis’ admin panel. This tutorial focuses on the former, showing you how to set up Moralis webhooks programmatically to stream any on-chain data into your projects! 

Moralis Logo.

To showcase the accessibility of working with Moralis’ Streams API, the tutorial specifically illustrates how to create a Moralis webhook for monitoring incoming and outgoing transactions of a particular Web3 wallet. However, this is only an example. If you complete the tutorial, you will be able to query other datatypes from any blockchain just as easily. What’s more, if you want to learn how to create streams via the Moralis admin panel instead, you will find a complete breakdown for doing so by checking out the official Streams API documentation

Without further ado, let us jump straight into the tutorial and show you how to stream on-chain data into your applications via Moralis webhooks programmatically! 

Create Moralis Webhooks Programmatically 

This section explains how to create a Web3 stream programmatically to receive Moralis webhooks whenever transfer events occur in relation to a certain wallet. To make this possible, we use Moralis’ Streams API and JavaScript SDK. If this sounds exciting, join us as we dissect the process from start to finish! 

To start, open your preferred integrated development environment (IDE) and set up a JavaScript project. Within the project folder, create a new file and install all Moralis dependencies. If you are using NodeJS, open a terminal and run the following command: 

npm install moralis @moralisweb3/common-evm-utils

With the JavaScript project at your disposal, open the file and initialize the Moralis JavaScript SDK by adding the code from the snippet below at the top of the file: 

import Moralis from 'moralis';
import { EvmChain } from "@moralisweb3/common-evm-utils";

Moralis.start({
  apiKey: 'YOUR_API_KEY',
});

By inspecting the code above, you will quickly notice that you need to add your Moralis API key by replacing ”YOUR_API_KEY. You can acquire the key by signing up with Moralis and copying the key from the “Home” tab: 

Copy Moralis API key.

With the key added to the JavaScript file and Moralis initialized, you can create a new ”stream” object, to which you must add a few parameters. Specifically, you need to specify the chains, a description, a tag, and your webhook URL. To clarify things, this is what it can look like: 

const stream = {
  chains: [EvmChain.ETHEREUM, EvmChain.POLYGON], // List of blockchains to monitor
  description: "monitor Bobs wallet", // Your description
  tag: "bob", // Give it a tag
  includeNativeTxs: true, // select the events to monitor
  webhookUrl: "https://YOUR_WEBHOOK_URL", // Webhook URL to receive events,
}

To top things off, call the ”Moralis.Streams.add(stream)” function and pass the ”stream” object as a parameter. In combination with calling the function, add a new ”{ id }” object and the wallet address you are looking to monitor: 

const newStream = await Moralis.Streams.add(stream);
const { id } = newStream.toJSON(); // { id: 'YOUR_STREAM_ID', ...newStream }

// Now we attach bobs address to the stream
const address = "0x68b3f12d6e8d85a8d3dbbc15bba9dc5103b888a4";

await Moralis.Streams.addAddress({ address, id });

Consequently, you should now have a JavaScript file with code similar to the one found below: 

import Moralis from 'moralis';
import { EvmChain } from "@moralisweb3/common-evm-utils";

Moralis.start({
  apiKey: 'YOUR_API_KEY',
});

const stream = {
  chains: [EvmChain.ETHEREUM, EvmChain.POLYGON], // List of blockchains to monitor
  description: "monitor Bobs wallet", // Your description
  tag: "bob", // Give it a tag
  includeNativeTxs: true, // select the events to monitor
  webhookUrl: "https://YOUR_WEBHOOK_URL", // Webhook url to receive events,
}

const newStream = await Moralis.Streams.add(stream);
const { id } = newStream.toJSON(); // { id: 'YOUR_STREAM_ID', ...newStream }

// Now we attach bobs address to the stream
const address = "0x68b3f12d6e8d85a8d3dbbc15bba9dc5103b888a4";

await Moralis.Streams.addAddress({ address, id });

From here, all that remains is running the program. To do so, open a new terminal, ”cd” into the JavaScript file, and run the command below: 

node “FILE_NAME”

Required Test Webhook 

Whenever you create a new stream – whether you do it programmatically or via Moralis’ admin panel – you will always receive a Moralis test webhook. As such, to make the stream operational, you need to return status code 200. You can find what the test body looks like down below: 

{
  "abi": {},
  "block": {
    "hash": "",
    "number": "",
    "timestamp": ""
  },
  "txs": [],
  "txsInternal": [],
  "logs": [],
  "chainId": "",
  "tag": "",
  "streamId": : "",
  "confirmed": true,
  "retries": 0,
  "erc20Approvals": [],
  "erc20Transfers": [],
  "nftApprovals": [],
  "nftTransfers": []
}

As soon as you return the status code, you have successfully set up your Web3 streams to receive Moralis webhooks. Consequently, whenever the specified address is involved in incoming or outgoing transactions, you will receive Moralis webhooks with request payloads to your webhook URL! 

Congratulations! You have now successfully created your first stream programmatically to receive Moralis webhooks. As such, you can now use Moralis’ Streams API and the same fundamental principles to stream any on-chain data into your Web3 projects’ backend! 

However, if you, at any point during the tutorial, experienced any issues, check out the official Moralis Streams API documentation. In doing so, all your questions should hopefully get answered. Otherwise, you can also join Moralis’ Discord channel to receive top-level assistance from our talented community engineers.

Moralis Webhooks Firebase Integration 

You are now familiar with the Streams API and can use this interface to set up Moralis webhooks. Hence, you can now listen to and monitor blockchain activity, including smart contract events, token transfers, NFTs being minted, etc. Even though you can easily set up Moralis webhooks, you still need a way to store the data on your backend. This is where the Firebase integration enters the equation. 

With Moralis’ Firebase integration, you can quickly start using Moralis webhooks. Also, you get to do so without the hassle of having to set up your own Web3 backend infrastructure. Firebase takes care of the heavy lifting by providing a complete hosting solution. As such, you can use the Streams API to seamlessly stream on-chain data into your Firebase backend via Moralis webhooks to monitor blockchain activity in real time! 

If you want to know more about Moralis’ Streams Firebase integration, consider checking out the Moralis YouTube video below. By watching this clip, you will learn how to use the Streams API in combination with Firebase to listen to any blockchain events. 

Moralis Webhooks – Summary

In this article, you got to explore the intricacies of webhooks. This taught you that they are HTTP requests sent from source systems to destination systems based on certain events. As such, they enable cross-system communications, both within the Web2 and Web3 space. The article also presented a tutorial teaching you how to set up Moralis webhooks programmatically with the JavaScript SDK through the Streams API! 

If you have followed along this far, you are now familiar with webhooks and how to set them up. As a result, you can create Web3 streams to receive Moralis webhooks to monitor particular on-chain events. This includes wallet activities, asset transfers, in-game events, and more! Additionally, thanks to Moralis’ Firebase integration, you can easily stream blockchain data directly into your Firebase backend.

If you found this tutorial helpful, we recommend checking out additional content here at the Moralis Web3 blog. For example, learn how to create an AWS Lambda function or explore the intricacies of Amazon GameSparks. You can also learn more about webhooks through our guides on Ethereum webhooks and Polygon webhooks

Ultimately, if you want to set up Moralis webhooks, sign up with Moralis immediately. You can create your account for free, and it only takes a couple of seconds!

Moralis Money
Stay ahead of the markets with real-time, on-chain data insights. Inform your trades with true market alpha!
Moralis Money
Related Articles
January 11, 2023

Solana Wallet Overview – What is a Solana Wallet? 

August 12, 2022

Moralis Projects – Web3 Magic Treasure Chest

September 19, 2023

ERC20 Token Balance – Get the Balance of ERC20 Tokens

August 2, 2022

How to Create an NFT Candy Machine

November 8, 2022

How to Get All NFTs from a Contract

February 4, 2023

Tools and Best Practices for Smart Contract Security

August 14, 2022

How to Fetch an NFT by Address API – Exploring the NFT API

October 12, 2023

Rinkeby Faucet Guide – What is the Rinkeby Faucet and Testnet?