17

Is there a way with the Etherscan API you can get all the erc20 token balances for a specific address?

As explained here, you can see token balances with their web UI (the token tracker dropdown). Does their API provide equivalent functionality?

As far as I can tell, you can only query for a specific token of which you already have the address. I need functionality equivalent to Ethplorer's API endpoint getAddressInfo/{address}.

4

5 Answers 5

11

As previously mentioned it doesn't appear possible using etherscan. But it's possible using the (free) ethexplorer.io API.

The format is:

https://api.ethplorer.io/getAddressInfo/[YOUR_ADDRESS]?apiKey=freekey

For example:

https://api.ethplorer.io/getAddressInfo/0x32Be343B94f860124dC4fEe278FDCBD38C102D88?apiKey=freekey

(Note: Must be HTTPS, HTTP will return a 404 error with link to docs, without specifying exactly what's wrong.)

API documentation is here:

https://github.com/EverexIO/Ethplorer/wiki/Ethplorer-API

Website:

https://ethplorer.io

1
  • ethplorer has large delay.. unfortunatelly
    – l00k
    Aug 1, 2020 at 2:41
4

This is not possible with their current API.

Depending on your project, a workaround could be web scraping. Here’s an example using Lua and xpath:

Get all contract addresses for which the given ETH address holds tokens:

function requestContractAddressesForEthAddress(ethAddress)

  -- No API method for this (as of Mar 11, 2018), therefore using web scraping

  local connection = Connection()
  local html = HTML(connection:get("https://etherscan.io/address/" .. ethAddress))
  local elements = html:xpath("//ul[@id='balancelist']/li/a")
  local addresses = {}

  elements:each(function (index, element)
    local href = element:attr('href')
    local address = string.match(href, "^%/token%/(0x[0-9a-fA-F]+)")
    table.insert(addresses, address)
  end)

  return addresses
end

Get info on a token (name, USD price, divisor to calculate correct amount) for a given contract address:

function requestTokenInfo(contractAddress)

  -- No API method for this (as of Mar 11, 2018), therefore using web scraping

  local connection = Connection()
  local html = HTML(connection:get("https://etherscan.io/token/" .. contractAddress))
  local name = html:xpath("//*[@id='address']"):text()
  local summary = html:xpath("//*[@id='ContentPlaceHolder1_divSummary']"):text()
  local decimals = tonumber(string.match(summary, "Token Decimals:%s+([%d,]+)"))

  return {
    name = name,
    price = tonumber(string.match(summary, "Value per Token:%s+$([%d%.,]+)")),
    divisor = math.pow(10, decimals)
  }
end

Please be aware that web scraping is considered bad practice. This can break without notice, as it is obviously relying on how the frontend is rendered.

I’ve contacted Etherscan.io support on Mar 12, 2018 to find out if those API calls are on their roadmap. The answer was that providing paid custom APIs services is not their short-term focus.

2

You can do this with any node, no external API needed, with the help of a smart contract. I just recently wrote a smart contract and library for this, and put it into an NPM package. You can check it out here: https://www.npmjs.com/package/eth-balance-checker

The call looks like this:

import Web3 from 'web3';
import { getAddressBalances } from 'eth-balance-checker/lib/web3';

const web3 = new Web3(...);
const address = '0x123...';
const tokens = ['0x0', '0x456...'];
getAddressBalances(web3, address, tokens).then(balances => {
  console.log(balances); // { "0x0": "100", "0x456...": "200" }
});
0

I used the defilama free api to get all the token addresses. Then I looped over them with the etherscan free api.

0

ethexplorer.io is now defunct. Current options I could find for getting all token balances include Covalent and Etherscan

There were others providing this functionality and they keep disappearing. Others are launching it now, like Footprint Analytics (currently only NFTs, moving to all ERC20 as well, from direct communication)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.