November 13, 2023

Top Block Explorer API – How to Get All the Blockchain Data You Need

Table of Contents
Top Block Explorer API - How to Get All the Blockchain Data You Need

If you want to build a block explorer – or implement similar functionality into your decentralized applications (dapps) – you’ll likely want to leverage a comprehensive block explorer API. And, if this is the case for you, you’ve come to the right place!

In today’s article, we’ll introduce you to Moralis’ industry-leading Web3 API suite – the ultimate tool for building block explorers. With our premier interfaces, you can effortlessly get block data, token prices, wallet balances, and much more. If you’re eager to get going, here are three prominent Moralis API endpoints that can come in handy when building a block explorer: 

  • getBlock() – Query the contents of a block by its block number or hash:
const response = await Moralis.EvmApi.block.getBlock({
  "chain": "0x1",
  "blockNumberOrHash": "18541416"
});
  • getWalletTransactions() – Fetch all transactions of any address:
const response = await Moralis.EvmApi.transaction.getWalletTransactions({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});
  • getWalletTokenBalances() – Get the token balances of any wallet: 
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});

For a more detailed breakdown of the endpoints above and a tutorial on how to call them, join us in this article as we explore the intricacies of Moralis’ Web3 APIs. Furthermore, if you prefer watching videos to learn, check out the clip below, where we cover some prominent endpoints needed to build your own block explorer: 

Also, if you want to use our tools and APIs to save valuable development time and resources, don’t forget to sign up with Moralis. You can create your account free of charge, and you’ll be able to leverage blockchain technology’s full power immediately! 

Overview 

We’ll kickstart today’s article by diving into the ins and outs of block explorers. From there, we’ll cover the intricacies of block explorer APIs and explain why you might need one when building dapps. Next, we’ll introduce you to the industry’s premier API provider: Moralis. Additionally, we’ll cover some prominent endpoints that you’ll need when building block explorers or implementing similar functionality into your projects. Lastly, to top things off, we’re going to give you a block explorer API tutorial where we show you how to get the token balances of any wallet in three steps: 

  1. Set up Moralis
  2. Write a Script Calling the getWalletTokenBalances() Endpoint
  3. Run the Code

If this sounds intriguing, join us below as we kick things off by looking at what a block explorer is! 

What is a Block Explorer? 

Block explorers – also commonly referred to as blockchain explorers – are software applications that allow you to extract, visualize, and review blockchain data. This includes essential information about blocks, smart contracts, events, transactions, wallet balances, and much more! 

Most blockchain networks have their own distinct block explorers. For example, some prominent examples include Ethereum’s Etherscan, Polygon’s PolygonScan, and BNB Smart Chain’s (BSC) BscScan.

So, what exactly does a block explorer do? To answer this question, let’s look at some key features of block explorers. And let’s use Etherscan as an example: 

  • View Block Data: With Etherscan, you can seamlessly check the contents of any block on Ethereum. This includes information such as timestamps, transactions, gas used, gas limit, and much more: 
Etherscan block explorer - timestamps, transactions, gas used, gas limit, and much more
  • Explore Token Data: Explore any token address to get data such as the total supply, number of holders, transfers, prices, market caps, and information about underlying smart contracts: 
Etherscan block explorer showing total supply, number of holders, transfers, prices, market caps, and information about underlying smart contracts
  • Check Wallet Addresses: Check any wallet address and get information like native balances, token balances, transactions, internal transactions, transfer histories, NFT transfers, and much more: 
native balances, token balances, transactions, internal transactions, transfer histories, NFT transfers, and much more on Etherscan

To be able to effortlessly fetch and display the information above, including block data, token prices, wallet transfers, etc., directly from the blockchain, software applications like Etherscan leverage block explorer APIs! 

But what is a block explorer API? Also, why should you use one?

What is a Block Explorer API? 

A block explorer API (application programming interface) is a set of rules, protocols, and methods that allow you to seamlessly interact with a blockchain to integrate block explorer functionality and data into your projects. Consequently, a block explorer API essentially provides a structured way to access data about blocks, transactions, wallets, crypto addresses, and much more! 

Art illustration - blocks connected via software - illustration a block explorer API

But why do you need a block explorer API?

