You can retrieve the balance of an Ethereum address using libraries like web3.js or ethers.js.
Check the Balance of a Ethereum Wallet Address Using web3.js
First, ensure you have the web3 library installed:
1 | npm install web3 |
npm install web3
Here’s the code. The following is based on Infura which has a free plan API, rate limit is 10 requests per second.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const Web3 = require('web3'); const INFURA_ENDPOINT = 'YOUR_INFURA_ENDPOINT'; const web3 = new Web3(INFURA_ENDPOINT); const getBalance = async (address) => { const balanceWei = await web3.eth.getBalance(address); const balanceEth = web3.utils.fromWei(balanceWei, 'ether'); return balanceEth; }; const address = '0x...'; // Replace with your address getBalance(address).then(balance => { console.log(`Balance of ${address}: ${balance} ETH`); }); |
const Web3 = require('web3'); const INFURA_ENDPOINT = 'YOUR_INFURA_ENDPOINT'; const web3 = new Web3(INFURA_ENDPOINT); const getBalance = async (address) => { const balanceWei = await web3.eth.getBalance(address); const balanceEth = web3.utils.fromWei(balanceWei, 'ether'); return balanceEth; }; const address = '0x...'; // Replace with your address getBalance(address).then(balance => { console.log(`Balance of ${address}: ${balance} ETH`); });
Check the Balance of a Ethereum Wallet Address Using ethers.js
Install the required library:
1 | npm install ethers |
npm install ethers
Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const { providers } = require('ethers'); const INFURA_PROJECT_ID = 'YOUR_INFURA_PROJECT_ID'; // Replace with your Infura Project ID const provider = new providers.InfuraProvider('mainnet', INFURA_PROJECT_ID); const getBalance = async (address) => { const balanceWei = await provider.getBalance(address); const balanceEth = ethers.utils.formatEther(balanceWei); return balanceEth; }; const address = '0x...'; // Replace with your address getBalance(address).then(balance => { console.log(`Balance of ${address}: ${balance} ETH`); }); |
const { providers } = require('ethers'); const INFURA_PROJECT_ID = 'YOUR_INFURA_PROJECT_ID'; // Replace with your Infura Project ID const provider = new providers.InfuraProvider('mainnet', INFURA_PROJECT_ID); const getBalance = async (address) => { const balanceWei = await provider.getBalance(address); const balanceEth = ethers.utils.formatEther(balanceWei); return balanceEth; }; const address = '0x...'; // Replace with your address getBalance(address).then(balance => { console.log(`Balance of ${address}: ${balance} ETH`); });
Replace ‘YOUR_INFURA_ENDPOINT’ or ‘YOUR_INFURA_PROJECT_ID’ with your actual Infura endpoint or project ID. If you’re using another service or running a local Ethereum node, adjust the provider setup accordingly.
Check the Balance of a Ethereum Wallet Address Using Alchemy API
With Alchemy API, we can also easily query the RestFul API to query the balance of any given ETH Wallet Address, see below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | const getBalanceETH_Alchemy = async (address) => { const ALCHEMY_API_KEY = "REPLACE_WITH_API_KEY"; const MAIN_ENDPOINT_ALCHEMY = `https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`; try { const data = { id: 1, jsonrpc: "2.0", method: "eth_getBalance", params: [ address, "latest" ] }; // Making POST request to fetch gas price const resp = await axios.post(MAIN_ENDPOINT_ALCHEMY, data, { headers: { 'accept': 'application/json', 'content-type': 'application/json' } }); if (resp && resp.data && resp.data.result) { return parseInt(resp.data.result, 16)/1e18; } return null; } catch (error) { console.error(`Error fetching data: ${error}`); return null; } } |
const getBalanceETH_Alchemy = async (address) => { const ALCHEMY_API_KEY = "REPLACE_WITH_API_KEY"; const MAIN_ENDPOINT_ALCHEMY = `https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`; try { const data = { id: 1, jsonrpc: "2.0", method: "eth_getBalance", params: [ address, "latest" ] }; // Making POST request to fetch gas price const resp = await axios.post(MAIN_ENDPOINT_ALCHEMY, data, { headers: { 'accept': 'application/json', 'content-type': 'application/json' } }); if (resp && resp.data && resp.data.result) { return parseInt(resp.data.result, 16)/1e18; } return null; } catch (error) { console.error(`Error fetching data: ${error}`); return null; } }
Check the Balance of a Ethereum Wallet Address Using Etherscan API
The Etherscan provides Free API to query the balance. The Free Plan has a rate limit of 5 Requests per second. See Using Etherscan API to Query the Balance of a ETH Wallet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | const getBalanceETH_Etherscan = async (address) => { const ETHERSCAN_API_KEY = "REPLACE_WITH_KEY"; const api = `https://api.etherscan.io/api?module=account&action=balance&apikey=${ETHERSCAN_API_KEY}&address=${address}&tag=latest`; try { const response = await fetch(api, { method: 'GET', headers: { 'Content-Type': 'application/json', } }); const data = await response.json(); //console.log(data); if (data.status == "1" && data.message == "OK" && data.result) { return (data.result)/(1000000000000000000); } return null; } catch (error) { console.error('Error fetching balance:', error); return null; } } |
const getBalanceETH_Etherscan = async (address) => { const ETHERSCAN_API_KEY = "REPLACE_WITH_KEY"; const api = `https://api.etherscan.io/api?module=account&action=balance&apikey=${ETHERSCAN_API_KEY}&address=${address}&tag=latest`; try { const response = await fetch(api, { method: 'GET', headers: { 'Content-Type': 'application/json', } }); const data = await response.json(); //console.log(data); if (data.status == "1" && data.message == "OK" && data.result) { return (data.result)/(1000000000000000000); } return null; } catch (error) { console.error('Error fetching balance:', error); return null; } }
Ethereum Blockchain
- How to Fix "Returned Error: replacement transaction underpriced" when Sending the ETH on Ethereum Blockchain?
- How to Transfer or Send ETH to Another Wallet on Ethereum Blockchain?
- How to Check the Balance of a Ethereum Wallet Address?
- How to Check if a Given String is a Valid Ethereum Wallet Address?
- How to Get the Gas Price and Gas Fee on Ethereum Blockchain?
- How to Get the Latest Block Number (Head Block) on Ethereum Blockchain?
- Today.. Gas Fee was High
- Gas Fee is Fixed Regardless the Amount of ETH to Send
- The Simple Way to Watch the Transactions of an ETH Account
- Watch Gas Price Before You Send Steem to Swap ETH
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Check if a Given String is a Valid Ethereum Wallet Address?
Next Post: Teaching Kids Programming - Compute the Amount of Water of a Glass in a Pyramid Stacked Glasses (Top Down Dynamic Programming Algorithm - Recursion with Memoization)