August 16, 2022

NodeJS SDK for Web3 – Exploring Moralis’ NodeJS SDK

Table of Contents

JavaScript (JS) is a programming language giant. However, since it is only used for client-based scripting, it can prove bothersome for developers since they need to work with multiple additional languages. This is where NodeJS enters the picture. NodeJS is a runtime environment providing everything necessary to run JS-based programs outside of browsers. Moreover, within the Web3 development space, you now have the opportunity to work with NodeJS in a more accessible way, thanks to Moralis’ NodeJS SDK. This SDK bridges the gap between Web2 and Web3, allowing you to create powerful dapps (decentralized applications) with the ease of use of Web2. For example, it allows you to authenticate Web3 users with a single snippet of code. In this article, we will explore this NodeJS SDK for Web3 in further detail! 

However, before we dive deep into the SDK, we will take a closer look at what NodeJS is and why this environment is advantageous. In doing so, we will also learn more about the features of NodeJS and why someone would opt to use this development tool.

What’s more, the NodeJS SDK for Web3 is only one of the many excellent features of Moralis. Along with the SDK, Moralis also makes the implementation of Web3 syncs and Web3 webhooks significantly more accessible. Additionally, you can learn more about Web3 authentication, how to pull data from the Polygon blockchain or implement blockchain syncs, all valuable features when developing blockchain dapps here at Moralis. 

If you are a Web3 developer and want to increase your efficiency, consider signing up with Moralis. You can create an account for free and reap the full benefits of the platform immediately! 

What is NodeJS? 

JS is one of the most popular programming languages in the world. This language powers millions of websites and web applications. Further, JS allows developers and designers to easily implement features. If you are new to programming and would like to learn JS, make sure to enroll in the following Moralis Academy course: “Learn JavaScript Programming“.

JS was initially released in 1995 and is only used for client-side scripting. However, this forces developers to work with multiple languages and frameworks when building a full stack to connect frontend and backend components. As such, to make JS development more accessible and manageable, NodeJS was introduced.

NodeJS is a runtime environment including everything you might need to execute a program written in JS. The environment is a single-threaded, cross-platform, open-source runtime environment allowing developers to build fast and scalable networking and server-side applications. Furthermore, NodeJS runs using the V8 JS runtime engine and utilizes an event-driven, non-blocking I/O (input/output) architecture. This makes this environment both efficient and effective, ultimately suggesting that NodeJS is suitable for real-time applications. 

Moreover, NodeJS has one of the largest collections of public and private packages that are stored in the NPM registry, where a lot of open source software is hosted, making it one of the largest open source ecosystems that exist today. As a result, this provides software developers huge quantity of open source tools that are unmatched in other language ecosystems, making it easy for people to start JavaScript development.

What’s more, NodeJS is uniquely suitable for blockchain development making this an attractive tool when developing dapps or other Web3 projects. So, with a better understanding of what NodeJS is, we can proceed and take a closer look at why you would want to use NodeJS in your development endeavors. 

Why Use NodeJS? 

As was mentioned in the previous section, NodeJS supports higher scalability and allows developers to create high-performing dapps. This is essential within the Web3 development space, where scalability is an ever-growing issue and challenge. As such, developers must have the proper tools to environments to combat these problems. 

However, to fully understand the power of NodeJS and why you would want to use it when developing dapps, we will explore some of its features: 

  • Event-Driven, Non-Blocking I/O Architecture – This architecture of NodeJS makes sure that node processes do not lock up an entire website or application when retrieving single responses. As such, NodeJS simply move on to the next event in the queue until it is empty. 
  • Both Client and Server-Side – NodeJS makes JS development more manageable and accessible as it runs both the client and server-side of a web application. Also, NodeJS allows web applications to run with two-way, real-time connections. This ensures that both servers and clients have the ability to initiate communications and exchange data freely. 
  • Reusing Code – Programmers running JS-heavy apps can benefit from the potential to reuse code. As such, you can avoid the hassle of context switching between search and edit tools. NPM (node package manager) is known for code reuse, where developers can access more than a million packages in their development endeavors. 

