[2024 Update] Bitkub API V3 Python
3 min readJan 4, 2024
ในช่วงปลายปี 2023 ที่ผ่าน Bitkub ได้เปลี่ยนระบบยกเครื่อง API ใหม่ เพื่อความปลอดภัยที่มากขึ้น ทำให้ไลบรารี่ python เดิมที่ใช้อยู่ใช้ไม่ได้แล้วเลยมาเขียนบันทึกนี้ขึ้นมาใหม่เพื่อโน๊ตและแชร์ให้เพื่อนๆพี่ๆน้องๆเอาไว้ใช้
ผมใช้ ChatGPT เขียนโค้ดให้เป็น Functions ออกมาจะได้ไม่ต้องไปงมเองโดยเอา example ของทาง Bitkub ให้ไปให้ ChatGPT ดูเป็นตัวอย่างและได้โค้ดออกมาดังนี้ครับ
import hashlib, hmac, json,time, requests, urllib3, sys
api_key = ""
api_secret = ""
def gen_sign(api_secret, payload_string=None):
return hmac.new(api_secret.encode('utf-8'), payload_string.encode('utf-8'), hashlib.sha256).hexdigest()
def gen_query_param(url, query_param):
req = requests.PreparedRequest()
req.prepare_url(url, query_param)
return req.url.replace(url,"")
def servertime():
host = 'https://api.bitkub.com'
path = '/api/v3/servertime'
url = host + path
return requests.request('GET', url).text
def get_my_open_orders(symbol):
host = 'https://api.bitkub.com'
path = '/api/v3/market/my-open-orders'
ts = str(servertime())
param = {'sym': symbol}
query_param = gen_query_param(host + path, param)
payload = [ts, 'GET', path, query_param]
sig = gen_sign(api_secret, ''.join(payload))
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-BTK-TIMESTAMP': ts,
'X-BTK-SIGN': sig,
'X-BTK-APIKEY': api_key
}
response = requests.request('GET', f'{host}{path}{query_param}', headers=headers, data={}, verify=False)
return response.json()
# Example for the first function
my_open_orders_result = get_my_open_orders(symbol)
print("My Open Orders Result:", my_open_orders_result)
ส่วนข้างบนเอาไว้เรียกดู my open orders
ส่วนคำสั่ง Order ต่างๆรวมไว้ดังนี้
def cancel_order(order_hash):
host = 'https://api.bitkub.com'
path = '/api/v3/market/cancel-order'
ts = str(servertime())
req_body = {'hash': order_hash}
payload = [ts, 'POST', path, json.dumps(req_body)]
sig = gen_sign(api_secret, ''.join(payload))
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-BTK-TIMESTAMP': ts,
'X-BTK-SIGN': sig,
'X-BTK-APIKEY': api_key
}
response = requests.request('POST', f'{host}{path}', headers=headers, data=json.dumps(req_body), verify=False)
return response.json()
def place_bid_order(symbol, amount, rate, order_type):
host = 'https://api.bitkub.com'
path = '/api/v3/market/place-bid'
ts = str(servertime())
req_body = {'sym': symbol, 'amt': amount, 'rat': rate, 'typ': order_type}
payload = [ts, 'POST', path, json.dumps(req_body)]
sig = gen_sign(api_secret, ''.join(payload))
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-BTK-TIMESTAMP': ts,
'X-BTK-SIGN': sig,
'X-BTK-APIKEY': api_key
}
response = requests.request('POST', f'{host}{path}', headers=headers, data=json.dumps(req_body), verify=False)
return response.json()
def place_ask_order(symbol, amount, rate, order_type):
host = 'https://api.bitkub.com'
path = '/api/v3/market/place-ask'
ts = str(servertime())
req_body = {'sym': symbol, 'amt': amount, 'rat': rate, 'typ': order_type}
payload = [ts, 'POST', path, json.dumps(req_body)]
sig = gen_sign(api_secret, ''.join(payload))
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-BTK-TIMESTAMP': ts,
'X-BTK-SIGN': sig,
'X-BTK-APIKEY': api_key
}
response = requests.request('POST', f'{host}{path}', headers=headers, data=json.dumps(req_body), verify=False)
return response.json()
def get_order_info(symbol, order_id, side, order_hash=''):
host = 'https://api.bitkub.com'
path = '/api/v3/market/order-info'
ts = str(servertime())
param = {'sym': symbol, 'id': order_id, 'sd': side, 'hash': order_hash}
query_param = gen_query_param(host + path, param)
payload = [ts, 'GET', path, query_param]
sig = gen_sign(api_secret, ''.join(payload))
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-BTK-TIMESTAMP': ts,
'X-BTK-SIGN': sig,
'X-BTK-APIKEY': api_key
}
response = requests.request('GET', f'{host}{path}{query_param}', headers=headers, data={}, verify=False)
return response.json()
# Example for the second function
order_info_result = get_order_info(symbol='', order_id='', side='', order_hash='fwQ6dnQYKnqEHoxtaMynnLwYn6C')
print("Order Info Result:", order_info_result)
# Example for the third function
bid_order_result = place_bid_order('doge_thb', 10, 1, 'limit')
print("Place Bid Order Result:", bid_order_result)
HNY2024 All The Best
Donate
BTC: bc1qlppgg2f5w6vcsf9z3wnzwysk558htjrrw52teq
ETH: 0x2b0DaC4dF9d7D0aDA5681f5F7dA490B4880CaB3A (ERC20)
USDT: 0x2b0DaC4dF9d7D0aDA5681f5F7dA490B4880CaB3A (ERC20)
DOGE: DEkoTu6fZv9EU3AKFUxEVUEAXckW1LS2iM
TRX: THzZ2Pen4GxaA7ACsaGaKLbTX8wZWKuSM5