DeveloperBreeze

Transaction Details Development Tutorials, Guides & Insights

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

Understanding and Using the Etherscan API to Query Blockchain Data

Tutorial October 24, 2024

Let’s create a Node.js script to query the balance of an Ethereum wallet using the Etherscan API.

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();