Cookie settings
We use essential cookies to keep you signed in and secure, and analytics cookies to improve the product. We never use them for ad tracking.Learn more
Building an AI agent? Start with /llms.txt for the full site index.
文件
使用 typed fetch wrapper 呼叫台股資料 API,內建 timeout 與錯誤碼映射。
npm install ./packages/js-sdkimport { TWMarketDataClient } from "@twmarketdata/sdk"; const client = new TWMarketDataClient({ apiKey: "sk_live_xxx", baseUrl: "https://twmarketdata.com", timeoutMs: 10000,});SDK 內建 AbortController timeout,避免長時間等待上游回應造成程序卡住。
const result = await client.twseDailyPrice({ symbol: "2330", limit: 1,});const valuation = await client.valuationData({ symbol: "2330", limit: 5,}); console.log(valuation.status);console.log(valuation.meta.requestId);console.log(valuation.meta.creditsCost);SDK 會把 X-Request-Id、X-TWMD-Dry-Run、X-TWMD-Credits-Cost、X-TWMD-Credits-Charged、X-TWMD-Plan 轉成 meta。
import { AuthenticationError, EntitlementError, InsufficientCreditsError, DatasetNotFoundError, UpstreamError,} from "@twmarketdata/sdk"; try { await client.monthlyRevenue({ symbol: "2330", limit: 12 });} catch (error) { if (error instanceof AuthenticationError) console.error("API key 無效"); else if (error instanceof EntitlementError) console.error("方案權限不足"); else if (error instanceof InsufficientCreditsError) console.error("credits 不足"); else if (error instanceof DatasetNotFoundError) console.error("dataset 不存在"); else if (error instanceof UpstreamError) console.error("上游服務異常");}