<!-- GENERATED BY scripts/gen_social_pages.py; DO NOT EDIT -->
# Collect comments with bounded pagination

A working Python pattern for Douyin comments: cap the number of paid requests, keep cursors intact, remove duplicate comment IDs and stop when no new data arrives.

Canonical: https://chinaapi.ai/social-data-api/guides/comment-pagination/
Catalog captured: 2026-10-29T17:56:18Z
Price and availability source: https://dash.chinaapi.ai/api/social/catalog?lang=en&utm_source=chinaapi&utm_medium=social-catalog&utm_campaign=social-data-beta

[Create an account](https://dash.chinaapi.ai/register?lang=en&utm_source=chinaapi&utm_medium=social-social-data-api-guides-comment-pagination&utm_campaign=social-data-beta)

## Set a spending boundary first

Choose a maximum page count and multiply it by the live catalog price before starting. A successful page is a billable call even when it overlaps a previous page. The example deliberately stops on errors rather than hiding extra retries.

## Choose the correct pagination contract

The example below is for Douyin comments with count 20. Comment replies also need a parent comment_id. Other platforms use different cursor or page fields. Threads comments and top search currently support the first page only; nonempty end_cursor is rejected before billing.

## Persist checkpoints for deliberate resumptions

Store the video ID, capability, last cursor and seen IDs together. Resume only after deciding whether another paid request is needed. When changing endpoint implementations, start a new cursor chain instead of mixing pagination formats.

## Request example

```python
import os
import requests

base = "https://api.chinaapi.ai"
headers = {"Authorization": "Bearer " + os.environ["CHINAAPI_API_KEY"]}
video_id = "YOUR_VIDEO_ID"
seen_ids, seen_cursors = set(), set()
cursor = 0
max_pages = 3  # Budget before starting: max_pages × catalog call price.
for _ in range(max_pages):
    response = requests.get(base + "/v1/social/douyin/comments",
        headers=headers, params={"aweme_id": video_id, "cursor": cursor, "count": 20},
        timeout=45)
    if not response.ok:
        error = response.json().get("error", {})
        if error.get("code") == "social_data_no_result":
            break
        response.raise_for_status()  # Stop; do not retry in a tight loop.
    data = response.json()["data"]
    new_items = []
    for item in data.get("comments", []):
        cid = str(item.get("cid", ""))
        if cid and cid not in seen_ids:
            seen_ids.add(cid)
            new_items.append(item)
    for item in new_items:
        print(item)
    next_cursor = data.get("cursor")
    if not data.get("has_more") or not new_items or next_cursor is None:
        break
    marker = str(next_cursor)
    if marker in seen_cursors or marker == str(cursor):
        break
    seen_cursors.add(str(cursor))
    cursor = next_cursor

```

Keep the key server-side. Replace resource identifiers before running.

## Billing and limits

First top-up required; trial credit is excluded. No SLA. Synchronous empty or failed results are not charged. Every successful page is separately billed. Follow platform terms; personal profiling and private-content access are excluded.

[Full reference](https://chinaapi.ai/social-data-api/docs/)

## Related guides

- [Douyin API: video search, comments and replies](https://chinaapi.ai/social-data-api/douyin/index.md)
- [Bilibili API: search videos and retrieve public discussion](https://chinaapi.ai/social-data-api/bilibili/index.md)
- [Weibo API: keyword search, posts, comments and trends](https://chinaapi.ai/social-data-api/weibo/index.md)
- [Xiaohongshu API: search notes, details and comments](https://chinaapi.ai/social-data-api/xiaohongshu/index.md)
- [Collect comments with bounded pagination](https://chinaapi.ai/social-data-api/guides/comment-pagination/index.md)
- [Build an MCP social research workflow](https://chinaapi.ai/social-data-api/guides/mcp-research/index.md)
