Skip to content

Instantly share code, notes, and snippets.

@jordanbaucke
Created June 19, 2013 06:16
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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)
@mingyun
Copy link

mingyun commented Nov 8, 2013

hi:
run the code ,it returns Key symbol was not present.where is the problem?

@streblo
Copy link

streblo commented Jul 11, 2014

@mingyun, you probably forgot the API key / secret. PS. Don't post these publically!

@jordanbaucke, I updated the URL to https://api.bitfinex.com/v1/ and the hashlib calls to m = hashlib.sha384(api_secret + payload).hexdigest(). BFX returns

Response Code: 400
Response Header: {'status': '400 Bad Request', ...}
Response Content: {"message":"Invalid X-BFX-SIGNATURE."}

Any ideas?

@BitcoinResearch
Copy link

Streblo, I've had the same issue but fixed it. My working code is below, using Python 3.4:

#PYTHON 3.4
import requests
import json
import base64
import hashlib
import time
import hmac

bitfinexURL = 'https://api.bitfinex.com/v1/balances'
bitfinexKey = 'KEY HERE'
bitfinexSecret = b'SECRET KEY HERE' #the b is deliberate, encodes to bytes

def start():
    print("BitFinex")
    payloadObject = {
            'request':'/v1/balances',
            'nonce':str(time.time() * 100000), #convert to string
            'options':{}
    }

    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))

@m-i-k-a-e-l
Copy link

@mingyun, see following msg
@jordanbaucke, see following msg
@BitcoinResearch, see following msg:

I get the "Key symbol was not present" even thogh I provide the X-BFX-APIKEY header. I doublechecked that the key is correct.

Do you have any thought or suggestion here?

@allanpallan
Copy link

Hello

I saw commented code in sample that could enable me to make a trade. I looked at BitcoinResearch code example and wrote my info into it, but seems that I get no answer back from site. Also there is no trade. Wallet info is working fine. What could be the issue here?

Error description:
Traceback (most recent call last):
File "C:/Users/Al/PycharmProjects/bitbot/BitfinexExample.py", line 120, in
order ()
File "C:/Users/Al/PycharmProjects/bitbot/BitfinexExample.py", line 116, in order
r = requests.get(bitfinexorderURL, data={}, headers=headers).json()
File "C:\Users\Al\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\models.py", line 894, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Al\AppData\Local\Programs\Python\Python36-32\lib\json_init_.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Al\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Al\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Code:
import requests
import json
import base64
import hashlib
import time
import hmac

bitfinexorderURL = 'https://api.bitfinex.com/v1/order/new'
bitfinexKey = 'I got the key here'
bitfinexSecret = b'I got the secret here' #the b is deliberate, encodes to bytes

def order():
print("BitFinexorder")
payloadObject = {
'request':'/v1/order/new',
'nonce':str(time.time() * 100000), #convert to string
'options':{'symbol': 'etcusd',
'amount':'1.0000',
'price':'1.00',
'exchange':'bitfinex',
'side':'buy',
'type':'limit'}
}

print(payloadObject)

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(bitfinexorderURL, data={}, headers=headers).json()
print("Response is: ", r)

order ()

@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