I use the code that the Tron api generates and get a 200 response code ('ok'), but I don't get any information about the balances.
Code
import requests
#wallet = 'TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD'
url = "https://api.trongrid.io/v1/contracts/TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD/tokens"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
print(response.text)
Response
{
"data": [],
"meta": {
"at": 1676483294906,
"page_size": 0
},
"success": true
}
There seems to be a problem with the API endpoint you used: https://api.trongrid.io/v1/contracts/TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD/tokens
Here, I used a different API endpoint and it fixed the problem: https://apilist.tronscan.org/api/account?address=TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD&includeToken=true.
import requests
contract_address = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' # USDT TRC20 contract address
wallet_address = 'TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD' # wallet address
url = f"https://apilist.tronscan.org/api/account?address={wallet_address}&includeToken=true"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
data = response.json()
if 'error' in data:
print(f"Error: {data['error']}")
else:
usdt_balance = None
for token in data['trc20token_balances']:
if token['tokenName'] == 'Tether USD':
usdt_balance = round(float(token['balance'])*pow(10,-token['tokenDecimal']),6)
break
if usdt_balance is not None:
print(f'USDT TRC20 balance in {wallet_address}: {usdt_balance}')
else:
print(f'USDT TRC20 token not found in {wallet_address}')
Here is the result: USDT TRC20 token balance