Skip to main content

Introduction

In this tutorial, you’ll learn how to retrieve native token balances (like ETH, MATIC, or BNB) for any wallet address using the Moralis API. This works for both externally owned accounts (EOAs) and smart contract wallets like Safe or Argent. 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-native-balance && cd get-native-balance
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 getNativeBalance() {
  const response = await fetch(
    `https://deep-index.moralis.io/api/v2.2/${WALLET_ADDRESS}/balance?chain=eth`,
    {
      headers: {
        'X-API-Key': API_KEY,
      },
    }
  );

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

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

Step 3: Run the Script

Execute the script to fetch the native balance:
node index.js

Example Response

{
  "balance": "2847583920000000000000"
}

Understanding the Response

FieldDescription
balanceThe native token balance in wei (smallest unit)
To convert to a human-readable format, divide by 10^18:
const balanceInEth = Number(data.balance) / 1e18;
console.log(`Balance: ${balanceInEth} ETH`);

Next Steps