I wanted to place order limit with my kucoin api using python but all i got is (Bad Request) 400. How do I solve this, please I need help. here is my code.
stringId = secrets.token_hex(8) #Generating TradeID
url = 'https://api.kucoin.com/api/v1/orders'
now = int(time.time() * 1000)
str_to_sign = str(now) 'POST' '/api/v1/orders'
signature = base64.b64encode(
hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())
passphrase = base64.b64encode(hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
headers = {
"KC-API-SIGN": signature,
"KC-API-TIMESTAMP": str(now),
"KC-API-KEY": api_key,
"KC-API-PASSPHRASE": passphrase,
"KC-API-KEY-VERSION": "2",
"clientOid": stringId,
"side": "sell",
"symbol": "AFK-USDT",
"type": "limit",
"price": "0.05",
"size": "50.00",
}
response = requests.request('post', url, headers=headers)
print(response.status_code)
print(response.json())
This is the error response I got back
400
{'code': '400000', 'msg': 'Bad Request'}
CodePudding user response:
There is a python module called kucoin-python-sdk that does all this for you.
If you would use this library it would boil down to one line like
client.create_limit_order('AFK-USDT', 'sell', '50', '0.05')
If you cannot use this module, you have to follow KuCoin's documentation:
According to KuCoin's documentation, parameters have to be sent in the body of the POST request, not as headers.
So your code should be similar to this:
params = {
"clientOid": stringId,
"side": "sell",
"symbol": "AFK-USDT",
"type": "limit",
"price": "0.05",
"size": "50.00"
}
data_json = json.dumps(params)
str_to_sign = str(now) 'POST' '/api/v1/orders' data_json
signature = base64.b64encode(
hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())
passphrase = base64.b64encode(hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
headers = {
"KC-API-SIGN": signature,
"KC-API-TIMESTAMP": str(now),
"KC-API-KEY": api_key,
"KC-API-PASSPHRASE": passphrase,
"KC-API-KEY-VERSION": "2",
"Content-Type": "application/json"
}
response = requests.request('post', url, headers=headers, data=data_json)