I want to check the margin balance of bitflyer lightning with python, but API_KEY and API_SECRET never want to stick to the source code.
Prepare config.txt separately and describe API_KEY and API_SECRET. Use configparser. https://docs.python.org/ja/3/library/configparser.html
config.txt
[bf]
api_key = <API here_Enter KEY>
api_secret = <API here_Enter SELECT>
Margin acquisition.py
import configparser
import hmac
import datetime
import hashlib
import requests
config = configparser.ConfigParser()
config.read('./config.txt')
API_KEY = config['bf']['api_key']
API_SECRET = config['bf']['api_secret']
print(getcollateral())
#Margin confirmation
def getcollateral():
api_key = API_KEY
api_secret = API_SECRET
base_url = "https://api.bitflyer.jp"
path_url = "/v1/me/getcollateral"
method = "GET"
timestamp = str(datetime.datetime.today())
message = timestamp + method + path_url
signature = hmac.new(bytearray(api_secret.encode('utf-8')), message.encode('utf-8') , digestmod = hashlib.sha256 ).hexdigest()
headers = {
'ACCESS-KEY' : api_key,
'ACCESS-TIMESTAMP' : timestamp,
'ACCESS-SIGN' : signature,
'Content-Type' : 'application/json'
}
response = requests.get( base_url + path_url , headers = headers)
return response.json()
{'collateral': 5070.0, 'open_position_pnl': 0.0, 'require_collateral': 0.0, 'keep_rate': 0.0}
For how to use the API, I referred to the post by the following person. Thank you very much. https://qiita.com/sodiumplus3/items/b69dbd3e51fc2a0f7e01
Recommended Posts