Now, with a better understanding of NodeJS and why this environment is helpful, we can take a closer look at the Moralis NodeJS SDK for Web3 development! 

NodeJS SDK for Web3

The following sections dive deep into Moralis’ NodeJS SDK for Web3 development. The SDK makes Web3 development significantly more accessible, allowing you to create Web3 projects with ease. As such, if you are looking to become a blockchain developer, follow along as we explore the intricacies of Moralis’ NodeJS SDK for Web3! 

The NodeJS SDK for Web3 is the first step in implementing Moralis 2.0 to make Moralis compatible with any backend. This SDK is set up in a modular fashion and is fully typescript supported, making it accessible for you to use.

The kit includes numerous features, including an EVM (Ethereum Virtual Machine) API, Solana API, Web3 authentication, etc. However, in this article, we will direct our attention toward the following three areas: 

  1. EVM Chains
  2. EVM Addresses
  3. Token Balances

If you prefer watching videos to learn and educate yourself, make sure to check out the following clip from Moralis’ YouTube channel. This tutorial provides a great introduction to Moralis’ new NodeJS SDK for Web3 development and a few examples:

Nonetheless, we will additionally cover the same features and use cases in this article. As such, feel free to follow along as we explore the NodeJS SDK for Web3 even further! However, before taking a closer look at the features, we will quickly cover how to set up a project of your own. 

Setting Up Moralis’ SDK 

We will start this brief guide by showing you how to set up your application to use the Moralis NodeJS SDK. This is a simple process, and based on the video above, all you need is the following snippet of code to initialize the NodeJS SDK for Web3: 

    apiKey: env.MORALIS_API_KEY,
    logLevel: 'error',
    formatEvmAddress: 'checksum',
    formatEvmChainId: 'decimal',

All we really do here is call the ”Moralis.start()” function and provide an API key. Furthermore, you can additionally see other options such as ”logLevel”, ”formatEvmAddress”, and ”formatEvmChainId”. 

From the snippet above, ”logLevel” is set to ”error”. However, if you are looking to debug, this can be changed to, for example, ”verbose”. What’s more, the additional options are used to format addresses and chain IDs. 

Nonetheless, that is it for setting up your project. If you would like even more detailed information on how to get going with a new project, make sure to check out the official Moralis 2.0 documentation. For example, take a closer look at the “Your First Dapp” section and learn how to use the NodeJS SDK for Web3! 

NodeJS SDK for Web3 – EVM Chains

Now that you know how to set up a project, we will continue by taking a closer look at the 2.0 version of the SDK and how it handles EVM blockchains. The most significant difference is that the NodeJS SDK does not handle the chains as decimals or hex strings. Instead, Moralis now handles these chains like separate class instances called ”Moralis data types”. 

You can create a new class instance through one of the following two code snippets: 

const chain = EvmChain.ETHEREUM; 

Once you have created the class instance, you can access various information. For example, here is a list of commands you can use: 

console.log('decimal', chain.decimal);
console.log('hex', chain.hex);
console.log('format', chain.format());
console.log('currency', chain.currency);

The initial two lines will console log this instance’s decimal and hex values. Moreover, you also have the “format()” function. This function will format this information based on the prerequisites we specified in the earlier section when initiating the SDK:

This is essential since this function will be called on any EVM API endpoint. So, if the EVM API returns a chain in one of the data responses, it will be formatted based on your specifications from above. Moreover, along with the “format()” function, there are some utility methods to get the name or data about native tokens. However, this is not all there is to the API. You can additionally check out the Moralis 2.0 docs regarding the EVM API to learn more about this tool! 

What’s more, if you want to develop EVM-compatible dapps, make sure to check out Moralis’ Ethereum dapp API or Polygon dapp API. These tools allow you to create Ethereum dapps or build a Polygon dapp easily and efficiently! 

NodeJS SDK for Web3 – EVM Addresses

Next up, we will look closely at how the NodeJS SDK for Web3 works with an EVM address. This is what it might look like in a project: 

