Hi flashtrade, Here’s a working example of order creation in Pyhton with signature creation flow. You should include your api key and secret for api_key
and api_secret
variables in the script. Also change the url variable if you are not testing on localhost
the script places a limit buy order for btc-usdt at 30,000 price with 0,001 amount
import json
import requests
import time
import hmac
import hashlib
# API
api_key = "" # use your api key here
api_secret = "" # use your my api secret here
url = 'http://localhost/v2/order'
def get_api_expires():
return str(int(time.time() + 60)) # Expires in 60 seconds
def generate_signature(PATH_URL, METHOD, api_expires, params=None):
string_to_encode = METHOD + PATH_URL + api_expires
if params != None:
string_to_encode += json.dumps(params, separators=(',', ':'))
signature = hmac.new(API_SECRET.encode(),string_to_encode.encode(),hashlib.sha256).hexdigest()
return signature
def init_signature( PATH_URL, METHOD, is_ws):
if is_ws:
method = "CONNECT"
path = '/stream'
api_expires = get_api_expires()
return method, path, api_expires
else:
method = METHOD
path = f"/v2{PATH_URL}"
api_expires = get_api_expires()
return method, path, api_expires
def auth_me( PATH_URL, METHOD, is_ws=False, params=None):
method, path, api_expires = init_signature(PATH_URL, METHOD, is_ws)
signature = generate_signature(path, method, api_expires, params=params)
headers = {
"api-key": API_KEY,
"api-signature": signature,
"api-expires": api_expires
}
return headers
def create_order(market, amount, price, side):
params = {
'symbol': market,
'size': amount,
'price': price,
'side': side,
'type': 'limit' # or 'market' for a market order
}
headers = auth_me("/order", "POST", params=params)
response = requests.post(url, json=params, headers=headers)
if response.status_code == 200:
order_id = response.json().get('id')
print(f'Order created, order id: {order_id}')
return order_id
else:
print(f'Error creating order: {response.text}')
return None
create_order("btc-usdt", 0.001, 30000, "buy")