Hi Tam, I Need your help fetching my wallet balance using Python API.
pls refer to the following documentation for fetching wallet balance. Delta Exchange Api
endpoint to call : https://api.india.delta.exchange/v2/wallet/balances
Let me know if there is anything specific you want to ask regarding this.
Yes, cant able to get through that. need your help. below is the code:
import time
import hmac
import hashlib
import requests
Delta Exchange API credentials
api_key = ‘YOUR_API_KEY’ # Replace with your API key
api_secret = ‘YOUR_API_SECRET’ # Replace with your API secret
Base URL for Delta Exchange API
base_url = ‘https://api.india.delta.exchange/v2’
Function to create the signature
def create_signature(endpoint, params):
query_string = ‘&’.join([f"{key}={value}" for key, value in sorted(params.items())])
payload = f"{endpoint}?{query_string}"
return hmac.new(api_secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
Function to get available balance
def get_balance():
endpoint = ‘/v2/wallet/balance’
timestamp = int(time.time() * 1000)
# Prepare the request parameters
params = {
'api_key': api_key,
'timestamp': timestamp,
}
# Create the signature
signature = create_signature(endpoint, params)
params['signature'] = signature
# Send the request to Delta Exchange API
response = requests.get(base_url + endpoint, params=params)
# Handle the response
if response.status_code == 200:
data = response.json()
return data['data'] # Return the balance data
else:
print(f"Error: {response.status_code} - {response.text}")
return None
Get and display the available balance
balance = get_balance()
if balance:
print(“Available Balance:”)
for currency in balance:
print(f"{currency[‘currency’]} : {currency[‘available_balance’]}")
else:
print(“Failed to retrieve balance.”)
Hi All, Do you have the code ready to get the balance in Delta Wallet?
Hi Deepakc8052a, will be sharing the code for the same shortly.
- The base URL should not include a suffix like
/v2
. - According to the documentation, the correct endpoint is
/v2/wallet/balances
. - Signature data must be included in the headers, not in the parameters.
- The
create_signature
function must include the HTTP method as part of the signature. - The headers should contain
api-key
, notapi_key
.
Sharing the corrected version of the code
import hmac
import hashlib
import requests
import datetime
base_url = "https://api.india.delta.exchange"
api_key = "<api_key>"
api_secret = "<api_secret>"
def create_signature(api_secret, method, timestamp, path, query_string, payload):
signature_data = method + timestamp + path + query_string + payload
signature_data = bytes(signature_data, 'utf-8')
api_secret = bytes(api_secret, 'utf-8')
return hmac.new(api_secret, signature_data, hashlib.sha256).hexdigest()
def get_time_stamp():
d = datetime.datetime.now(datetime.timezone.utc)
epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
return str(int((d - epoch).total_seconds()))
def get_balance():
method = "GET"
path = "/v2/wallet/balances"
timestamp = get_time_stamp()
headers = {
"api-key": api_key,
"signature": create_signature(api_secret, method, timestamp, path, "", ""),
"timestamp": timestamp,
'Content-Type': 'application/json'
}
response = requests.request(method, base_url + path, headers=headers)
if response.status_code == 200:
data = response.json()
return data["result"]
else:
print(f"Error: {response.status_code} - {response.text}")
return None
balance = get_balance()
if balance:
print("Available Balance:")
for currency in balance:
print(f'{currency["asset_symbol"]} : {currency["available_balance"]}')
else:
print("Failed to retrieve balance.")