const address = EvmAddress.create(EVM_ADDRESS); 
console.log('lowercase', address.lowercase);
console.log('checksum, address.checksum);
console.log('format', address.format());

The first thing we do here is to create an address using a lowercase string. When the address is created, you can read the lowercase string, checksum, call the “format()” function, etc., which we, in this case, console log. As such, you have several options when it comes to EVM addresses. 

As a reminder, you can check the documentation for more information on this! 

NodeJS SDK for Web3 – EVM API

In this section, we look at how the EVM API works and, more specifically, how you query balances. Compared to the previous version of the SDK, we now refer to the API as “EvmApi” instead of “Web3Api”. What’s more, for Solana, we now have “SolApi” as well, depending on which network you are developing. 

To illustrate how this works, we have a quick example where we query the token balance on Ethereum for a particular address. This is what the code looks like: 

const tokenResponse = await Moralis.EvmApi.account.getTokenBalances({
    chain: EvmChain.ETHEREUM,
console.log('Json result', tokenResponse.toJSON()[0]);
console.log('Direct api result', tokenResponse.raw[0]);
console.log('Data with dataTypes', tokenResponse.result[0]);

As you can see, we call the ”getTokenBalances()” function where the chain and address are specified. An essential distinction between the previous SDK and the new version is that the data regarding token balances are returned in three different ways. 

First, it is returned with a “toJSON()” function, which returns the values as primitive data types that can be directly used in your applications. Accordingly, you do not need to worry about parsing any returned data. 

Second, the SDK returns the results containing all the internal data types. If you use this internally, you have access to all the utility functions of the SDK. Lastly, Moralis additionally returns the raw data as it comes from the API. However, some data is still formatted to make it more consistent. 

That’s it for this short guide! Hopefully, you better understand Moralis’ NodeJS SDK for Web3 and how it functions. It is now up to you to use this information when creating dapps, as Moralis is compatible with most backends. 

Moralis’ NodeJS SDK for Web3 – Summary

In this article, we took the time to explore the intricacies of NodeJS and why this environment is useful. We dove deeper into the features of NodeJS, providing insight into how it benefits JS developers. The architecture of NodeJS makes it ideal for creating web applications more efficiently, and it’s suited for scalability. These two features provide an edge, not only in traditional development but within the Web3 industry alike. 

Following this, we took a closer look at the Moralis NodeJS SDK for Web3. In doing so, we decided to delve deeper into the 2.0 launch of this development kit. To illustrate how the SDK deviated from the previous version, we took a closer look at how this tool works in relation to the following three areas: 

  1. EVM Chains
  2. EVM Addresses
  3. Token Balances

However, these are only some examples in which the SDK comes in handy. For example, if you want to get into Solana programming, you can check out the Solana API. This API allows you to create sophisticated dapps for the Solana blockchain. If this interests you, check out our guide on how to build a Solana dapp in three steps

Moreover, if you are a Web3 enthusiast, visit the Moralis blog and read up on the most recent Web3 development content. If this sounds exciting, we recommend taking a closer look at our articles that dives deeper into different types of DAOs, on-chain data, or how to build on Web3

So, if you have more interest in Web3 development and want to build dapps, sign up with Moralis now! Creating an account is free and 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
October 30, 2023

Blockchain API Providers – Comparing Top Blockchain API Services

October 6, 2023

Crypto Market Data API – Get Crypto Trading Data

October 16, 2023

How to Get the Current Price of an NFT – NFT API Guide

January 15, 2023

ERC721 vs ERC1155 – Full-On ERC721 and ERC1155 Comparison

November 14, 2022

How to Get All NFTs from a Collection in 3 Steps

October 11, 2023

Check Wallet Activity and Get Crypto Address Labels

December 26, 2022

Exploring Verse – A New Metaverse Programming Language from Epic Games

January 9, 2023

Sepolia Testnet Faucet – What is a Sepolia Faucet?

October 31, 2023

Complete Blockchain API Tutorial with API Examples