Crypto.com API : Create Order

490 Views Asked by At

Crypto.com API is not very friendly.

Took forever to get account balance authorized. Using same technique, same encoding I cannot get the order created. Always get unauthorized.

<cfscript>
   apiKey = "#cr_key#";
   apiSecret = "#cr_s#";

   hmacm = #req_path# & 121 & apikey & #unixdatetimeNow.getTime()#;

   CrHex = hmac(hmacm, apiSecret, "HmacSHA256");

   theKeyBytes = charsetDecode(ApiSecret, "UTF-8");
   crsign = lcase(hmac(hmacm, apiSecret, "HmacSHA256"));

   newbody = serializeJSON({
   "api_key": "#cr_key#",
   "id": "121",
   "method": "#req_path#",
   "nonce": "#unixdatetimeNow.getTime()#",
   "params": {},    
   "sig": "#crsign#"
    });

 </cfscript>

This works for account balance just fine.

 hmacm = #req_path# & 121 & apikey & #unixdatetimeNow.getTime()#; 

THIS is the HMACM getting envrypted which works is:

private/get-account-summary121XXXXoZW8Cw75583kiSMqjp1649261460418

This HMACM has no parameters in it which are only {} in the body.

 <CFHTTP METHOD="POST" URL="#base_api##req_path#" result="result">
 <cfhttpparam type="header" name="Content-Type" value="application/json">
 <cfhttpparam type="body" value="#newbody#">
 </cfhttp>
 

But below trying the order. I get Unauthorized no matter what.

  <cfscript>

   apiKey = "#cr_key#";
   apiSecret = "#cr_s#";

   sparams = serializeJSON({
  "instrument_name": "#symb#",
  "side": "#side#",
  "type": "#type#",
  "quantity": #size#
  });

   hmacm = #req_path# & 121 & apikey & sparams & #unixdatetimeNow.getTime()#;

   CrHex = hmac(hmacm, apiSecret, "HmacSHA256");

   theKeyBytes = charsetDecode(ApiSecret, "UTF-8");
   crsign = lcase(hmac(hmacm, apiSecret, "HmacSHA256"));

   newbody = serializeJSON({
   "api_key": "#cr_key#",
   "id": "121",
   "method": "#req_path#",
   "nonce": "#unixdatetimeNow.getTime()#",
   "params": deserializeJSON(sparams),    
   "sig": "#crsign#"
    });

    trybody = serializeJSON({
   "id": "121",
   "method": "#req_path#",
   "params": deserializeJSON(sparams),    
    "nonce": "#unixdatetimeNow.getTime()#" 
    });

 </cfscript>

For the ORDER HMACM I have tried both of these with the sparams (used to avoid params variable) and without:

 hmacm = #req_path# & 121 & apikey & sparams & #unixdatetimeNow.getTime()#;

 hmacm = #req_path# & 121 & apikey & #unixdatetimeNow.getTime()#;

I have tried both these for the HMACM:

private/create-order121XXXXoZW8Cw75583kiSMqjp{"side":"sell","instrument_name":"XLM_USDT","quantity":333,"type":"market"}1649261460418

private/create-order121XXXXoZW8Cw75583kiSMqjp1649261460418

In the order I have tried both the newbody and trybody. Using the same CFHTTP.

 <CFHTTP METHOD="POST" URL="#base_api##req_path#" result="result">
 <cfhttpparam type="header" name="Content-Type" value="application/json">
 <cfhttpparam type="body" value="#newbody#">
 </cfhttp>
 

I'm lost. Some exchanges I have got up and running in 30 minutes. Crypto.com API is proving to be very difficult.

Crypto.com API Documentation link.

https://exchange-docs.crypto.com/spot/index.html?csharp#private-create-order

1

There are 1 best solutions below

0
On

Cracked it finally. Worked backwards to the Account Summary working. Might have been capital letters for SELL and MARKET.

I just used simple cfsets to arrange the strings in alphabetical order as well.

<cfset unixdatetimeNow = dateConvert( "local2Utc", now() )>

<cfset pl = "instrument_nameXLM_USDTquantity5000sideSELLtypeMARKET">

<cfset strp = '{"instrument_name": "XLM_USDT","quantity": 5000,"side": "SELL","type": "MARKET"}'>

<cfscript>
  apiKey = "#cr_key#";
  apiSecret = "#cr_s#";

  hmacm = #req_path# & 121 & apikey & #pl# & #unixdatetimeNow.getTime()#;

  CrHex = hmac(hmacm, apiSecret, "HmacSHA256");

  theKeyBytes = charsetDecode(ApiSecret, "UTF-8");
  crsign = lcase(hmac(hmacm, apiSecret, "HmacSHA256"));

</cfscript>

<cfset fullbody = '{"api_key": "#cr_key#","id": 121,"method": "#req_path#","params": #strp#,"sig": "#crsign#","nonce": #unixdatetimeNow.getTime()#}'>

<CFHTTP METHOD="POST" URL="#base_api##req_path#" result="result">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="body" value="#fullbody#">
</cfhttp>