Rob Cranton
Rob Cranton

Reputation: 21

Using Google Sheets script editor to access the Kraken API

I can't figure out where the Kraken API inputs go. I am using https://api.kraken.com/0/private/AddOrder as a starting point. The inputs include things like: pair = XBTUSD, type = buy, ordertype = limit etc. I'm an API noob and I realise this isn't a typical approach but I would greatly appreciate any guidance.

I've successfully used the API for other things like retrieving account balances. Just not sure where inputs fit in. I couldn't figure it out from the documentation here https://www.kraken.com/help/api. I would like to use a specific buy order as a learning example. ie buy 0.003 BTC, pair XBTUSD, limit 5000 USD...

The relevant piece of code-

function buyKraken () {  

  var path = "/0/private/AddOrder";   
  var nonce = new Date () * 1000;  
  var postdata = "nonce=" + nonce;  
  var signature = getKrakenSignature (path, postdata, nonce);  
  var url = 'https://api.kraken.com' + path;  

var options = {  
    method: 'post',  
    headers: {  
      'API-Key': "###########",  
      'API-Sign': signature  
    },      
    payload: postdata  
  };  
  var response = UrlFetchApp.fetch (url, options);  
  Logger.log(response);  
  ;
}

Upvotes: 2

Views: 1069

Answers (2)

Pedro C
Pedro C

Reputation: 31

To the new people that may come search for an answer I used a small different function:

    function getKrakenSignature (path, postdata, krakenSecretKey, nonce) {
        var sha256obj = new jsSHA ("SHA-256", "BYTES");
        sha256obj.update (nonce + postdata);
        var hash_digest = sha256obj.getHash ("BYTES");

        var sha512obj = new jsSHA ("SHA-512", "BYTES");
        sha512obj.setHMACKey (krakenSecretKey, "B64");
        sha512obj.update (path);
        sha512obj.update (hash_digest);
        return sha512obj.getHMAC ("B64");
     }

I used the jsSHA function from this repository: https://github.com/Caligatio/jsSHA You just have to create a new file in the Google Scripts, and copy/paste the raw of this file: https://raw.githubusercontent.com/Caligatio/jsSHA/master/src/sha.js

Upvotes: 1

eborrallo
eborrallo

Reputation: 750

see this project of Github for kraken api and go to the part of examples .

The part do you need is this , it is a one part of the all code ,please don't copy and paste

function QueryPrivate($method, array $request = array())
    {
        if(!isset($request['nonce'])) {
            // generate a 64 bit nonce using a timestamp at microsecond resolution
            // string functions are used to avoid problems on 32 bit systems
            $nonce = explode(' ', microtime());
            $request['nonce'] = $nonce[1] . str_pad(substr($nonce[0], 2, 6), 6, '0');
        }
        // build the POST data string
        $postdata = http_build_query($request, '', '&');
        // set API key and sign the message
        $path = '/' . $this->version . '/private/' . $method;
        $sign = hash_hmac('sha512', $path . hash('sha256', $request['nonce'] . $postdata, true), base64_decode($this->secret), true);
        $headers = array(
            'API-Key: ' . $this->key, // "your key", 
            'API-Sign: ' . base64_encode($sign)//your signature
        );
        // make request
        curl_setopt($this->curl, CURLOPT_URL, $this->url . $path);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($this->curl);
        if($result===false)
            throw new KrakenAPIException('CURL error: ' . curl_error($this->curl));
        // decode results
        $result = json_decode($result, true);
        if(!is_array($result))
            throw new KrakenAPIException('JSON decode error');
        return $result;
    }

// Add a standard order: buy €300 worth of BTC at market at 2013-08-12T09:27:22+0000 
$res = QueryPrivate('AddOrder', array(
    'pair' => 'XBTCZEUR', 
    'type' => 'buy', 
    'ordertype' => 'market', 
    'oflags' => 'viqc',
    'volume' => '300', 
    'starttm' => '1376299642' 
));
print_r($res);

Upvotes: 0

Related Questions