在 Looker Studio API 场景中使用 Narwhal API

通过 Google 表格,你可以在 Looker Studio 中为 Narwhal 的节假日、发布日期、经济数据、大宗商品、外汇和预测数据绘制图表。Apps Script 用你的 API 密钥把数据拉进表格,Looker Studio 再把该表格作为数据源。每日触发器会为 Brent、汇率和预测市场赔率追加行。

你将完成的设置

Looker Studio 会连接到存放 Narwhal 数据的 Google 表格。每个领域使用一个标签页,并用 Apps Script 把 API 响应拉取到这些标签页中。每日脚本使用名为 Brent、FX 和 Fed 的标签页;节假日、发布日期和 CPI 使用各自的标签页,通过 Google 表格教程处理。Narwhal 的经济数据包括 CPI、通胀、GDP 和收益率;这里的 CPI 请求是表格用的经济数据示例。

设置你的 API 密钥

  1. 创建免费密钥 在 narwhalapi.com/signup 创建账户并获取 API 密钥。Narwhal 用一个账户、一个密钥覆盖全部数据 API,REST 与 MCP 共用同一个密钥。
  2. 把密钥存入 Apps Script 在 Google 表格的 Apps Script 项目中,把密钥存入脚本属性 NARWHAL_API_KEY。脚本发起请求时会读取该属性;不要把密钥写进你粘贴的代码里。
  3. 准备表格标签页 创建 appendDaily 使用的 Brent、FX 和 Fed 标签页,再为你想拉取的其余每个领域各建一个标签页。标签页名称要与脚本一致,这样每一行才会写入预期的标签页。

把表格连接到 Looker Studio

  1. 填充表格 加载数据时,请使用 Google 表格教程中的 Apps Script 设置。其中的 pullAll() 函数会填充节假日、发布日期和 CPI 标签页;每日新增的行则在这里用 appendDaily。
  2. 把表格连接为数据源 在 Looker Studio 中,把 Google 表格连接为数据源。选用你想绘图的领域所对应的标签页,每个领域各自独占一个标签页。
  3. 用表格数据制作图表 用该标签页中的数据制作图表。若要做时间序列,请保留之前拉取的行,并在每次定时拉取时追加一行,而不是清空标签页。

用代码获取数据

把这段 Apps Script 添加到包含数据标签页的 Google 表格中。它从脚本属性读取 NARWHAL_API_KEY,并以 Authorization: Bearer YOUR_KEY 请求头发送到 https://api.narwhalapi.com。每个追加的行都包含拉取日期,其余列存放该领域的值。Brent 行包含现货价格,FX 行包含货币和汇率,Fed 行则包含会议决议日期和按兵不动的概率。

Apps Script,每天追加一行
function appendDaily() {
  const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
  const get = (path) =>
    JSON.parse(
      UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
        headers: { Authorization: "Bearer " + key },
      }).getContentText(),
    );
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const brent = get("/v1/commodities/spot/BRENT/USD");
  book.getSheetByName("Brent").appendRow([new Date(), Number(brent.price)]);
  const fx = get("/v1/fx/rates/USD?currencies=EUR,IDR").rates;
  fx.forEach((rate) =>
    book.getSheetByName("FX").appendRow([new Date(), rate.currency, Number(rate.rate)]),
  );
  const hold = get("/v1/predictions/policy-paths/federal-reserve").meetings[0];
  book
    .getSheetByName("Fed")
    .appendRow([new Date(), hold.resolves_at, Number(hold.p_hold)]);
}
// Holidays, releases and CPI change rarely: fill their tabs with pullAll() from
// the Google Sheets tutorial.

按计划定时运行

在 Apps Script 中设置每日时间驱动触发器来运行 appendDaily。该函数把 Brent 价格追加到 Brent,把货币汇率追加到 FX,把第一场会议的按兵不动概率追加到 Fed。Looker Studio 读取该表格来绘制图表。

想要时间序列,就要保留之前的行。清空标签页会删掉图表用作历史的先前拉取数据。节假日、发布日期和 CPI 很少变化,因此这些标签页请用 Google 表格教程中的 pullAll() 来填充。

在 Looker Studio 中使用节假日

