🚀 Investors Wanted! 📈

Join us in our journey to innovate blockchain technologies. Let's shape the future together.

Learn More Contact Us
logo

Tutorials

General

PHP

Python

NodeJs

Tutorial to get ERC20 or Ethereum balance of an address

To query the balance of ERC20 tokens, you need a valid Ethereum address. With this tutorial you can easily query the balance of the respective Ethereum address. We have written a detailed tutorial for this purpose. Read more.

Prerequisites

  1. Basic knowledge of Nodejs
  2. Account at Chaingateway.io
  3. A local or remote machine with NodeJs and installed axios module

The Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    
    /*
    * Replace with your Values
    */
    var apikey = "YOURAPIKEY"
    var ethereumaddress = "ETHEREUMADDRESS"; // Ethereum address you want to get the balance of
    var contractaddress = "CONTRACTADDRESS"; // Smart contract address of the Token

   var axios = require('axios');

    var config = {
        method: 'get',
        maxBodyLength: Infinity,
        url: 'https://api.chaingateway.io/v2/ethereum/balances/'+ethereumaddress+'/erc20/'+contractaddress,
        headers: { 
            'Accept': 'application/json',
            'Authorization': apikey
        }
    };

        axios(config)
        .then(function (response) {
        console.log(JSON.stringify(response.data));
        })
        .catch(function (error) {
        console.log(error);
        });

Code Modifications

  1. Replace “YOURAPIKEY” in line 5 of above code with the API Key in your Chaingateway.io Account Panel.
  2. Replace “ETHEREUMADDRESS” in line 6 of above code with the Ethereum address you want to get the balance of.
  3. Replace “CONTRACTADDRESS” in line 7 of above code with the smart contract address of the Token

Final Steps

Upload the code above to your local or remote machine

Call the script by navigating to your favorite browser and entering the URL

Congratulations! You should now see a new generated address which you can use to send and receive ERC-20 Tokens.