This tutorial will guide you through the process of using a Node.js script to retrieve and log the portfolio balance of a cryptocurrency wallet across various EVM chains using the Axios HTTP client.
Before you start, make sure you have the following:
npm install axios
Create a new JavaScript file, e.g., checkPortfolio.js
, and open it in your code editor.
At the top of your checkPortfolio.js
file, import the Axios library:
const axios = require('axios');
Set up the API request options using your RapidAPI key and the desired wallet address:
const options = {
method: 'GET',
url: 'https://debank-unofficial-wallet-portfolio-balance-api.p.rapidapi.com/user/total_balance',
params: { address: '0xYourWalletAddress' }, // Replace with the wallet address
headers: {
'X-RapidAPI-Key': 'YourRapidAPIKey', // Replace with your API key
'X-RapidAPI-Host': 'debank-unofficial-wallet-portfolio-balance-api.p.rapidapi.com'
}
};
Replace 0xYourWalletAddress
with the actual wallet address and YourRapidAPIKey
with your RapidAPI key.
Using an async function, send the request and process the response:
async function getPortfolioBalance() {
try {
const response = await axios.request(options);
console.log('Portfolio Balance:', response.data);
} catch (error) {
console.error('Error fetching portfolio balance:', error);
}
}
getPortfolioBalance();
This function sends a request to the API and logs the response to the console.
Run your script using Node.js:
node checkPortfolio.js
You should see the portfolio balance of the specified wallet address logged to the console, including individual balances for each chain and the total USD worth.
You’ve successfully set up a Node.js script to retrieve and log the portfolio balance of a cryptocurrency wallet using Axios. This script can be expanded or integrated into larger projects to monitor wallet balances across various EVM chains.