Google Script - REST API 公共端點

上次更新時間: 2025年4月1日

我們的REST API 公共端點可透過簡單的 HTTP 請求存取(就像透過網絡瀏覽器請求網頁一樣),因此只需幾行 Google Script 程式碼即可實作將市場數據匯入 Google 試算表的 API 客戶端。

安裝

  1. 1

    建立新的 Google 試算表或開啟現有試算表。

  2. 2

    透過擴充功能 -> Apps Script 選單開啟指令碼編輯器。

  3. 3

    刪除顯示的預設程式碼(例如,全選然後刪除/退格)。

  4. 4

    將 Google Script API 程式碼(如下所示)複製/貼上到指令碼編輯器。

  5. 5

    可選 - 新增任何您需要的額外自訂函數(例如,呼叫不同的端點或傳回不同的 JSON 欄位)。

  6. 6

    透過 (儲存專案) 圖示儲存 Google Script 程式碼。

    General_SaveIcon_10052020.png

用法

KAPI_Public() 函數負責建立適當的 URL 並向 API 發出 HTTP 請求。透過在 Google 試算表儲存格中輸入以下內容(或根據端點和參數而定類似內容),可以直接呼叫 KAPI_Public() 函數:

bash

Bash

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

=KAPI_Depth("XDGUSD", "5")

KAPI_Public() 函數會從 API 傳回原始 JSON 回應,例如上述 Depth 端點範例的以下內容:

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]]}}}

為了將個別欄位/值放入 Google 試算表中的不同儲存格,可以呼叫額外的自訂函數來解析 JSON 回應,例如範例 KAPI_Ticker() 和 KAPI_Depth() 函數:

bash

Bash

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

=KAPI_Depth("XDGUSD", "5")

這將顯示類似以下的結果,其中數據可供任何標準 Google 試算表函數(SUM、AVERAGE、COUNT 等)存取:

API_GoogleSheetExample_10052020.png

公共端點的 Google 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
}

特定端點的函數範例:

深度:

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
}

行情:

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
}

要使用上述指令碼,只需將通用指令碼附加到特定端點指令碼即可:

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
}

本文中顯示的小數點和千位分隔符可能與我們交易平台上顯示的格式不同。請查閱我們關於如何使用點和逗號的文章以獲取更多資訊。

需要更多幫助?