+ Start a Discussion
Anindya ChattopadhyayAnindya Chattopadhyay 

Consuming Kraken API from Apex

Hi all:

I am trying to consume Kraken API from Apex. Here is the link to their API:

https://www.kraken.com/help/api#general-usage

For the public market data, it works without any problem. However can't get the Private user data API working. For Private calls, we need to provide Signature and Key. 

API-Key = API key
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key

Here is my code:

           request.setHeader('API-KEY', 'XYZ');    //API-Key = API key	        
	       private static String getNonce{
    		 get{
    			return String.valueOf(System.currentTimeMillis());
    			
    		 } 
    		 set;
    	    }            
            String path = '/0/'+endPoint;
            String secret = 'ABC==';
            String payLoad = null //Not passing any data at this stage
            Blob digest = Crypto.generateDigest('SHA-256', blob.valueOf(getNonce+payLoad));
            Blob pathBlob = Blob.valueOf(path);
            
            String combinedDataAsHex = EncodingUtil.convertToHex(pathBlob) + EncodingUtil.convertToHex(digest);
            
	        Blob combinedDataAsBlob = EncodingUtil.convertFromHex(combinedDataAsHex);
            
            Blob apiSignature = Crypto.generateMac('hmacSHA512', combinedDataAsBlob, EncodingUtil.base64Decode(secret)); 
            request.setHeader('API-Sign', EncodingUtil.base64Encode(apiSignature));

I made sure the key and secret are exact. Now when I execute the following line:

HttpResponse response = new Http().send(request);

the reasponse code is always 200 OK. However when I print the response, I see:

15:58:53:953 USER_DEBUG [843]|DEBUG|RESPONSE: {"error":["EGeneral:Internal error"]}

If someone can please help me that would be great. May be I am doing something wrong when generating the combined data as Blob?!

Any help will be much appreciated.

Thanks and regards,
Anindya

alex chernikalex chernik
Hi. Did you solve this issue?