apicryptoapicoldfusion-2016bybit

ByBit : API ColdFusion


Having trouble with ByBit By/Sell API. ColdFusion any help appreciated.

https://bybit-exchange.github.io/docs/spot/v3/?console#t-authenticationparameters

Authentication for POST POST rule: timestamp + api_key + recv_window + raw_request_body

param_str = "1659073093578T0d98KyVamQ62YBzN85000{ "symbol": "BTCUSDT", "orderQty":"0.05", "side": "Sell", "orderType": "LIMITT", "timeInForce": "GTC", "orderPrice": "24500", "orderLinkId": "spotA0008" }"

curl --location --request POST 'https://api-testnet.bybit.com/spot/v3/private/order' \

--header 'X-BAPI-API-KEY: {api key}'
--header 'X-BAPI-TIMESTAMP: 1659067662307'
--header 'X-BAPI-RECV-WINDOW: 5000'
--header 'X-BAPI-SIGN: cc63fb44be4a87f4b7bbd42db012ddacc1c935c3d3ae3e01c3b4be393522c213'
--header 'Content-Type: application/json'
--data-raw '{ "symbol": "BTCUSDT", "orderQty":"0.01", "side": "Buy", "orderType": "LIMIT", "timeInForce": "GTC", "orderPrice": "21300", "orderLinkId": "spotx006", "orderCategory": 1, "triggerPrice": "21700" }'

This is the Post Example. For Account - my signature works fine.

   <cfscript>

    apiKey = "#_key#";
    apiSecret = "#_s#";

   newbody = serializeJSON({
      "symbol": "#symb#",
      "orderQty":"#qty#",
       "side": "#side#",
       "orderType": "#type#"
   });

   ts_key_str = #unixdatetimeNow.getTime()# & '#apikey#' & '5000';

   str_to_sign = #unixdatetimeNow.getTime()# & '#apikey#' & '5000' & '#newbody#';

   HMAC = hmac(str_to_sign, apiSecret, "HMACSHA256");

   </cfscript>

    <cfhttp url="#base_api##req_path#" method="POST" result="result" charset="utf-8">
    <cfhttpparam type="body" value="#newbody#">
    <cfhttpparam type="HEADER" name="Content_Type" value="application/json">
    <cfhttpparam type="header" name="X-BAPI-SIGN-TYPE" value="2"> 
    <cfhttpparam type="header" name="X-BAPI-API-KEY" value="#_key#"> 
    <cfhttpparam type="header" name="X-BAPI-RECV-WINDOW" value="5000"> 
    <cfhttpparam type="header" name="X-BAPI-SIGN" value="#lhmac#"> 
    <cfhttpparam type="header" name="X-BAPI-TIMESTAMP" value="#unixdatetimeNow.getTime()#"> 
    </cfhttp> 

Even adding the ts_key_str in front of new body does not work either.

I get bad signature. When getting account data I using this it works fine cfhttpparam type="body" value=""

Any help appreciated.


Solution

  • It was a timestamp issue.

    Set one timestamp, and use that timestamp. So it doesn't maybe pull different timestamp on a delayed call.

    Updated code.

        <!--- MARKET --->
        <cfset b_body = '{"symbol": "#pair#","orderQty":"#size#","side": "#side#","orderType": "MARKET"}'>
    
        <cfset unixdatetimeNow = dateConvert( "local2Utc", now() )>
        <cfset timestamp = #unixdatetimeNow.getTime()#>
    
        <cfscript>
            apiKey = "#_key#";
            apiSecret = "#_s#";
            str_to_sign = #timestamp# & '#apikey#' & '5000' & '#b_body#';   
            HMAC = hmac(str_to_sign, apiSecret, "HMACSHA256");
        </cfscript>
    
        <cfset lhmac = lCase(#hmac#)>
       
        <cfhttp url="#base_api##req_path#" method="POST" result="result" charset="utf-8">
        <cfhttpparam type="body" value="#b_body#">
        <cfhttpparam type="header" name="X-BAPI-SIGN-TYPE" value="2">
        <cfhttpparam type="header" name="X-BAPI-SIGN" value="#lhmac#"> 
        <cfhttpparam type="header" name="X-BAPI-API-KEY" value="#_key#"> 
        <cfhttpparam type="header" name="X-BAPI-TIMESTAMP" value="#timestamp#"> 
        <cfhttpparam type="header" name="X-BAPI-RECV-WINDOW" value="5000"> 
        <cfhttpparam type="HEADER" name="Content_Type" value="application/json">
        </cfhttp>