> ## Documentation Index
> Fetch the complete documentation index at: https://social-b97141fb-auto-generate-llmstxt.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Query Comments

> Query comments for posts, stories, or custom content with pagination, parent filtering, deleted-state filtering, and sort order.

Use comment queries when your app needs a paged list for a post, story, or custom content item. Query top-level comments for the main thread, then query replies with `parentId` when a user expands a comment.

<Info>
  Query APIs return paged/live collection results on TypeScript, iOS, Android, and Flutter. For a single known comment ID, use [Get Comment](/social-plus-sdk/social/content-management/comments/retrieval/get-comment).
</Info>

## Parameters

| Operation      | Parameter            | Required | Description                                                                                  |
| -------------- | -------------------- | -------- | -------------------------------------------------------------------------------------------- |
| Query comments | `referenceId`        | Yes      | ID of the post, story, or custom content item.                                               |
| Query comments | `referenceType`      | Yes      | Target content type, such as `post`, `story`, or `content`.                                  |
| Query comments | `parentId`           | No       | `null` / `nil` for top-level comments, a comment ID for replies, or omitted where supported. |
| Query comments | `includeDeleted`     | No       | Include soft-deleted comments for moderation or audit views.                                 |
| Query comments | `dataTypes`          | No       | Filter comments by content type, such as text or image.                                      |
| Query comments | `sortBy` / `orderBy` | No       | Newest-first or oldest-first comment order.                                                  |
| Query comments | `pageSize` / `limit` | No       | Number of comments to load per page.                                                         |

## Query Options

| Option               | Description                                                                                                                   |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `referenceId`        | ID of the post, story, or custom content item                                                                                 |
| `referenceType`      | `post`, `story`, or `content`                                                                                                 |
| `parentId`           | `null` / `nil` for top-level comments; a comment ID for replies; omit the parent filter to include all levels where supported |
| `includeDeleted`     | Include soft-deleted comments when building moderation or audit views                                                         |
| `dataTypes`          | Filter comments by content type, such as text or image                                                                        |
| `sortBy` / `orderBy` | Newest-first or oldest-first comment order                                                                                    |
| `pageSize` / `limit` | Number of comments to load per page                                                                                           |

## Query Top-Level Comments

Set the parent filter to `null` / `nil` when you only want root comments.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { CommentRepository } from "@amityco/ts-sdk";

  const unsubscribe = CommentRepository.getComments(
    {
      referenceType: "post",
      referenceId: postId,
      parentId: null,
      sortBy: "lastCreated",
      pageSize: 10,
    },
    ({ data: comments, loading, error }) => {
      if (loading) return;

      if (error) {
        handleError(error);
        return;
      }

      renderResults(comments);
    },
  );
  ```

  ```swift iOS theme={null}
  let queryOptions = AmityCommentQueryOptions(
      referenceId: "post-id",
      referenceType: .post,
      filterByParentId: true,
      parentId: nil,
      orderBy: .descending,
      includeDeleted: false,
      pageSize: 20
  )

  token = commentRepository
      .getComments(with: queryOptions)
      .observe { collection, error in
          if let error {
              handleError(error)
              return
          }

          showSuccessMessage(collection.snapshots.count)
      }
  ```

  ```kotlin Android theme={null}
  import com.amity.socialcloud.sdk.api.social.comment.query.AmityCommentSortOption

  AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId = postId)
      .parentId(parentId = null)
      .sortBy(AmityCommentSortOption.LAST_CREATED)
      .includeDeleted(includeDeleted = false)
      .pageSize(pageSize = 15)
      .build()
      .query()
      .subscribe(
          { pagingData -> getPagingData(pagingData) },
          { error -> handleGeneralError(error) }
      )
  ```

  ```dart Flutter theme={null}
  final liveCollection = AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId)
      .parentId(null)
      .sortBy(AmityCommentSortOption.LAST_CREATED)
      .includeDeleted(false)
      .getLiveCollection(pageSize: 20);

  final subscription = liveCollection.getStreamController().stream.listen(
    (comments) {
      final visibleCommentCount = comments.length;
    },
    onError: (error) {
      showError(error);
    },
  );

  liveCollection.loadNext();
  await subscription.cancel();
  ```
</CodeGroup>

## Query Replies

Set `parentId` to an existing comment ID to load replies for that comment.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { CommentRepository } from "@amityco/ts-sdk";

  const unsubscribe = CommentRepository.getComments(
    {
      referenceType: "post",
      referenceId: postId,
      parentId: commentId,
      sortBy: "firstCreated",
      pageSize: 5,
    },
    ({ data: replies, loading, error }) => {
      if (loading) return;

      if (error) {
        handleError(error);
        return;
      }

      renderResults(replies);
    },
  );
  ```

  ```swift iOS theme={null}
  let replyQueryOptions = AmityCommentQueryOptions(
      referenceId: "post-id",
      referenceType: .post,
      filterByParentId: true,
      parentId: "parent-comment-id",
      orderBy: .ascending,
      includeDeleted: false,
      pageSize: 5
  )

  token = commentRepository
      .getComments(with: replyQueryOptions)
      .observe { collection, error in
          if let error {
              handleError(error)
              return
          }

          showSuccessMessage(collection.snapshots.count)
      }
  ```

  ```kotlin Android theme={null}
  import com.amity.socialcloud.sdk.api.social.comment.query.AmityCommentSortOption

  AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId = postId)
      .parentId(parentId = commentId)
      .sortBy(AmityCommentSortOption.FIRST_CREATED)
      .includeDeleted(includeDeleted = false)
      .pageSize(pageSize = 5)
      .build()
      .query()
      .subscribe(
          { pagingData -> getPagingData(pagingData) },
          { error -> handleGeneralError(error) }
      )
  ```

  ```dart Flutter theme={null}
  final replies = await AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId)
      .parentId(commentId)
      .sortBy(AmityCommentSortOption.FIRST_CREATED)
      .includeDeleted(false)
      .query(limit: 5);

  final replyCount = replies.length;
  ```