Querying data directly from a blockchain network is a tedious and time-consuming task. And from a conventional perspective, it required developers to, for instance, run a full node themselves and build their own infrastructure for data retrieval. 

Fortunately, this is no longer the case with sophisticated tools like block explorer APIs, as they abstract away a lot of complexity associated with blockchain technology, giving you a standardized way to communicate with the various networks. 

All in all, a block explorer API makes the process of querying data from a blockchain much easier. And with interfaces like these, you can leverage premade methods and protocols, meaning you don’t have to reinvent the wheel. As a result, block explorer APIs allow you to save both a lot of development time and resources, underscoring the crucial role these interfaces have within the blockchain space! 

So, which is the best block explorer API provider? 

Introducing Moralis: The Ultimate Block Explorer API Provider 

Moralis is an industry-leading API provider, and our suite of premier interfaces makes Web3 development as seamless as Web2. Consequently, when working with Moralis, it has never been easier to build everything from NFT marketplaces to your own block explorer! 

Furthermore, in our suite of Web3 APIs, you’ll find everything that a block explorer API has to offer and more. As such, when working with Moralis, you can effortlessly fetch block data, transactions, balances, token prices, NFTs, etc., with only single lines of code. 

Marketing banner - Art image - Moralis Block Explorer API

But why should you be using our Web3 APIs? 

To answer the question above, let’s look at three prominent benefits of Moralis: 

  • Scalability: Our Web3 APIs are built to scale. Consequently, you don’t have to worry when your projects grow and see increased adoption, as Moralis will have no trouble handling the traffic.
  • High Performance: Our APIs are the industry’s premier interfaces. Choose any metric – whether you want to measure by reliability, data coverage, speed, or anything else – our Web3 APIs outperform the competition every day of the week. 
Moralis vs. Competition Block Explorer APIs - Graph with results
  • Cross-Chain Compatability: With our Web3 APIs, you can seamlessly build across the most popular blockchain networks, including Ethereum, BNB Smart Chain, Polygon, Avalanche, Gnosis, Chiliz, and many others. As such, it has never been easier to build cross-chain compatible dapps! 

So, what tools can you find in Moralis’ Web3 API suite? 

Well, here’s a list of some of our most prominent interfaces: 

If you want to explore them all, check out our Web3 API page! 

How to Build a Block Explorer with Moralis’ API Suite 

Now, with an overview of Moralis’ Web3 API suite, you might be wondering how you can leverage our tools to build a block explorer yourself. However, since block explorers are quite extensive pieces of software, we won’t be able to cover all functionality in this tutorial. Instead, we’ll focus on a few examples and dive into the essential endpoints needed to: 

  • Get Block Data with the Blockchain API
  • Fetch Token Prices with the Token API 
  • Query Token Balances with the Wallet API

So, without further ado, let’s kick things off by showing you how to get block data using Moralis’ Blockchain API! 

Get Block Data with the Blockchain API 

The first central feature of any block explorer that we covered in the ”What is a Block Explorer?” section was the ability to view block data, including timestamps, transactions, gas limits, etc: 

view block data, including timestamps, transactions, gas limits, etc on Etherscan via API

The easiest way to get this information is to leverage Moralis’ Blockchain API and the getBlock() endpoint. All you have to do is call this endpoint with two parameters: chain and blockNumberOrHash

const response = await Moralis.EvmApi.block.getBlock({
  "chain": "0x1",
  "blockNumberOrHash": "18541416"
});

Calling the endpoint above returns the contents of the block in question, including a timestamp, hash, transactions, gas used, gas limit, and much more. This is what it might look like:

{
  "timestamp": "2023-11-10T12:02:11.000Z",
  "number": "18541416",
  "hash": "0xe9de65582d5a210f3f5703e0d7dc1daf9fbb5a0670937f5d92283a41ea1bcb2b",
  "parent_hash": "0xb63af7df0e2639cc25c323f22359b784408a117b534dea09cfec14263ea8d4fb",
  "nonce": "0x0000000000000000",
  "sha3_uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  "logs_bloom": "0x00200//...",
  "transactions_root": "0x0b6895fbd7fdf9b8e0239e5be21a195c89ffae093c1c2bcdea9ad498d720003b",
  "state_root": "0x787cc0fff225d2b6d8acef1cea33522293d134297c1f4b22819a90f157db183d",
  "receipts_root": "0x6d7caa30f12937a232dcedda5f8bc2c973ba917a67062cedeb4ccb52c729058c",
  "miner": "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5",
  "difficulty": "0",
  "total_difficulty": "58750003716598352816469",
  "size": "7792",
  "extra_data": "0x6265617665726275696c642e6f7267",
  "gas_limit": "30000000",
  "gas_used": "1372772",
  //...
}

