快速入门

连接 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)

使用无效密钥时,返回的响应为 HTTP 401,并包含平台错误码 SY90005;SDK 会引发 AuthenticationError。如果同一错误码出现在 200 响应体中,SDK 也会进行防御性处理。有关完整的异常层级结构,请参阅错误处理;有关 HTTP 状态码对照表,请参阅错误码参考。

后续步骤