# Reddit Comment Tree Scraper - Post Comments with Depth (`thenetaji/reddit-comment-tree-scraper`) Actor

Reads a Reddit post's comments from its link or its id and saves one flat row per comment, each carrying the depth and the parent Reddit states for it, so the thread rebuilds exactly. Every row also reports how much of the tree the run reached and how many branches were left unread.

- **URL**: https://apify.com/thenetaji/reddit-comment-tree-scraper.md
- **Developed by:** [The Netaji](https://apify.com/thenetaji) (community)
- **Categories:** Social media, Marketing, Automation
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $2.13 / 1,000 results

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.
Since this Actor supports Apify Store discounts, the price gets lower the higher subscription plan you have.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

Actors are web data automations that power AI and operations. They run on the Apify platform to scrape websites, process data, connect APIs, and automate workflows.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

- **AI agents and MCP clients** — the [Apify MCP server](https://docs.apify.com/integrations/mcp.md) at `https://mcp.apify.com` (remote, streamable HTTP, OAuth on first use).
- **Agentic workflows and local Actor development** — [Agent Skills](https://apify.com/.well-known/agent-skills/index.json) with the [Apify CLI](https://docs.apify.com/cli/docs.md): `npm install -g apify-cli`, then `apify login`.
- **JavaScript/TypeScript projects** — the official [JS/TS client](https://docs.apify.com/api/client/js/docs.md): `npm install apify-client`.
- **Python projects** — the official [Python client](https://docs.apify.com/api/client/python/docs.md): `pip install apify-client`.
- **Any other language** — the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).

# README

## Reddit Comment Tree Scraper

The Actor reads a Reddit post's comments and saves one flat record per comment, each carrying the
depth and the parent Reddit states for it, so the nested thread can be rebuilt exactly from the
saved rows. Every row also reports how much of the tree the run reached and how much was left, which
is the part of a comment dataset that is easiest to get wrong. No Reddit account, application, or
API credential is involved.

### Accepted input

| Field | Required | Default | Notes |
| --- | --- | --- | --- |
| `post_id` | yes | — | A base36 post id (`1vqcqpz`), a `t3_` fullname, or a pasted permalink. A permalink also supplies the community. |
| `subreddit` | no | — | Only needed when `post_id` is a bare id rather than a link. |
| `commentSort` | no | — | `top`, `new`, `controversial`, or `old`. Empty asks for Reddit's own default order. |
| `maxItems` | no | `100` | Comments to save. A tree is read in one request, so the effective ceiling is 200. |

Pasting the post's link is the reliable form. A post is addressed by its id together with the
community it was posted in, and a link carries both in one string, so they cannot contradict each
other. An id paired with the wrong community does not return an empty result; it fails. If both are
supplied and they disagree, the link wins and the run log says so.

```json
{
  "post_id": "https://www.reddit.com/r/technology/comments/1vqcqpz/example_title/",
  "commentSort": "top",
  "maxItems": 200
}
```

### Response fields

Each saved record carries `kind`, `id`, `fullname`, `body`, `author`, `author_icon`, `score`,
`created_at`, `permalink`, `award_count`, `subreddit`, `post_id`, `post_fullname`,
`parent_fullname`, `depth`, `position`, `parent_positions`, `is_deleted`, `is_collapsed`,
`total_comments`, `tree_complete`, `unfollowed_branches`, and `truncated_threads`.

```json
{
  "kind": "comment",
  "id": "p44qdpd",
  "fullname": "t1_p44qdpd",
  "body": "This is the comment text.",
  "author": "example_user",
  "score": 42,
  "created_at": "2026-08-14T09:41:07.000Z",
  "permalink": "/r/technology/comments/1vqcqpz/example_title/p44qdpd/",
  "post_id": "1vqcqpz",
  "post_fullname": "t3_1vqcqpz",
  "parent_fullname": "t1_ixbo2n8",
  "depth": 1,
  "position": 3,
  "parent_positions": [1],
  "total_comments": 464,
  "tree_complete": false,
  "unfollowed_branches": 7,
  "truncated_threads": 2
}
```

### Rebuilding the thread

Comments are returned flat, in reading order, and the nesting is stated rather than implied. `depth`
is Reddit's own value for the comment, not something inferred from indentation, and
`parent_fullname` names the comment or post it replies to. Grouping rows by `parent_fullname` and
ordering each group by `position` reproduces the thread exactly. `parent_positions` gives the same
location as a path from the top of the tree, for consumers that prefer to sort on one column.

### How much of the tree a run reaches

A comment tree is read in branches rather than in pages. Reddit serves the top-level batch together
with a separate continuation for each collapsed branch beneath it — a large thread can hand back
nineteen of them at once — and following one branch to its end says nothing about the other
eighteen. The Actor works through them in turn until `maxItems` is reached or the branches run out.

The result is that a run routinely returns part of the tree rather than all of it, and a partial
tree that reads as complete is the failure this Actor exists to avoid. Four columns on every saved
row make the difference visible without reference to the run log:

- `total_comments` is Reddit's own total for the post. It is the denominator, and it is normally
  larger than the number of rows saved.
- `tree_complete` is `true` only when every branch was read.
- `unfollowed_branches` counts branches still waiting when the run stopped. A non-zero value means a
  larger `maxItems` would return more comments.
- `truncated_threads` counts sub-threads Reddit links to by address only, giving no way to continue
  them from inside the tree. These are not reachable at any value of `maxItems`; each needs its own
  run against the address in the sub-thread's own permalink.

The run log states the same thing in words, and warns rather than informs when the tree was left
incomplete.

### Sort order

`commentSort` accepts `top`, `new`, `controversial`, and `old`, and all four were checked against
the same post and returned content. Leaving it empty is not the same as choosing one: Reddit applies
its own default order to a thread nobody has re-sorted, and there is no value in the list that names
that order, so it is asked for by omitting the field rather than by guessing at a spelling for it.

Sort order changes which branches are read first, and therefore which comments a bounded run
returns. A run of 50 comments sorted by `top` and one sorted by `new` will not overlap much.

### Deleted comments and absent fields

A comment Reddit reports as deleted keeps its position in the tree and keeps its replies, so it is
saved rather than dropped, with `is_deleted` set, `body` `null`, and no author. Removing it would
break the parent chain of everything beneath it. `is_collapsed` reports whether Reddit renders the
comment collapsed by default. Fields absent from the source are returned as `null` rather than
omitted, so the column set is the same in every record.

### Failures and partial results

A post value that cannot be reduced to an id is rejected before any request is made, and a bare id
with no community is rejected with a message saying why an id alone does not address a post. A post
with no comments ends the run cleanly rather than failing — an empty thread is an answer.

### Cost

Charging is per comment saved to the dataset, plus the platform's one-off Actor start event. Reading
deeper into a large tree costs more because it saves more, so `maxItems` is the direct control on
what a run costs.

### Related Actors

- **Reddit Post Details Scraper** — the post itself: title, body, media, score, and Reddit's comment
  total, without the comments.
- **Reddit Subreddit Feed Scraper** — the posts of a community, to find threads worth reading.
- **Reddit User Comment History Scraper** — one account's comments across every thread, rather than
  one thread's comments across every account.

### Support

Questions, bug reports, and requests for a field that is not here yet: thenetaji@proton.me.

# Actor input Schema

## `post_id` (type: `string`):

The post to read. A base36 post id (1vqcqpz), a t3\_ fullname, or a pasted permalink are all accepted. A permalink is the safest form: the post id and the subreddit are read out of the same string and cannot contradict each other, and it makes the Subreddit field unnecessary.

## `subreddit` (type: `string`):

The subreddit to read. A bare name (AskReddit), a prefixed name (r/AskReddit), or a pasted reddit.com URL are all accepted; a URL is reduced to the subreddit it names, and any sort or time in that URL is ignored in favour of the fields below.

## `commentSort` (type: `string`):

The order the comment tree is read in. Leave it empty to take Reddit's own default order, which is the one reddit.com applies to a thread nobody has re-sorted.

## `maxItems` (type: `integer`):

The maximum number of records to save. A value of 0 removes the bound and the run continues until the source reports it has no more to give.

## Actor input object example

```json
{
  "post_id": "https://www.reddit.com/r/technology/comments/1vqcqpz/example_title/",
  "subreddit": "AskReddit",
  "maxItems": 25
}
```

# Actor output Schema

## `dataset` (type: `string`):

All records scraped by this run

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "post_id": "https://www.reddit.com/r/technology/comments/1vqcqpz/example_title/",
    "subreddit": "AskReddit",
    "maxItems": 25
};

// Run the Actor and wait for it to finish
const run = await client.actor("thenetaji/reddit-comment-tree-scraper").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {
    "post_id": "https://www.reddit.com/r/technology/comments/1vqcqpz/example_title/",
    "subreddit": "AskReddit",
    "maxItems": 25,
}

# Run the Actor and wait for it to finish
run = client.actor("thenetaji/reddit-comment-tree-scraper").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "post_id": "https://www.reddit.com/r/technology/comments/1vqcqpz/example_title/",
  "subreddit": "AskReddit",
  "maxItems": 25
}' |
apify call thenetaji/reddit-comment-tree-scraper --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,thenetaji/reddit-comment-tree-scraper"
        }
    }
}

```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/Lv2nOJWTLTF9Em3ZI/builds/fmdCZbzPIUCO799P4/openapi.json
