In Australia, Beaufort Fiduciaries Pty Ltd (ACN 162 139 871, AFSL No. 545124) provides wholesale clients with access to derivatives where the underlying assets are digital assets. Derivatives are complex, regulated financial products that are difficult to understand and may not be suitable for inexperienced investors. For eligibility, terms and conditions click here. Krak Pay is offered by Bit Trade Australia Pty Ltd (ACN 163 237 634), Authorised Representative of Flexewallet Pty Ltd (AFSL 448066). This information is general in nature and does not take into account your personal objectives, financial situation or needs. You should consider whether it is appropriate for you and read the relevant disclosure documents before making any decision.

Google Script - REST API public endpoints

Last updated: April 1, 2025

Our REST API public 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

    Optional - Add any additional custom functions that you require (to call different endpoints or return different JSON fields, for example).

  6. 6

    Save the Google Script Code via the (Save project) icon.

    General_SaveIcon_10052020.png

Usage

The KAPI_Public() function is responsible for creating the appropriate URL and making the HTTP request to the API. The KAPI_Public() function can be called directly by entering the following (or similar depending upon the endpoint and the parameters) within a Google Sheet cell:

bash

Bash

=KAPI_Ticker("XBTUSD,ETHEUR,LTCUSD,XDGXBT,XDGUSD")

=KAPI_Depth("XDGUSD", "5")

The KAPI_Public() function returns the original JSON response from the API, such as the following for the above Depth endpoint example:

bash

Bash

{"error":[],"result":{"XETHZUSD":{"asks":[["231.74000","4.386",1583402326],["231.75000","27.337",1583402277],["231.76000","5.887",1583402311],["231.79000","36.280",1583402334],["231.80000","50.000",1583402299]],"bids":[["231.70000","18.534",1583402335],["231.67000","22.109",1583402335],["231.61000","7.930",1583402335],["231.60000","33.841",1583402335],["231.54000","115.017",1583402334]]}}}

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_Ticker() and KAPI_Depth() functions:

bash

Bash

=KAPI_Ticker("XBTUSD,ETHEUR,LTCUSD,XDGXBT,XDGUSD")

=KAPI_Depth("XDGUSD", "5")

which 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_GoogleSheetExample_10052020.png

Google Script Code for Public Endpoints:

General Function to be included for calls to all Public Endpoints:

bash

Bash

function KAPI_Public(endpoint, parameters) {
  http_response = UrlFetchApp.fetch(
    'https://api.kraken.com/0/public/' + endpoint + '?' + parameters
  )
  api_data = http_response.getContentText()
  return api_data
}

Examples of Functions that are specific to a particular Endpoint:

Depth:

bash

Bash

function KAPI_Depth(currency_pair, depth) {
  api_data = JSON.parse(
    KAPI_Public("Depth", "pair=" + currency_pair + "&count=" + depth)
  )
  api_results = new Array()
  for (count = 0; count < parseInt(depth); count++) {
    api_results.push([
      api_data['result'][currency_pair]['bids'][count][0], 
      api_data['result'][currency_pair]['bids'][count][1], 
      api_data['result'][currency_pair]['asks'][count][0], 
      api_data['result'][currency_pair]['asks'][count][1]
    ])
  }
  return api_results
}

Ticker:

bash

Bash

function KAPI_Ticker(currency_pairs) {
  api_data = JSON.parse(
    KAPI_Public("Ticker", "pair=" + currency_pairs)
  )
  api_results = new Array()
  for (name in api_data['result']) {
    api_results.push([
      name, 
      api_data['result'][name]['a'][0], 
      api_data['result'][name]['a'][2], 
      api_data['result'][name]['b'][0], 
      api_data['result'][name]['b'][2], 
      api_data['result'][name]['c'][0], 
      api_data['result'][name]['c'][1]
    ])
  }
  return api_results
}

To use the scripts above, simply append the generic script to the endpoint specific script:

bash

Bash

function KAPI_Public(endpoint, parameters) {
  http_response = UrlFetchApp.fetch(
    'https://api.kraken.com/0/public/' + endpoint + '?' + parameters
  )
  api_data = http_response.getContentText()
  return api_data
}

function KAPI_Depth(currency_pair, depth) {
  api_data = JSON.parse(
    KAPI_Public("Depth", "pair=" + currency_pair + "&count=" + depth)
  )
  api_results = new Array()
  for (count = 0; count < parseInt(depth); count++) {
    api_results.push([
      api_data['result'][currency_pair]['bids'][count][0], 
      api_data['result'][currency_pair]['bids'][count][1], 
      api_data['result'][currency_pair]['asks'][count][0], 
      api_data['result'][currency_pair]['asks'][count][1]
    ])
  }
  return api_results
}

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.

Need more help?