how to get binance Testnet api balance using python

139 views Asked by At

hi im trying to get the balance of the Binance Testnet api but can't seem to figure out how. I am making an arbitrage bot on python and need it to show the balance but want it to show the balance on the Testnet api and not the actual Binance API

Tried searching but couldn't find a way to do it

import requests
from requests.auth import HTTPBasicAuth

# Replace with your Binance Testnet API key and secret
api_key = 'your_api_key'
api_secret = 'your_api_secret'

# Binance Testnet API URL
base_url = 'https://testnet.binance.vision/api/v3'

# Endpoint for getting account information
account_info_endpoint = '/account'

# Set up the authentication
auth = HTTPBasicAuth(api_key, api_secret)

def get_account_balance():
    # Construct the request URL
    url = base_url + account_info_endpoint

    try:
        # Make the API request
        response = requests.get(url, auth=auth)
        response.raise_for_status()  # Check for errors in the response

        # Parse the JSON response
        account_info = response.json()

        # Extract and print the balances
        for balance in account_info['balances']:
            asset = balance['asset']
            free_balance = float(balance['free'])
            locked_balance = float(balance['locked'])
            
            print(f'{asset}: Free: {free_balance}, Locked: {locked_balance}')

    except requests.exceptions.RequestException as e:
        print(f'Error making API request: {e}')

if __name__ == "__main__":
    get_account_balance()
0

There are 0 answers