Skip to content

Instantly share code, notes, and snippets.

@jordanbaucke
Created June 19, 2013 06:16
Show Gist options
  • Save jordanbaucke/5812039 to your computer and use it in GitHub Desktop.
Save jordanbaucke/5812039 to your computer and use it in GitHub Desktop.
Python example code for an authenticated call to Bitfinex API v1
import requests # pip install requests
import json
import base64
import hashlib
import time #for nonce
api_key = ''
api_secret = ''
#url = 'https://bitfinex.com/api/v1/order/new'
url = 'https://bitfinex.com/api/v1/balances'
payloadObject = {
'request':'/api/v1/balances',
'nonce':time.time(),
'options':{}
}
#payload = {
# 'request':'/v1/'
# }
# payload buyorder, btcusd 100 @ $1.00
# payload = {
# 'request':'/v1/order/new',
# 'nonce':time.time(),
# 'options' : {'symbol':'btcusd',
# 'amount':'100.00000000',
# 'price':'1.00',
# 'exchange':'bitfinex',
# 'side':'buy',
# 'type':'limit'}
# }
payload_json = json.dumps(payloadObject)
print payload_json
payload = str(base64.b64encode(payload_json))
print payload
m = hashlib.sha384(api_secret).update(payload).hexdigest()
# headers
headers = {
'X-BFX-APIKEY' : api_key,
'X-BFX-PAYLOAD' : base64.b64encode(payload_json),
'X-BFX-SIGNATURE' : m
}
r = requests.get(url, data={}, headers=headers)
print 'Response Code: ' + str(r.status_code)
print 'Response Header: ' + str(r.headers)
print 'Response Content: '+ str(r.content)
@dothanhnhadktd
Copy link

Good! Thank you!

@trainotti
Copy link

@BitcoinResearch

I've tried your code, but it is not working. Actually the response code it gets is 200 but the content is not right. Would you help me?

My code is like this:

import requests
import json
import base64
import hashlib
import time
import hmac

bitfinexURL = 'https://api.bitfinex.com/'
bitfinexKey = 'key'
bitfinexSecret = b'secret' #the b is deliberate, encodes to bytes

def start():
    print("BitFinex")
    payloadObject = {
            'request':'v1/order/new',
            'nonce':str(int(time.time()*1000000)), #convert to string
            'options':{'symbol':'tIOTUSD',
                     'amount':'100.00',
                     'price':'0.98500',
                     'exchange':'bitfinex',
                     'side':'buy',
                     'type':'limit'}
    }

    payload_json = json.dumps(payloadObject)
    print("payload_json: ", payload_json)

    payload = base64.b64encode(bytes(payload_json, "utf-8"))
    print("payload: ", payload)

    m = hmac.new(bitfinexSecret, payload, hashlib.sha384)
    m = m.hexdigest()

    #headers
    headers = {
          'X-BFX-APIKEY' : bitfinexKey,
          'X-BFX-PAYLOAD' : base64.b64encode(bytes(payload_json, "utf-8")),
          'X-BFX-SIGNATURE' : m
    }

    r = requests.get(bitfinexURL, data={}, headers=headers)
    print('Response Code: ' + str(r.status_code))
    print('Response Header: ' + str(r.headers))
    print('Response Content: '+ str(r.content))

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