arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

getAccountBalance

hashtag
How to call the API from an app

Hedera Wallet Snap connects to your currently connected Metamask account by default. To learn how apps can connect to Hedera Wallet Snap using a non-metamask(external) account, refer to this documentation.

Then, depending on whether you're trying to connect to a metamask account or a non-metamask account, you can call the snap API in the following way:

circle-info

If you don't pass in "accountId", it will retrieve account info for the currently connected account.

Note that you can also call this API to retrieve account balance for another account Id which you do not own. Think of this as fetching the account balance of an arbitrary hedera account Id. To do that, you would just pass in accountId like this:

This would retrieve the account balance of the account 0.0.1 from the Hedera Network Nodes.

hashtag
What the API does

  1. Retrieves the currently connected account the user has selected on Metamask. If it's the first time, a new is created and the account info is saved in snap state.

  2. Calls the to get account balance.

  3. Returns the result.

Some example responses:

For a hedera account id 0.0.4559:

hashtag
Live Demo on CodePen

circle-info
  • Note: Visit to activate your hedera account before you interact with the demo

  • If you're getting any errors with the live demo, make sure you go through the section to learn about what you may be missing. You need to install in your browser for the live demo to work.

const snapId = `npm:@hashgraph/hedera-wallet-snap`

const getAccountInfoAPI = async () => {
  const externalAccountParams = {
    externalAccount: {
      accountIdOrEvmAddress: '0.0.12345',
      curve: 'ED25519'
    }
  }

  await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
      snapId,
      request: {
        method: 'getAccountBalance',
        params: {
          network: 'testnet',
          mirrorNodeUrl: 'https://testnet.mirrornode.hedera.com'
          // Pass 'accountId' is useful if you want to retrieve account balance
          // for someone else rather than yourself
          accountId: '0.0.67890', 
          /* 
            Uncomment the below line if you want to connect 
            to a non-metamask account
          */
          // ...externalAccountParams
        }
      }
    }
  })
}

To ease the integration of Hedera Wallet Snap on an application, we have created a template web application that you can run locally and check out the code in its entirety to learn how you can integrate and interact with various APIs exposed by Hedera Wallet Snap. Check out the full source code at .

  • You can also check out the to learn how each API works.

  • snap account
    Get Account Balance APIarrow-up-right
    https://pulse.tuum.tech/arrow-up-right
    FAQs
    Metamaskarrow-up-right
    const snapId = `npm:@hashgraph/hedera-wallet-snap`
    
      await window.ethereum.request({
        method: 'wallet_invokeSnap',
        params: {
          snapId,
          request: {
            method: 'getAccountBalance',
            params: {
              network: 'testnet',
              mirrorNodeUrl: 'https://testnet.mirrornode.hedera.com'
              accountId: '0.0.1',
            }
          }
        }
      })
    }
    {
      "currentAccount": {
        "hederaAccountId": "0.0.4559",
        "hederaEvmAddress": "0x3ba201df50314e4702d4d92b52d304ee63bfca23",
        "balance": {
          "hbars": 89.60420503,
          "timestamp": "2024-02-01T21:35:21.826Z",
          "tokens": {}
        },
        "network": "testnet"
      },
      "accountBalance": 89.60420503
    }
    template application github repositoryarrow-up-right
    API reference