If you want to know more about how this works, check out our Blockchain API tutorial

Fetch Token Prices with the Token API 

Another important feature of most block explorers is the ability to explore token data, including transfers, holders, etc. And in this section, we’ll show you how to get the price of any token: 

Etherscan Block Explorer API Example on get the price of any token

The most straightforward way to get this data is to leverage Moralis’ Token API. With only a single call to the getTokenPrice() endpoint, you can seamlessly get the price of any token, and all you have to do is specify two parameters: chain and address

const response = await Moralis.EvmApi.token.getTokenPrice({
  "chain": "0x1",
  "address": "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"
});

In return for calling the getTokenPrice() endpoint, you get the token’s price denominated in the chain’s native currency and USD. Here’s an example of what it might look like: 

{
  "tokenName": "Matic Token",
  "tokenSymbol": "MATIC",
  "tokenLogo": "https://cdn.moralis.io/eth/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0.png",
  "tokenDecimals": "18",
  "nativePrice": {
    "value": "423085770833740",
    "decimals": 18,
    "name": "Ether",
    "symbol": "ETH",
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
  },
  "usdPrice": 0.8771902804648514,
  "usdPriceFormatted": "0.877190280464851369",
  "exchangeName": "Uniswap v3",
  "exchangeAddress": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
  "tokenAddress": "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"
}

Query Token Balances with the Wallet API 

Lastly, a final core feature of most block explorers is the functionality to check data associated with a wallet address, including transfers, balances, etc. And in this section, we’ll show you how to fetch all token holdings of a wallet: 

data associated with a wallet address, including transfers, balances, etc., on Etherscan using Block Explorer API

The easiest way to get this information is to leverage Moralis’ Wallet API. All you need is a single call to the getWalletTokenBalances() endpoint while passing along two parameters: chain and address

const response = await Moralis.EvmApi.token.getWalletTokenBalances({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});

In return for calling the endpoint above, you get a list of all tokens held by the wallet in question. It will look something like this: 

[
  {
    "token_address": "0x3c978fc9a42c80a127863d786d8883614b01b3cd",
    "symbol": "USDT",
    "name": "USDTOKEN",
    "logo": null,
    "thumbnail": null,
    "decimals": 18,
    "balance": "10000000000000000000000",
    "possible_spam": true
  },
  {
    "token_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
    "symbol": "USDT",
    "name": "Tether USD",
    "logo": "https://cdn.moralis.io/eth/0xdac17f958d2ee523a2206206994597c13d831ec7.png",
    "thumbnail": "https://cdn.moralis.io/eth/0xdac17f958d2ee523a2206206994597c13d831ec7_thumb.png",
    "decimals": 6,
    "balance": "209355461",
    "possible_spam": false
  },
  //...
]

If you want to explore all other endpoints, check out our official Web3 API documentation page! 

What’s more, if you’re looking for a tutorial on how you can integrate this data into your dapps, check out the Moralis YouTube video below. This clip provides a comprehensive breakdown of how you can build an Etherscan clone in less than twelve minutes:

Block Explorer API Tutorial: How to Get the Token Balances of a Wallet in 3 Steps 

In this part of the article, we’ll present a complete tutorial on how you can call the various endpoints of Moralis. To highlight the accessibility of our premier interfaces, we’ll use the getWalletTokenBalances() endpoint as an example and show you how to get the token balances of a wallet in three steps:

  1. Set up Moralis
  2. Write a Script Calling the getWalletTokenBalances() Endpoint
  3. Run the Code

However, you must deal with a few prerequisites before we can continue! 

Prerequisites 

