快速入門

連線至 Listings API、列出您的地點、透過單次呼叫建立地點,並回覆評論。

1. 設定您的 API 金鑰

在 Listings API 開發者儀表板中,從 API 金鑰 → 新增金鑰取得您的金鑰。SDK 會自動從 LISTINGSAPI_KEY 環境變數讀取金鑰。

bash
export LISTINGSAPI_KEY="your-api-key"

2. 列出您的地點

quickstart.py
import listingsapi client = listingsapi.ListingsAPI() page = client.locations.list(first=5)for loc in page:  print(loc.name, "--", loc.city, loc.stateIso) print(f"\nShowing {len(page)} locations, has_more={page.has_more}")
bash
python quickstart.py

預期輸出:

bash
Acme Dental Downtown -- New York NYAcme Dental Midtown -- New York NYAcme Dental Brooklyn -- Brooklyn NY Showing 3 locations, has_more=False

若要自動請求所有分頁,請使用 client.locations.list(first=100).auto_paging_iter();它會依游標逐頁取得資料,您不必手動管理游標。

3. 透過一次呼叫建立地點

locations.add() 接受以關鍵字引數傳入的所有必填欄位,並在發出任何網路呼叫前於用戶端進行驗證(名稱長度、描述至少 200 個字元、類別、國家、城市),因此無效的酬載會立即失敗,並提供明確的 ValidationError。

Python
import listingsapi client = listingsapi.ListingsAPI() result = client.locations.add(  name="Acme Dental Uptown",  description=(      "Acme Dental Uptown is a family-owned dental practice on the Upper "      "West Side of New York offering preventive care, cosmetic dentistry, "      "orthodontics, and emergency appointments. Our board-certified team "      "combines modern equipment with a gentle, patient-first approach."  ),  sub_category_id=1432,  country_iso="US",  city="New York",  street="482 Columbus Ave",  state_iso="NY",  postal_code="10024",  phone="6443859313",) print(result.location.id)print(result.location.name)

發布平台會將描述用作商家資訊的主要文案,因此 API 要求描述至少包含 200 個字元。請從 client.subcategories() 取得有效的 sub_category_id 值。

新 ID 會以 base64 字串傳回,例如 TG9jYXRpb246MTY4MDg=。凡是 SDK 接受地點 ID 的地方,都可以互換使用數字 ID 與 base64 ID,因此 client.locations.retrieve(16808) 和 client.locations.retrieve("TG9jYXRpb246MTY4MDg=") 會取得同一個地點。

4. 回覆評論

列出某個地點的評論,再使用評論的 interactionId 回覆:

Python
import listingsapi client = listingsapi.ListingsAPI() reviews = client.reviews.list(16808, first=10, rating_filters=[4, 5])for review in reviews:  print(review.rating, "-", review.get("content")) # A page can come back empty, and not every review is repliable: some sites# allow one owner reply per review, and some are read-only for the account.repliable = [r for r in reviews if r.get("canRespond")]if not repliable:  print("nothing to reply to")else:  client.reviews.respond(      interaction_id=repliable[0].interactionId,      content="Thank you for the kind review! We look forward to seeing you again soon.",  )

回覆前請先檢查 canRespond,且務必先確認分頁不是空的,才能透過索引存取其中的項目:對空分頁使用 reviews[0] 會引發 IndexError,而回覆來源平台已關閉的評論時,會在發布平台端失敗,而非在 API 端失敗。

如果平台拒絕回覆,即使 HTTP 狀態為 200,SDK 仍會引發 ValidationError;您不必自行檢查 success 旗標。

5. 處理錯誤

Python
import listingsapi try:  client = listingsapi.ListingsAPI(api_key="bad-key")  client.locations.list()except listingsapi.AuthenticationError as e:  print("Invalid key:", e.code)

金鑰無效時,API 會傳回 HTTP 401,並附上平台代碼 SY90005;SDK 會引發 AuthenticationError。如果相同的錯誤代碼出現在 200 本文中,SDK 也會進行防禦性處理。請參閱錯誤處理以瞭解完整的例外階層,以及錯誤代碼參考以查看 HTTP 狀態對照表。

後續步驟