Skip to content

Instantly share code, notes, and snippets.

@MarvinBaral
Last active January 19, 2019 21:20
Show Gist options
  • Save MarvinBaral/e30763aac68c9dbc8ca6d7f3d20b3a4e to your computer and use it in GitHub Desktop.
Save MarvinBaral/e30763aac68c9dbc8ca6d7f3d20b3a4e to your computer and use it in GitHub Desktop.
Bitforex API Call with signed Data
#!/usr/bin/env python2
# See https://github.com/bitforexapi/API_Doc_en/wiki/API-Call-Description
import urllib
import requests, json, time
import hmac, hashlib
def simple_api_call(requestLink, params) : #GET
url = "https://api.bitforex.com" + requestLink
i = 0
for key, value in params.iteritems():
if i == 0:
url += "?"
else:
url += "&"
url += key + "=" + str(value)
i += 1
response = urllib.urlopen(url)
data = json.loads(response.read())
return data
def signed_api_call(requestLink, params, secretKey) : #POST
#===============================================================================
#create data to sign
#===============================================================================
url = "https://api.bitforex.com" + requestLink
timestamp = int(round(time.time() * 1000))
params["nonce"] = timestamp #add timestamp
sortedPostData = sorted(params.items()) #alphabetical order important!!!
postDataString = requestLink
i = 0
for key, value in sortedPostData:
if i == 0:
postDataString += "?"
else:
postDataString += "&"
postDataString += key + "=" + str(value)
i += 1
#===============================================================================
#sign the data
#===============================================================================
contentToSign = postDataString
secret = secretKey.encode(encoding='UTF-8')
signData = hmac.new(secret, postDataString, digestmod=hashlib.sha256).hexdigest()
#===============================================================================
#send signed request
#===============================================================================
sortedPostData.append(('signData', signData))
r = requests.post(url, data=sortedPostData, allow_redirects=False)
respData = json.loads(r.content)
return respData
@MarvinBaral
Copy link
Author

MarvinBaral commented Jan 19, 2019

How to use:

#!/usr/bin/env python2
# See https://github.com/bitforexapi/API_Doc_en/wiki/API-Call-Description
import bitforexAPI

requestLink = "/api/v1/market/symbols" #dict()
requestLink = "/api/v1/market/ticker" #dict(symbol="coin-usdt-btc")
requestLink = "/api/v1/market/depth" #dict(symbol="coin-usdt-btc", size=5)
requestLink = "/api/v1/market/trades" #dict(symbol="coin-usdt-btc", size=5)
requestLink = "/api/v1/market/kline" #dict(symbol="coin-usdt-btc", ktype="1min", size=10) #!!!return values different than described in doku
data = bitforexAPI.simple_api_call(requestLink, dict(symbol="coin-usdt-btc", ktype="1min", size=10))
for key, value in data.items():
    print key, value

requestLink = "/api/v1/fund/mainAccount" #dict(currency="usdt", accessKey=accessKey)
requestLink = "/api/v1/fund/allAccount" #dict(accessKey=accessKey)
accessKey = "......."
secKey = "......."
data = bitforexAPI.signed_api_call(requestLink, dict(accessKey=accessKey), secKey)
for key, value in data.items():
    print key, value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment