> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.beatapi.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.beatapi.io/_mcp/server.

# How do I sort Reddit comments by newest and retain reply structure?

> Use post_id, sort_type, commentForest and after without mixing comment and reply cursors.

To read the newest comments on a Reddit post first, call `reddit.app.fetch_post_comments` with `sort_type:"NEW"`. A URL containing `/comments/abc123/` means `post_id:"t3_abc123"`. When the comment tree has another page, pass the returned `more.cursor` as `after` in the next request. Save each tree entry's `parentId` to keep the reply structure.

## Which sort should I choose?

Use `NEW` for newest first. `sort_type` accepts `CONFIDENCE` (default), `NEW`, `TOP`, `HOT`, `CONTROVERSIAL`, `OLD` and `RANDOM`, and the values are case-sensitive. `TOP` sorts comments by score; `TOP` does not limit the results to top-level comments. Set `need_format` to false to get the raw tree structure this page describes.

```bash
mkdir reddit-query-1 && \
curl --fail-with-body -sS https://api.beatapi.io/v1/social-data/call \
  -H "Authorization: Bearer $BEATAPI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"action":"reddit.app.fetch_post_comments","params":{"post_id":"t3_abc123","sort_type":"NEW","need_format":false,"after":""}}' \
  -D reddit-query-1/response.headers \
  -o reddit-query-1/reddit-comments.json
```

## Where are the text, score and reply relationships?

First check that the top-level `status` is `succeeded`. Then read each entry of `data.postInfoById.commentForest.trees[]`:

| Data                                             | Field                                                  |
| ------------------------------------------------ | ------------------------------------------------------ |
| Comment text                                     | `node.content.markdown`                                |
| Comment ID                                       | `node.id`                                              |
| Score: upvotes minus downvotes, not a like count | `node.score`                                           |
| Source link                                      | `node.permalink`                                       |
| Reply structure                                  | `parentId`, `depth` and `childCount` on the tree entry |

`depth=0` marks a top-level comment. `trees[]` is a flat list of every returned node, so `trees[]` holds replies as well as top-level comments. A `childCount` value does not mean the response contains that many replies; count the replies you actually received.

## Where does after come from?

Read `data.postInfoById.commentForest.pageInfo.hasNextPage`:

* `false`: the comment list has no next page.
* `true`: take `more.cursor` from the last entry of `trees[]` and pass the cursor as `after`. Keep `post_id` and `sort_type` unchanged.

Pass the cursor exactly as returned. Do not increment the cursor, and do not pass a search `endCursor` or another reply action's cursor as `after`.

When `hasNextPage` is true but `more.cursor` is empty, stop and save the `request_id`; do not replay page one. All four historical samples recorded by the backend have a null `more`, so your code must handle the empty-cursor case instead of guessing a field. Also stop on a repeated cursor, a business failure or when you reach your page budget. Reaching the page budget ends your batch; the thread can still have more comments.

The CSV exporter `export_comments.py` from the comment export guide keeps every valid comment the exporter already received and reports one stop reason:

* `missing_usable_cursor`: `hasNextPage` is true but no usable cursor came back.
* `provider_end`: the comment-list pagination ended; unexpanded reply branches can remain.
* `next_page_available`: a cursor exists. Requesting the next page is a separate charged call that you decide on.

The exporter always sets `all_comments_collected` to false. Within one response, two different nodes with the same comment ID stop the export. Changes between separately saved pages stay in the merge step's observation table.

## Does the comment tree contain every reply?

No. The tree contains only the nodes returned in the response; hidden or removed content, later pages and unexpanded branches are not in the tree. Save `node.isRemoved` and never reconstruct removed text. Count and deduplicate by actual comment ID. When a reply's parent is not on the same comment page, keep the reply's `parentId` for a later merge instead of turning the reply into a top-level comment.

[Export comments and relationships](/guides/reddit-comment-export) · [Reddit questions](/faq/reddit)

