I've been working with the Crypto.com API in Python to fetch data from my portfolio. My script successfully retrieves order history, but fails when trying to access the user balance, returning a 'BAD_REQUEST' error.
Here's the working code for fetching the order history:
def get_order_history():
req = {
"id": 2,
"method": "private/get-order-history",
"api_key": API_KEY,
"params": { },
"nonce": int(time.time() * 1000)
}
paramString = ""
if "params" in req:
for key in req['params']:
paramString += key
paramString += str(req['params'][key])
sigPayload = req['method'] + str(req['id']) + req['api_key'] + str(req['nonce'])
req['sig'] = hmac.new(
bytes(str(SECRET_KEY), 'utf-8'),
msg=bytes(sigPayload, 'utf-8'),
digestmod=hashlib.sha256
).hexdigest()
order_history = requests.post(BASE_URL + 'private/get-order-history', json=req, headers={'Content-Type': 'application/json'})
return json.loads(order_history.text)
order_history = get_order_history()
This works fine. However, when I modify the endpoint to /user-balance to check the balance, keeping the same code, I encounter the following error:
{'code': '10004', 'msg': 'BAD_REQUEST'}
I have tried altering the req parameters and the structure of sigPayload by adding or removing arguments, but nothing seems to work.
After some research and reading through similar issues, it seems like others have also faced problems with the Crypto.com Exchange API. However, I haven't found a clear solution that addresses this specific issue.
Can anyone help me figure out why I'm getting a 'BAD_REQUEST' for the /user-balance endpoint, and how to fix it?
I have tried altering the req parameters and the structure of sigPayload by adding or removing arguments, but nothing seems to work. In addition, I have tried to address the issue with a new pair of API and SECRET keys, and enable/disable the checkboxes with API permissions.