Skip to main content

Introduction

In this tutorial, you’ll learn how to retrieve all ERC20 token balances for a wallet address using the Moralis API. This is essential for building portfolio trackers, wallet dashboards, or any application that needs to display a user’s token holdings. We’ll use the following Moralis API endpoint:

Prerequisites

Step 1: Set Up Your Project

Create a new directory for your project and initialize it:
mkdir get-token-balances && cd get-token-balances
npm init -y

Step 2: Create the Script

Create a file called index.js and add the following code:
// index.js
const API_KEY = 'YOUR_API_KEY';
const WALLET_ADDRESS = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; // Vitalik's wallet

async function getTokenBalances() {
  const response = await fetch(
    `https://deep-index.moralis.io/api/v2.2/wallets/${WALLET_ADDRESS}/tokens?chain=eth`,
    {
      headers: {
        'X-API-Key': API_KEY,
      },
    }
  );

  const data = await response.json();
  console.log(JSON.stringify(data, null, 2));
}

getTokenBalances();
Replace YOUR_API_KEY with your actual Moralis API key.

Step 3: Run the Script

Execute the script to fetch the token balances:
node index.js

Example Response

{
  "result": [
    {
      "token_address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
      "symbol": "USDC",
      "name": "USD Coin",
      "logo": "https://cdn.moralis.io/eth/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png",
      "thumbnail": "https://cdn.moralis.io/eth/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48_thumb.png",
      "decimals": 6,
      "balance": "1000000000",
      "possible_spam": false,
      "verified_contract": true,
      "balance_formatted": "1000",
      "usd_price": 0.999,
      "usd_price_24hr_percent_change": 0.01,
      "usd_value": 999.0,
      "portfolio_percentage": 25.5
    }
  ]
}

Understanding the Response

FieldDescription
token_addressThe contract address of the ERC20 token
symbolThe token’s ticker symbol (e.g., USDC)
nameThe full name of the token
decimalsNumber of decimal places the token uses
balanceRaw token balance (in smallest unit)
balance_formattedHuman-readable balance with decimals applied
usd_priceCurrent USD price of the token
usd_valueTotal USD value of the token balance
possible_spamWhether the token is flagged as potential spam
verified_contractWhether the token contract is verified
portfolio_percentagePercentage this token represents in the wallet

Next Steps