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
}

本文中显示的小数点和千位分隔符可能与我们交易平台上显示的格式不同。请查阅我们关于如何使用小数点和逗号的文章以获取更多信息。

需要更多帮助吗?