Node SDK
Listings API 的官方 TypeScript 用戶端,適用於 Node 18+,無外部相依套件,支援自動分頁,並隨附型別定義。
listingsapi-js 是 Listings API v4 的官方 Node.js/TypeScript 用戶端。它透過單一扁平結構的用戶端管理地點、目錄商家資訊、評論、貼文、相片及商家檔案分析資料,並內建游標式分頁、自動 ID 編碼、重試及具型別的錯誤。
安裝
npm install listingsapi-js驗證身分
在 Listings API 開發者儀表板的 API Keys → New key 取得您的 API 金鑰。
import { ListingsAPI } from 'listingsapi-js'; const client = new ListingsAPI(); // reads LISTINGSAPI_KEY from envconst explicit = new ListingsAPI({ apiKey: 'your-api-key' }); // or pass it directly建構函式也接受 baseUrl(預設為 https://listingsapi.com)、timeout(預設為 240000 毫秒)及 maxRetries(預設為 2)。請參閱設定。
簡易範例
import { ListingsAPI } from 'listingsapi-js'; const client = new ListingsAPI(); const result = await client.fetchAllLocations({ first: 5 });if (!Array.isArray(result)) {for (const location of result.locations) { console.log(location.name, '--', location.city);}}資源
所有方法都位於用戶端實例上:
此 SDK 尚未封裝 Social API,涵蓋品牌、已連結的社群帳號,以及發布至 Facebook、Instagram、X、LinkedIn 和 Pinterest 的貼文;請依照 Social REST 參考文件直接呼叫。
分頁
傳回清單的方法支援游標式分頁。
// Single pageconst page = await client.fetchAllLocations({ first: 25 });if (!Array.isArray(page)) {console.log(page.locations); // Location[]console.log(page.pageInfo); // { hasNextPage, hasPreviousPage, startCursor, endCursor } // Next pageif (page.pageInfo.hasNextPage) { const next = await client.fetchAllLocations({ first: 25, after: page.pageInfo.endCursor!, });}} // Auto-paginate: returns a flat Location[]const all = await client.fetchAllLocations({ fetchAll: true, pageSize: 100 });searchLocations 和 fetchInteractions 採用相同模式:預設傳回分頁物件,使用 fetchAll: true 時則傳回扁平陣列。
ID 編碼
您可以在任何需要地點 ID 的地方傳入數字 ID:SDK 會自動將其編碼為 base64。已編碼的 ID 會原樣接受,編碼器也已匯出,供您需要時自行使用。
import { encodeLocationId } from 'listingsapi-js'; // These are equivalentawait client.fetchPremiumListings(16808);await client.fetchPremiumListings('TG9jYXRpb246MTY4MDg='); encodeLocationId(16808); // 'TG9jYXRpb246MTY4MDg='錯誤處理
所有 SDK 錯誤都衍生自 ListingsAPIError。API 發生錯誤時會擲回 APIError 或其子類別,無論平台是透過 HTTP 狀態還是 HTTP 200 回應主體回報錯誤(無效權杖及寫入驗證錯誤會以 errors[] 酬載傳回,寫入也可能回報 success: false)。SDK 會針對上述所有情況擲回錯誤,因此您無須自行檢查 success 旗標。網路連線失敗時會擲回 APIConnectionError。
每個 APIError 都包含 statusCode、responseBody、code(第一個平台錯誤代碼,e.g。"SY10005"),以及 errors(每個項目都正規化為 { code, message, context })。
import { ListingsAPI, APIError, RateLimitError } from 'listingsapi-js'; const client = new ListingsAPI(); try {await client.fetchAllLocations();} catch (error) {if (error instanceof RateLimitError) { console.log(error.retryAfter); // seconds to wait} else if (error instanceof APIError) { console.log(error.statusCode); // 401, 404, 500, or 200 for payload-level errors console.log(error.code); // first platform code, e.g. 'SY10005' console.log(error.errors); // [{ code, message, context }, ...]}}用戶端收到 429 和 5xx 回應時會自動重試(預設嘗試 2 次,並遵循 Retry-After)。重試模式與狀態碼說明請參閱錯誤處理。