Sources: the input contract for `data:reddit.app.fetch_post_comments`, the backend output structure from four samples dated 2026-09-24, and the gateway's current renderer.

After export, the [reviewed question-backlog workflow](/guides/reddit-comment-export) counts top-level comments and replies separately and checks each supporting excerpt against the original response. A reply whose parent is missing stays a reply; do not count the reply as an independent top-level complaint. A comment count is not the share of customers who have the problem.

## Prepare the correct post request from a copied link

Use the ID right after `/comments/` as the post ID, not the last path segment: a comment permalink contains the post ID and, later, a comment ID. `t3_` marks a post and `t1_` marks a comment; replacing a comment ID's prefix does not produce a post ID. [Reddit’s thing-ID documentation](https://developers.reddit.com/docs/capabilities/server/reddit-api) describes both prefixes.

Save the script below as `reddit_post_request.py`. The script accepts a lowercase `t3_` fullname, or a standard HTTPS post or comment permalink on reddit.com, [www.reddit.com](http://www.reddit.com), old.reddit.com or new\.reddit.com. The script rejects a bare ID because a bare ID does not say whether the ID names a post or a comment. Open a `/s/` sharing link, a shortened link, a community page or an external URL first, and copy the final post URL; the script does not follow redirects and makes no paid calls.

```bash
python3 reddit_post_request.py 'https://www.reddit.com/r/example/comments/abc123/title/def456/' reddit-request
```

The IDs in the command are examples. For the example link, `request.json` targets `t3_abc123`, and `input-audit.json` records that the input pointed at a comment. The request reads the first comment page of the containing post; the request does not fetch only comment `def456` and does not keep a share link’s on-page filter. The request sets NEW order, the raw structure, an empty first-page `after` and en-US. The en-US language preference does not filter authors or content to English. The script refuses to write into an existing output directory.

```python
import json
import re
import sys
from pathlib import Path
from urllib.parse import urlsplit

value, output = sys.argv[1:]
post_id = value
comment_link = False
if not re.fullmatch(r"t3_[a-z0-9]{1,17}", value):
    url = urlsplit(value)
    if url.scheme != "https" or url.hostname not in {"reddit.com", "www.reddit.com", "old.reddit.com", "new.reddit.com"} or url.username or url.password or url.port not in (None, 443):
        raise SystemExit("Use an HTTPS Reddit post permalink or an explicit t3_ fullname")
    match = re.fullmatch(r"/(?:r/[A-Za-z0-9_]+/)?comments/([a-z0-9]{1,17})(?:/([^/]+)(?:/([a-z0-9]+))?)?/?", url.path)
    if not match:
        raise SystemExit("No supported post permalink; open shared/short links and copy the final post URL")
    post_id = "t3_" + match.group(1)
    comment_link = match.group(3) is not None
request = {"action": "reddit.app.fetch_post_comments", "params": {
    "post_id": post_id, "sort_type": "NEW", "need_format": False, "after": "", "language": "en-US"}}
folder = Path(output)
folder.mkdir(exist_ok=False)
(folder / "request.json").write_text(json.dumps(request, indent=2), encoding="utf-8")
(folder / "input-audit.json").write_text(json.dumps({
    "original_input": value, "post_id": post_id, "input_points_to_comment": comment_link,
    "scope": "first page of the containing post, not a selected comment or all replies",
    "post_exists_verified": False, "requests_executed": 0}, indent=2), encoding="utf-8")
print(json.dumps({"post_id": post_id, "requests_prepared": 1, "requests_executed": 0}))
```

Check the current price of `reddit.app.fetch_post_comments` with Inspect before you send the generated body. When this page was written, Inspect listed the first comment page at `$0.003`; preparing the JSON is free, and each later page is a separate call. After the cost is approved, send `request.json` to `/v1/social-data/call` with your own Bearer key and save the full response. Then follow the [CSV export and reviewed backlog](/guides/reddit-comment-export). The script checks link syntax only; the API response shows whether the post exists, is public and has retrievable comments.