把 2026 年美国节假日拉取到节假日标签页。请求返回 holidays,每一项都有 date 和 name。在 Google 表格设置中,用 pullAll() 填充此标签页。若希望图表从表格读取节假日日期和名称,请把 Looker Studio 连接到节假日标签页。

读取节假日
const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
const get = (path) =>
  JSON.parse(
    UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
      headers: { Authorization: "Bearer " + key },
    }).getContentText(),
  );
const holidays = get("/v1/calendars/holidays/USA/2026").holidays;

在 Looker Studio 中使用发布日期

把 2026 年 11 月的美国发布日期拉取到发布日期标签页。设置 countries=USA,用 start_date 和 end_date 指定范围,并像此请求一样使用 limit=100。读取 releases,每一项包含 at、period、topic 和 status。用 Google 表格教程中的 pullAll() 来填充该标签页的发布数据。

读取发布日期
const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
const get = (path) =>
  JSON.parse(
    UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
      headers: { Authorization: "Bearer " + key },
    }).getContentText(),
  );
const releases = get(
  "/v1/economics/calendar?countries=USA&start_date=2026-11-01" +
    "&end_date=2026-11-30&limit=100",
).releases;

在 Looker Studio 中使用经济数据

使用起始于 2025 年 9 月的美国 CPI 观测值请求。读取 observations,再把每条观测值的 period 和 value 用于 CPI 标签页。Google 表格教程中的 pullAll() 函数可以与节假日、发布日期一起填充此标签页。CPI 序列请使用观测值的 period,而不是 Apps Script 把响应拉进表格的日期。

读取 CPI 观测值
const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
const get = (path) =>
  JSON.parse(
    UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
      headers: { Authorization: "Bearer " + key },
    }).getContentText(),
  );
const observations = get(
  "/v1/economics/USA/cpi/observations?start=2025-09",
).observations;

在 Looker Studio 中使用大宗商品

拉取以 USD 计价的 Brent 现货价格,并读取响应中的 price。每日脚本会把拉取日期和该价格追加到 Brent。现货代码例如有 XAU、XAG、XPT、XPD、XCU、BRENT 和 WTI。

读取 Brent 价格
const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
const get = (path) =>
  JSON.parse(
    UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
      headers: { Authorization: "Bearer " + key },
    }).getContentText(),
  );
const brent = get("/v1/commodities/spot/BRENT/USD");
brent.price;

在 Looker Studio 中使用外汇

把 USD 兑 EUR 和 IDR 的汇率拉取到 FX 标签页。读取响应中的 rates 数组,再取每项的 currency 和 rate。每日脚本会把各货币的汇率连同拉取日期一起追加,因此该标签页会积累时间序列所需的行。

读取汇率
const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
const get = (path) =>
  JSON.parse(
    UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
      headers: { Authorization: "Bearer " + key },
    }).getContentText(),
  );
const rates = get("/v1/fx/rates/USD?currencies=EUR,IDR").rates;

在 Looker Studio 中使用预测数据

拉取美联储政策路径响应并读取 meetings。每场会议都有 resolves_at、p_cut、p_hold 和 p_hike。每日脚本使用第一场会议,把它的决议日期和按兵不动概率连同拉取日期一起追加到 Fed。若要绘制降息或加息概率,请在填充预测标签页时从同一场会议的响应中读取 p_cut 或 p_hike。

读取预测会议
const key = PropertiesService.getScriptProperties().getProperty("NARWHAL_API_KEY");
const get = (path) =>
  JSON.parse(
    UrlFetchApp.fetch("https://api.narwhalapi.com" + path, {
      headers: { Authorization: "Bearer " + key },
    }).getContentText(),
  );
const meetings = get("/v1/predictions/policy-paths/federal-reserve").meetings;

为什么要经过 Google 表格?

Looker Studio 内置的连接器没有设置请求头的字段。Narwhal 的请求使用 Authorization 请求头,因此由 Apps Script 处理请求,并把响应数据放进 Google 表格。然后 Looker Studio 再把该表格连接为数据源。

图表的数据有多新?

图表的新鲜度取决于最近一次触发器运行的时间,再加上 Looker Studio 自身的数据缓存。每日触发器决定 Apps Script 何时把数据拉进表格。表格中的值何时出现在图表里,也会受 Looker Studio 缓存的影响。

发布于。

后续步骤