DeveloperBreeze

Query Wallet Balance Development Tutorials, Guides & Insights

Unlock 1+ expert-curated query wallet balance tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your query wallet balance skills on DeveloperBreeze.

Understanding and Using the Etherscan API to Query Blockchain Data

Tutorial October 24, 2024

const axios = require('axios');

// Replace this with your actual Etherscan API key
const apiKey = 'YOUR_ETHERSCAN_API_KEY';

// Replace this with the Ethereum address you want to query
const address = '0xYourEthereumAddress';

// Etherscan API URL to fetch wallet balance
const url = `https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest&apikey=${apiKey}`;

async function getWalletBalance() {
  try {
    // Make the API request to Etherscan
    const response = await axios.get(url);
    const balanceInWei = response.data.result;

    // Convert balance from Wei to Ether
    const balanceInEther = balanceInWei / 1e18;
    console.log(`Wallet Address: ${address}`);
    console.log(`Wallet Balance: ${balanceInEther} ETH`);
  } catch (error) {
    console.error('Error fetching balance:', error);
  }
}

// Call the function to get the balance
getWalletBalance();
  • Axios is used to make the HTTP request to the Etherscan API.
  • Replace 'YOUR_ETHERSCAN_API_KEY' with the actual API key you obtained from Etherscan.
  • Replace '0xYourEthereumAddress' with the Ethereum address you want to query.
  • The balance is returned in Wei (the smallest unit of Ether). We convert this to Ether for readability by dividing by 1e18.