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
.