kucoin-node-sdk icon indicating copy to clipboard operation
kucoin-node-sdk copied to clipboard

How do I sign requests?

Open wliamwhite opened this issue 3 years ago • 4 comments

I am having trouble making api requests for anything that requires a signature. I am under the impression that I need to use base64 and hmac to encode the 'api_secret' and timestamp, as well as the 'api_secret' and the 'api_passphrase.' this is from the kucoin api docs:

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())

and then the request object includes

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" # specifying content type or using json=data in request }

Do I have a second export in config.js for this?

wliamwhite avatar Dec 02 '21 15:12 wliamwhite

You can review the source code of the utils file. The utils file

If you use this package you don't need to create a sign again. But for your manual handling, you can review the code.

 function sign(text, secret, outputType = 'base64') {
   return CryptoJS
    .createHmac('sha256', secret)
     .update(text)
     .digest(outputType);
 }

 function auth(ApiKey, method, url, data) {
   const { authVersion } = getConfig();
   const timestamp = Date.now();
   const signature = sign(timestamp + method.toUpperCase() + url + data, ApiKey.secret);
   const returnData = {
     'KC-API-KEY': ApiKey.key,
     'KC-API-SIGN': signature,
     'KC-API-TIMESTAMP': timestamp.toString(),
     'KC-API-PASSPHRASE': ApiKey.passphrase || '',
     'Content-Type': 'application/json',
     'User-Agent': 'KuCoin-Node-SDK/${version}',
   };
   if (authVersion && (authVersion === 2 || authVersion === '2')) { // for v2 API-KEY
     returnData['KC-API-KEY-VERSION'] = 2;
     returnData['KC-API-PASSPHRASE'] = sign(ApiKey.passphrase || '', ApiKey.secret);
   }
   return returnData;
 }

RezaAb avatar Dec 04 '21 09:12 RezaAb

@RezaAb Thanks for sharing this however how do you make us of it? Do you have an example with Get Order Id for instance? Thanks

StephaneTurquay avatar Jan 26 '22 21:01 StephaneTurquay

@RezaAb same here. How to use this library?

AndrejGajdos avatar Feb 14 '22 15:02 AndrejGajdos

@StephaneTurquay @AndrejGajdos You don't need to sign anything. It is automated.

 const kucoinAPI = require('kucoin-node-sdk');
 kucoinAPI.init({
 	baseUrl: 'https://api.kucoin.com',
 	apiAuth: {
 		key: "", // KC-API-KEY
 		secret: "", // API-Secret
 		passphrase: "", // KC-API-PASSPHRASE
 	},
 	authVersion: 2, // KC-API-KEY-VERSION. Notice: for v2 API-KEY, not required for v1 version.
 });
 let orderId = "";
 let result = await kucoinAPI.rest.Trade.Orders.getOrderByID(orderId);

RezaAb avatar Feb 16 '22 08:02 RezaAb