在 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 快取的影響。

發布於 。

後續步驟