
Getting ERC20 token balances with Web3.js
Sometimes you might want to get the ERC20 token balance of a specific address on the Ethereum network and do something with this data programmatically. Fortunately, this is very straightforward as ERC20 tokens are a standard and all of them need to implement certain methods to adhere to this standard. The method we’re going to focus on in this post is the balanceOf method and we’re going to be using the web3.js library together with NodeJS.
Token address
The first thing you need is the address of the token you are interacting with. In this post I’m going to interact with the Uniswap ERC20 token and its address on the Ethereum network is 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.
ABI
Next, you need the ABI of the token contract that you are going to interact with. You can easily find the ABI of any contract on etherscan.io or some other block explorer. On etherscan, after searching for the token address you will find the ABI under the “Contract” tab right here:

Writing the code
Now that we have both the token contract address and the ABI of the token we can finally start using the web3.js library and create a new contract instance. The final code below will work for getting the ERC20 token balance of any token & address you’d like. The only thing you need to change for other tokens is the ABI & the tokenAddress.
const Web3 = require("web3");
const web3 = new Web3("YOUR_ETH_NODE_ENDPOINT");
let abi = "PASTE_ABI_HERE";
let tokenAddress = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
const UniswapToken = new web3.eth.Contract(abi, tokenAddress);
(async () => {
let balance = await UniswapToken.methods.balanceOf("THE_ADDRESS_YOU_WANT_THE_BALANCE_OF_HERE").call();
console.log(web3.utils.fromWei(balance, 'ether');
})();
In the code above we receive the token balance in the balance variable after awaiting response from our call. Once we’ve received the balance we use built in utilities in web3 to convert the output to a more human friendly interpretation of the balance and log it to the console.
Other networks
The above will work just as well on any other EVM compatible blockchain, for example BSC or Polygon.