Introduction
Cryptocurrency enthusiasts and developers often need to track ERC20 token balances associated with a specific wallet address. Using Chainbase’s API, you can automate this process with the getAccountTokens endpoint. This guide covers everything from account setup to scripting and output interpretation—ensuring you retrieve token balances efficiently.
Tools Required
Before starting, gather these essentials:
- Chainbase Account: A free account with an active API key.
- IDE: Visual Studio Code (VS Code) recommended for JavaScript.
- Wallet Address: The Ethereum address you want to query (e.g.,
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045).
Step 1: Set Up a Chainbase Account
- Register: Visit Chainbase and sign up for a free account.
- Create a Project: Navigate to the dashboard and create a new project to generate your API key.
- Secure API Key: Note this key for authenticating API requests.
👉 Get started with Chainbase today!
Step 2: Write a Script Using Chainbase API
Below are two JavaScript examples using fetch and axios:
Option A: Using Fetch
const network_id = '1'; // Mainnet Ethereum
const wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const API_KEY = 'YOUR_CHAINBASE_API_KEY';
fetch(`https://api.chainbase.online/v1/account/tokens?chain_id=${network_id}&address=${wallet_addr}&limit=5&page=1`, {
method: 'GET',
headers: {
'x-api-key': API_KEY,
'accept': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(data.data))
.catch(error => console.error(error));Option B: Using Axios
First, install Axios:
npm install axios --saveThen, execute:
const axios = require('axios');
const network_id = '1';
const wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const API_KEY = 'YOUR_CHAINBASE_API_KEY';
axios.get(`https://api.chainbase.online/v1/account/tokens?chain_id=${network_id}&address=${wallet_addr}&limit=5&page=1`, {
headers: {
'x-api-key': API_KEY,
'accept': 'application/json'
}
})
.then(response => console.log(response.data.data))
.catch(error => console.log(error));Step 3: Interpret Token Balances
The API returns an array of objects, each representing a token:
{
"balance": "0x2386f26fc10000",
"contract_address": "0x954b7997b8bfa9b3d642c477549e284551012f05",
"decimals": 9,
"name": "Eterium",
"symbol": "ETE"
}Key Fields:
balance: Token amount in hexadecimal.contract_address: Token’s smart contract.decimals: Precision for display.symbol: Token ticker (e.g.,ETE).
👉 Explore more API integrations here!
FAQs
Q1: Can I query tokens across blockchains?
Yes! Specify the chain_id (e.g., 1 for Ethereum).
Q2: How do I convert hex balances to readable numbers?
Use libraries like ethers.js or manual conversion based on decimals.
Q3: Is there a rate limit?
Free-tier accounts have limits; check Chainbase’s docs for details.
Q4: What if my query returns no tokens?
Ensure the address holds ERC20 tokens and the chain_id is correct.
Conclusion
Chainbase’s API simplifies ERC20 token tracking, enabling seamless integration into wallets or analytics tools. By following this guide, you can query balances programmatically and focus on building powerful Web3 applications.
Final Tip: Always store API keys securely and monitor usage to avoid disruptions.
For advanced features, explore Chainbase’s documentation.