Search code examples
pythonkucoin

Kucoin Api v2 Python Place a New Order Code - {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}


Getting this error how to fix ? i m trying to create a function to plance an order kucoin api v2 python but give error how to fix

i tried to make this function with full code but still cant find a solution whats wrong

Kucoin Api v2 Python Place a New Order Code - {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}
base_uri = 'https://api.kucoin.com'

def float_to_dic(price):
    formatted_float = "{:.9f}".format(price)
    return formatted_float

def get_headers(method, endpoint):
    now = int(time.time() * 1000)
    str_to_sign = str(now) + method + endpoint
    signature = base64.b64encode(hmac.new(api_secret.encode(), str_to_sign.encode(), hashlib.sha256).digest()).decode()
    passphrase = base64.b64encode(hmac.new(api_secret.encode(), api_passphrase.encode(), hashlib.sha256).digest()).decode()
    return {'KC-API-KEY': api_key,
            'KC-API-KEY-VERSION': '2',
            'KC-API-PASSPHRASE': passphrase,
            'KC-API-SIGN': signature,
            'KC-API-TIMESTAMP': str(now)
    }

#List Accounts
method = 'POST'
endpoint = '/api/v1/orders'

price_buy = float(1900)
quantity = 0.3

price = "{:.9f}".format(price_buy)
symbol = 'ETH-USDT'


body = '{"text":"t-123456","symbol":"' + symbol + '","type":"market","tradeType":"TRADE","side":"buy","time_in_force":"gtc","auto_borrow":false}'

response = requests.request(method, base_uri+endpoint, headers=get_headers(method, endpoint), data=body)


print(response.status_code)
print(response.json())

Solution

  • Here is a snippet of code i have created based on kucoin-python wrapper. You can test it :

    from kucoin.client import Trade, Market
    
    
    class my_kukoin_handler:
    def __init__(self,key,sec,pas):
        self.api_key = key
        self.api_secret = sec
        self.api_passphrase = pas
        self.client = Trade(key=self.api_key, secret=self.api_secret, passphrase=self.api_passphrase, is_sandbox=False, url='')
    
    def update_client(self):
        self.client = Trade(key=self.api_key, secret=self.api_secret, passphrase=self.api_passphrase, is_sandbox=False,url='')
    
    def buy(self,coin_no_usdt,qty):
        try:
            order_id = self.client.create_market_order(str(coin_no_usdt).upper().replace('USDT','').strip()+'-USDT', 'buy', size=str(int(qty)))
        except Exception as e :
            print('Kukoin error while buyin has occured',str(e))