</CodeGroup>

## Filter by Data Type

Use data type filters when your UI needs a specific type of comment, such as image-only comments.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { CommentRepository } from "@amityco/ts-sdk";

  const unsubscribe = CommentRepository.getComments(
    {
      referenceType: "post",
      referenceId: postId,
      dataTypes: {
        values: ["image"],
        matchType: "exact",
      },
      pageSize: 10,
    },
    ({ data: imageComments, error }) => {
      if (error) {
        handleError(error);
        return;
      }

      renderResults(imageComments);
    },
  );
  ```

  ```swift iOS theme={null}
  let imageOnlyOptions = AmityCommentQueryOptions(
      referenceId: "post-id",
      referenceType: .post,
      filterByParentId: false,
      dataTypes: .exact([.image]),
      orderBy: .descending,
      includeDeleted: false,
      pageSize: 20
  )

  token = commentRepository
      .getComments(with: imageOnlyOptions)
      .observe { collection, error in
          if let error {
              handleError(error)
              return
          }

          showSuccessMessage(collection.snapshots.count)
      }
  ```

  ```kotlin Android theme={null}
  import com.amity.socialcloud.sdk.api.social.comment.query.AmityCommentDataTypeFilter
  import com.amity.socialcloud.sdk.api.social.comment.query.AmityCommentSortOption

  AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId = postId)
      .dataTypes(
          AmityCommentDataTypeFilter.Exact(
              dataTypes = listOf(AmityComment.DataType.IMAGE)
          )
      )
      .sortBy(AmityCommentSortOption.LAST_CREATED)
      .includeDeleted(includeDeleted = false)
      .pageSize(pageSize = 10)
      .build()
      .query()
      .subscribe(
          { pagingData -> getPagingData(pagingData) },
          { error -> handleGeneralError(error) }
      )
  ```

  ```dart Flutter theme={null}
  final imageComments = await AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId)
      .dataTypes(
        AmityCommentDataTypeFilter.exact(dataTypes: [AmityDataType.IMAGE]),
      )
      .sortBy(AmityCommentSortOption.LAST_CREATED)
      .includeDeleted(false)
      .query(limit: 10);

  final imageCommentCount = imageComments.length;
  ```
</CodeGroup>

## Reference Targets

| Target  | TypeScript                 | iOS        | Android                     | Flutter               |
| ------- | -------------------------- | ---------- | --------------------------- | --------------------- |
| Post    | `referenceType: "post"`    | `.post`    | `.post(postId = ...)`       | `.post(postId)`       |
| Story   | `referenceType: "story"`   | `.story`   | `.story(storyId = ...)`     | `.story(storyId)`     |
| Content | `referenceType: "content"` | `.content` | `.content(contentId = ...)` | `.content(contentId)` |

## Notes

* Keep `referenceId` and `referenceType` consistent between the parent comment and its replies.
* Use smaller page sizes for reply threads than for top-level comments.
* `includeDeleted: true` is mainly for moderation, audit, or admin-style views; most end-user views should exclude deleted comments.
* Dispose of observers, subscriptions, or live collections when the screen is no longer active.

## Related Topics

<CardGroup cols={2}>
  <Card title="Get Comment" href="/social-plus-sdk/social/content-management/comments/retrieval/get-comment" icon="message">
    Retrieve a specific comment by ID
  </Card>

  <Card title="Text Comment" href="/social-plus-sdk/social/content-management/comments/creation/text-comment" icon="message-square">
    Create top-level comments and replies
  </Card>
</CardGroup>