This will be a JavaScript tutorial. As such, you need to have the following ready: 

  • Node.js v14+
  • NPM/Yarn

Step 1: Set Up Moralis 

For the first step, you need to join Moralis to get your API key. As such, if you don’t already have an account, click on the ”Start for Free” button at the top right to sign up: 

Moralis Block Explorer API - Get Started for Free button

From there, go to the ”Settings” tab, scroll down, and copy your API key: 

Moralis Block Explorer API interface - showing how to copy API

Keep it for now, as you’ll need it in the next section! 

Step 2: Write a Script Calling the getWalletTokenBalances() Endpoint 

For the second step, set up a new project in your IDE, open a new terminal, and run the following command in the root folder to install the Moralis SDK: 

npm install moralis @moralisweb3/common-evm-utils

Next, create an ”index.js” file and add the following code: 

const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

const runApp = async () => {
  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });

  const address = "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d";

  const chain = EvmChain.ETHEREUM;

  const response = await Moralis.EvmApi.token.getWalletTokenBalances({
    address,
    chain,
  });

  console.log(response.toJSON());
};

runApp();

From there, add your Moralis API key by replacing YOUR_API_KEY

Block Explorer API key code parameter

Next, configure chain and address to fit your query: 

chain and address code parameters for the block explorer API feature

From here, we then pass along chain and address as parameters when calling the endpoint: 

Calling the block explorer API endpoint

And that’s it for the script! 

Step 3: Run the Code 

For the final step, open a new terminal and run the command below in the project’s root folder: 

node index.js

In return, you’ll get an array of all tokens held by the wallet in question. And it will look something like this:

[
  {
    "token_address": "0xefd6c64533602ac55ab64442307f6fe2c9307305",
    "name": "APE",
    "symbol": "APE",
    "logo": null,
    "thumbnail": null,
    "decimals": 18,
    "balance": "101715701444169451516503179"
  },
  {
    "token_address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    "name": "Wrapped Ether",
    "symbol": "WETH",
    "logo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.webp",
    "thumbnail": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.webp",
    "decimals": 18,
    "balance": "85000000000000000"
  }
]

Congratulations! You now know how to get the token balances of a wallet with Moralis! 

If questions remain, check out the official documentation page demonstrating how to get all ERC-20 tokens owned by an address. Also, you can follow the steps above to call any other Moralis endpoint. All you have to do is replace the endpoint and configure the parameters during the second step!  

Summary: Exploring the Industry’s Premier Block Explorer API

In today’s article, we introduced you to Moralis’ industry-leading suite of Web3 APIs. With our premier interfaces, you get everything you need to build a block explorer or integrate similar functionality into your projects, making it the ultimate Etherescan API alternative. And thanks to the accessibility of our APIs, you can get the data you need with single lines of code! 

In addition to introducing you to Moralis, we also showed you how to get the token balances of a wallet in three steps: 

  1. Set up Moralis
  2. Write a Script Calling the getWalletTokenBalances() Endpoint
  3. Run the Code

Consequently, if you have followed along this far, you now know how to get blockchain data using Moralis! 

If you liked this block explorer API tutorial, consider checking out more content on the Web3 blog. For instance, check out our Ropsten faucet guide or learn about the best blockchain API providers

Also, if you want to leverage our industry-leading Web3 APIs, don’t forget to sign up with Moralis today! 

Market Data API
Build amazing trading and portfolio dapps with this Market Data API. Keep users engaged with up-to-date data!
Market Data API
Related Articles
September 29, 2022

How to Connect PlayFab with Web3 Using Azure Functions

October 16, 2023

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

November 24, 2023

Moralis Dapp Store – Exploring the Web3 Wiki

November 20, 2023

What’s the Difference Between Polygon PoS vs Polygon zkEVM?

November 27, 2022

Blockchain Infrastructure Companies – How to Choose the Right Provider

January 1, 2024

Full Web3 Wallets List – Exploring the Best Web3 Wallets

December 1, 2022

Python and Web3 – A Web3 and Python Tutorial for Blockchain Development

October 13, 2022

Cronos Boilerplate – How to Create Cronos Dapps

March 18, 2024

SOC 2 in Web3 – The First SOC 2 Type 2 Certified Web3 Infra Provider