PL1337
PL1337

Reputation: 21

Retrieving the wallet balances on MECX - API Authentication in Python (Signature)

I'm trying to retrieve the wallet balances from the mexc.com cryptocurrency exchange. It works well on others such as Gate.io or Huobi thanks to their awesome documentation. I'm stuck on mexc.com however and confused on how to authenticate myself there.

Based on their documentation, I'd need to send a GET request to /open/api/v2/account/info. I'd also need to sign the request and this is where I'm stuck.

When a request requires a signature, I need to pass the API key, unix time stamp and a calculated signature as URL parameters. I have no clue though how to calculate that signature. Also I'd need to sign a string and pass it into the header.

This is their description:

Image

Upvotes: 2

Views: 2666

Answers (1)

Ramin
Ramin

Reputation: 11

Accounting to MEXC documentation https://mxcdevelop.github.io/apidocs/contract_v1_en/#access-to-url signature is combination of APIkey,secret,request_time and request params.

import hmac
from urllib.parse import urlencode, quote
from collections import OrderedDict

for spot account you can use:

 def signature(self, req_time, sign_params=None):
    if sign_params:
        sign_params = urlencode(sign_params, quote_via=quote)
        to_sign = "{}&timestamp={}".format(sign_params, req_time)
    else:
        to_sign = "timestamp={}".format(req_time)
    sign = hmac.new(self._secret.encode('utf-8'), to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
    return sign

and for future account you can use:

 def signature(self, req_time, sign_params=None):
    if sign_params:
        sign_params = urlencode(OrderedDict(sorted(sign_params.items())), quote_via=quote)
        to_sign = f"{self._access_id}{req_time}{sign_params}"
    else:
        to_sign = f"{self._access_id}{req_time}"
    sign = hmac.new(self._secret.encode('utf-8'), to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
    return sign

Note that place of signature and APIKey are deferent in spot account request and future account request, for future API its in header while in spot API its in request params. for more detail you can read MECX API documentation .

Upvotes: 1

Related Questions