API 快速入門:發出你的第一個請求
取得 API 金鑰,發出第一個請求並查看回應。一組金鑰即可用於所有 Narwhal API。
本指南從 CPI 開始,再介紹以下位址中已上線 API 的參考文件: api.narwhalapi.com。
46 國家: ARG, AUS, AUT, BEL, BRA, CAN, CHE, CHL, CHN, COL, CZE, DEU, DNK, ESP, EST, FIN, FRA, GBR, HRV, IDN, IND, IRL, ISR, ITA, JPN, KOR, LTU, LUX, LVA, MEX, MYS, NLD, NOR, NZL, PAK, PHL, POL, PRT, QAT, SAU, SGP, SVN, SWE, THA, USA, ZAF。 涵蓋範圍依操作與國家而異。各項操作均列出其支援的國家。 215 個國家。沒有本國統計機關、OECD、Eurostat 或 BIS 序列的國家,在歷史路由上使用世界銀行的年度歷史資料。各操作頁面列出其涵蓋的國家。 每個資料請求都需要 Bearer 金鑰。
免費額度: $10 free every month, with no card. Calls cost from $2 to $10 per 1,000, depending on the API. The limit is 50 requests a minute on free credit and 500 once you add paid credit. REST 與 MCP 使用同一份額度。
設定你的 API 金鑰
將金鑰以 Bearer 權杖形式放入 Authorization 標頭傳送,絕不可放入 URL。 建立帳戶以取得免費金鑰,然後閱讀 身分驗證指南 ,再儲存金鑰。
An AI agent can sign up for its user instead: 智慧代理註冊
export NARWHAL_API_KEY="nw_live_..."請求官方 CPI
在路徑中傳入 ISO 三字母國家代碼。省略 period 回傳已接受的最新完整發布版本。
curl --request GET \
--url https://api.narwhalapi.com/v1/economics/USA/cpi \
--header "Authorization: Bearer $NARWHAL_API_KEY"{
"country": "USA",
"period": "2026-07",
"value": "333.918",
"index_base": "1982-84=100",
"released_on": "2026-08-12"
}Python 和 JavaScript 範例
在應用程式中使用相同的環境變數和端點。這些範例使用內建函式庫。
Python 3
import json
import os
from urllib.request import Request, urlopen
request = Request(
"https://api.narwhalapi.com/v1/economics/USA/cpi",
headers={"Authorization": "Bearer " + os.environ["NARWHAL_API_KEY"]},
)
with urlopen(request, timeout=30) as response:
data = json.load(response)
print(data)JavaScript · Node.js 18+
const key = process.env.NARWHAL_API_KEY;
if (!key) throw new Error("Set NARWHAL_API_KEY first");
const response = await fetch(
"https://api.narwhalapi.com/v1/economics/USA/cpi",
{ headers: { Authorization: "Bearer " + key }, signal: AbortSignal.timeout(30000) }
);
const data = await response.json();
if (!response.ok) throw new Error(JSON.stringify(data));
console.log(data);選擇你的 API
選擇國家和指標
提供最新發布操作和對應的歷史操作,但可用指標因國家而異。 經濟資料 API 參考文件 列出每條路徑和回應結構。
| 指標 | 路徑後綴 |
|---|---|
| CPI | / |
| 通膨 | / |
| 失業率 | / |
| 勞動力參與率 | / |
| GDP 與成長率 | / · / |
| 貿易 | / |
MCP 金融資料:透過 MCP 連線
透過 MCP 用戶端使用同一把金鑰與相同的結構描述。工具探索會列出你的帳戶在全部四個 API 中可用的操作。請參閱 MCP 指南。
{
"url": "https://api.narwhalapi.com/mcp",
"headers": {
"Authorization": "Bearer YOUR_NARWHAL_API_KEY"
}
}透過 MCP 用戶端的安全機密設定替換預留值。純 JSON 本身不會自動展開環境變數。
{
"method": "tools/call",
"params": {
"name": "fx_get_rates",
"arguments": { "base": "USD", "currencies": ["EUR", "JPY"] }
}
}初始化與 tools/ 不收費。每次成功的 tools/ 以其 API 的價格計為一次呼叫。
閱讀回應
整合前請讀取每項操作的回應架構。
| 規則 | 這意味著什麼 |
|---|---|
period | 官方發布版本所回傳的確切月份、季度或年份。 |
released_ | 官方發布日期。 |
| 十進位字串 | 數值表示可避免二進位浮點數帶來的意外誤差。 |
| 缺失資料 | 若發布資料不完整或無法使用,則拒絕提供資料;缺失值絕不會變成零。 |
收費回應上的額度標頭
X-Request-IDX-Narwhal-CostX-Narwhal-BalanceX-RateLimit-LimitX-RateLimit-RemainingX-RateLimit-ResetX-RateLimit-Limit is the size of your request bucket (the burst), not a count of requests per minute. The bucket refills at your per-minute rate, X-RateLimit-Remaining is what is left in it, and X-RateLimit-Reset is when it is full again, in UTC.
按狀態和錯誤代碼處理錯誤
先檢查 HTTP 狀態,接著將 code 用於應用程式邏輯。 detail 用於日誌和偵錯。 錯誤與額度指南 列出所有公開狀態。
{
"type": "https://narwhalapi.com/problems/invalid-query",
"title": "Invalid query",
"status": 400,
"detail": "The request parameters are not valid for this operation.",
"code": "invalid_query",
"request_id": "019d1af4-8f33-7b21-91af-ef535f2dcf50"
}幫助與資源
需要金鑰相關協助嗎? 聯絡 Narwhal API。
查看最新變更: 更新日誌。
LLM? 閱讀 llms.txt。
此頁面對你有幫助嗎?