For information on changes for our US clients, please visit our Support Center article.

Пошук
Google Script - REST API Private Endpoints
Our REST API authenticated endpoints are accessible via a simple HTTP request (just as a web page is requested via a web browser), so an API client to import market data into a Google Sheet can be implemented with just a few lines of Google Script code.

Installation

  1. 1
    Create a new Google Sheet or open an existing sheet.
  2. 2
    Open the script editor via the Extensions -> Apps Script menu.
  3. 3
    Delete the default code that is shown (Select All then Delete/Backspace, for example).
  4. 4
    Copy/Paste the Google Script API code (shown below) into the script editor.
  5. 5
    Update the example API key to use an API key from your Kraken account.
  6. 6
    Optional - Add any additional custom functions that you require (to call different endpoints, or return different JSON fields, for example).
  7. 7
    Save the Google Script Code via the
    General_SaveIcon_10052020.png
    (Save project) icon.

Usage

The KAPI_Private() function is responsible for creating the appropriate URL, calculating the authentication signature, and making the HTTP request to the API. The KAPI_Private() function can be called directly by entering the following (or similar depending upon the account ID, the endpoint, and the parameters) within a Google Sheet cell:
  • =KAPI_Private("TEST", "Balance", "")
  • =KAPI_Private("TEST", "TradeBalance", "asset=xdg")
  • =KAPI_Private("TEST", "OpenOrders", "userref=886794735")
The KAPI_Private() function returns the original JSON response from the API, such as the following for the above Balance endpoint example:
  • {"error":[],"result":{"ZUSD":"16.4272","ZEUR":"0.3880","ZJPY":"0.45","KFEE":"10368.39","XXBT":"0.0000000072","XXRP":"0.00000000","XLTC":"0.0000000100","XXDG":"13997.00000000","XXLM":"100.00000000"}}
In order to place the individual fields/values into separate cells within the Google Sheet, additional custom functions can be called to parse the JSON response, such as the example KAPI_Balance() and KAPI_OpenOrders() functions:
  • =KAPI_Balance("TEST")
  • =KAPI_OpenOrders("TEST")
This would display the results similar to the following, where the data would be accessible to any of the standard Google Sheets functions (SUM, AVERAGE, COUNT, etc.):
API_GoogleSheetOutputEndpoints_10062020.png

Google Script Code
API_Public_Key = { 'TEST':'IqU1I8OPDz7dtYH7GtloZBVtcx3ECEoHTfmcW7uPGQ3wqnS/CSZ3xEiu' }
API_Private_Key = { 'TEST':'tLIpe3J1uXtzUtbrfFDRIQknuXftfU+D6K4twaeG8Qn4CogW/Y86y8ZU3kmnTOL2mKa03/o5WeKCkpUjCay84w==' }

function KAPI_Private(acc_id, endpoint, parameters) {
Utilities.sleep(Math.random() * 100)

api_key = API_Public_Key[acc_id]
api_secret = Utilities.base64Decode(API_Private_Key[acc_id])
api_path = Utilities.newBlob('/0/private/' + endpoint).getBytes()
api_nonce = Date.now().toString()
api_post = 'nonce=' + api_nonce + '&' + parameters

api_sha256 = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, api_nonce + api_post)
api_hmac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, api_path.concat(api_sha256), api_secret)
api_signature = Utilities.base64Encode(api_hmac)

http_options = {'method':'post', 'payload':api_post, 'headers':{'API-Key':api_key, 'API-Sign':api_signature}}
http_response = UrlFetchApp.fetch('https://api.kraken.com/0/private/' + endpoint, http_options)
api_data = http_response.getContentText()
return api_data
}

function KAPI_Balance(acc_id) {
acc_balances_json = JSON.parse(KAPI_Private(acc_id, 'BalanceEx', ''))
acc_balances = new Array
for ( name in acc_balances_json['result'] ) {
acc_balances.push([name, parseFloat(acc_balances_json['result'][name]['balance'])])
}
return acc_balances
}

function KAPI_OpenOrders(acc_id) {
acc_orders_json = JSON.parse(KAPI_Private(acc_id, 'OpenOrders', ''))
acc_orders = new Array
for ( name in acc_orders_json['result']['open'] ) {
acc_orders.push([name, acc_orders_json['result']['open'][name]['descr']['pair'], acc_orders_json['result']['open'][name]['descr']['type'], parseFloat(acc_orders_json['result']['open'][name]['descr']['price']), parseFloat(acc_orders_json['result']['open'][name]['vol']), parseFloat(acc_orders_json['result']['open'][name]['vol_exec']), parseFloat(acc_orders_json['result']['open'][name]['opentm'])])
}
return acc_orders
}
The decimal and thousands separators shown in this article may differ from the formats displayed on our trading platforms. Review our article on how we use points and commas for more information.