Overview - Tools you need to work with Chainbase
To get an account's token balances, you can use the Web3 APIs provided by Chainbase. These APIs allow you to access transaction data and account balances easily. By following the steps outlined below, you can obtain the native token balances of an account on a specific chain with a single API call using the Chainbase API getAccountBalance.
Table of Contents
- Introduction
- Set up a free account at Chainbase
- Write a script using Chainbase API
- Print native token balances
- Conclusion
- FAQs
1. Introduction
Getting the token balances of an account is an essential task when dealing with cryptocurrencies, as it provides valuable information about the amount of tokens held by that account. In this article, we will guide you through the process of retrieving native token balances owned by a specific address using the Chainbase API.
2. Set up a free account at Chainbase
Before we can start obtain token balances, you need to create a free account at Chainbase. Follow the steps below to set up your account:
- Visit the Chainbase website and sign up for a free account.
- Once you're logged in, navigate to the dashboard to get an overview of your account.
- Create a new project within the Chainbase console.
- Obtain your API key, which will be used to authenticate your API calls.
3. Write a script using Chainbase API
To retrieve the native token balances of an address, you can use the Chainbase API in your script. Below, we provide examples using both the fetch and axios libraries in JavaScript.
Using fetch in JavaScript:
network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; // Take Vitalik's wallet address as an example.
fetch(`https://api.chainbase.online/v1/account/balance?chain_id=${network_id}&address=${wallet_addr}`, {
method: 'GET',
headers: {
'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
'accept': 'application/json'
}
}).then(response => response.json())
.then(data => console.log(data.data))
.catch(error => console.error(error));
Using axios in JavaScript:
You need to install axios using npm install axios --save
 in the terminal first.
network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; // Take Vitalik's wallet address as an example.
const axios = require('axios');
const options = {
url: `https://api.chainbase.online/v1/account/balance?chain_id=${network_id}&address=${wallet_addr}`,
method: 'GET',
headers: {
'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
'accept': 'application/json'
}
};
axios(options)
.then(response => console.log(response.data.data))
.catch(error => console.log(error));
Ensure you replace CHAINBASE_API_KEY
with your actual API key obtained from Chainbase.
4. Print native token balances
After setting up your script with the Chainbase API call,
you can now proceed to print the native token balances. By executing the script, you will retrieve the balances of the native tokens owned by the specified address. Follow the steps below:
- Save the script in a file with a .js extension (e.g., getBalances.js).
- Open your terminal and navigate to the directory where the script file is located.
- Run the following command to execute the script:
After executing the command, the script will make the API call to Chainbase and retrieve the native token balances for the specified address.
The balances will be printed in the console or terminal window, allowing you to view the token balances associated with the address.
To get data printed, run command node <filename>.js
 in the terminal. In this case, the native token balances of Vitalik’s wallet on Ethereum is 0x122b7e019d9a0431a15
 by the time of Mar. 17th, 2023.
5. Conclusion
Retrieving native token balances owned by an address is made simple with the Chainbase API. By following the steps outlined in this article, you can set up a free account, write a script using the Chainbase API, and retrieve the token balances of a specific address. This information is valuable when managing and analyzing cryptocurrency holdings.
Start leveraging the power of Chainbase and its API to gain insights into token balances and enhance your cryptocurrency-related tasks.
6. FAQs
Q1: Can I use Chainbase API with other programming languages besides JavaScript?
Yes, Chainbase provides an API that can be used with various programming languages, not just JavaScript. You can explore the Chainbase documentation for more examples and libraries in your preferred programming language.
Q2: Is the Chainbase API free to use?
Yes, Chainbase offers a free account that provides access to our API. However, they may have limitations or offer additional paid plans for more extensive usage. Refer to Chainbase's pricing and documentation for more details.
Q3: Can I get token balances for addresses on different blockchain networks?
Yes, by specifying the appropriate network ID in the API call, you can retrieve token balances for addresses on different supported blockchain networks. Refer to the Chainbase documentation for a list of supported chains and our respective network IDs.
Q4: How often are the token balances updated on Chainbase?
The token balances provided by the Chainbase API are based on the latest available data on the blockchain network. Therefore, the balances should reflect the most recent transactions and changes in the account's holdings.
Q5: Can I obtain token balances for multiple addresses at once using the Chainbase API?
Yes, you can make multiple API calls to retrieve token balances for different addresses. Simply modify the script to include the desired addresses and iterate over them to fetch the balances individually.
About Chainbase
Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.
Want to learn more about Chainbase?
Visit our website chainbase.com Sign up for a free account, and Check out our documentation.
Website|Blog|Twitter|Discord|Link3