'Is there a way to read the balance of any public address on Ethereum mainnet using Ethers?

I am trying to build a very simple tool as a beginner project. A user inputs a wallet address, and my site shows the ETH balance of that address.

I tried this, which worked with wallets that I had connected to the site, but not with ones that weren't. I'm assuming because provider has something to do only with connected wallets.

  async function getBalance(wallet) {
    let balance = await provider.getBalance(wallet);
    // we use the code below to convert the balance from wei to eth
    balance = ethers.utils.formatEther(balance);
    console.log(balance);
  }

Does metamask/ethers have a built in way to read on-chain data or do I need to go through something like the Etherscan API? I'm assuming there is something built in because I'm able to get my own balance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source