In this tutorial, weโll walk through how to use Python to retrieve and log the portfolio balance of a cryptocurrency wallet across various EVM chains. Weโll be using the requests
library to make HTTP requests.
Before you begin, ensure you have the following:
requests
library installed. If you donโt have it, you can install it using pip: pip install requests
Create a new Python file, for example, check_portfolio.py
, and open it in your text editor.
At the beginning of your check_portfolio.py
file, import the requests
library:
import requests
Define the API request details with your RapidAPI key and the wallet address:
url = "https://debank-unofficial-wallet-portfolio-balance-api.p.rapidapi.com/user/total_balance"
headers = {
'X-RapidAPI-Key': 'YourRapidAPIKey', # Replace with your API key
'X-RapidAPI-Host': 'debank-unofficial-wallet-portfolio-balance-api.p.rapidapi.com'
}
params = {'address': '0xYourWalletAddress'} # Replace with the wallet address
Create a function to send the request and process the response:
def get_portfolio_balance():
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
print('Portfolio Balance:', response.json())
else:
print('Error fetching portfolio balance:', response.status_code)
get_portfolio_balance()
Execute your script using Python:
python check_portfolio.py
You should see the portfolio balance of the specified wallet address in the console, including individual balances for each chain and the total USD worth.
Congratulations! Youโve successfully created a Python script to retrieve and log the portfolio balance of a cryptocurrency wallet. This script can be further customized or integrated into larger projects for enhanced crypto asset management.