---
title: "Programmatic comments and review"
canonical_url: "https://www.nutrient.io/guides/document-authoring/review-and-collaboration/programmatic-comments/"
md_url: "https://www.nutrient.io/guides/document-authoring/review-and-collaboration/programmatic-comments.md"
last_updated: "2026-07-08T00:00:00.000Z"
description: "Query, create, and resolve comment threads in Document Authoring through transactions, and author document edits as tracked revisions."
---

# Manage comments and review edits from code

Document Authoring includes an in-editor comment UI, and it also enables you to manage comments from code. Inside a [transaction](https://www.nutrient.io/guides/document-authoring/editing-content/programmatic-editing.md), you can query, create, and resolve comment threads. You can also author edits as tracked revisions.

Before you start, make sure the Document Authoring library is installed and running in your app. If you haven’t set it up yet, refer to the [getting started](https://www.nutrient.io/sdk/document-authoring/getting-started.md) guide.

The examples omit error handling. Add it before you use this code in production.

## Query comment threads

Use [`CommentThreadCollection`](https://www.nutrient.io/api/document-authoring/types/programmatic/commentthreadcollection/) to read comment threads from the draft document:

```js

const currentDoc = editor.currentDocument();

await currentDoc.transaction(async ({ draft }) => {
  const openThreads = draft.commentThreads().all({ status: 'open' });

  for (const thread of openThreads) {
    console.log(thread.comment.text);
  }

  return false;
});

```

## Create a comment thread

Create a text-anchored comment thread by passing a comment body and one or more text anchor ranges:

```js

const currentDoc = editor.currentDocument();

await currentDoc.transaction(async ({ draft }) => {
  const paragraph = draft.body().content().addParagraph();
  const textView = paragraph.asTextView();

  textView.setText('The supplier must provide reviewed text before signing.');

  const match = textView.searchText('reviewed text');

  if (!match) {
    return false;
  }

  draft.commentThreads().add({
    body: 'Can Legal confirm this wording?',
    anchor: {
      type: 'text',
      ranges: [{ textView, range: match.range }],
    },
  });

  return true;
});

```

## Manage a thread

After you query or create a thread, use [`CommentThread`](https://www.nutrient.io/api/document-authoring/types/programmatic/commentthread/) methods to manage it:

- `reply(body)`

- `edit(body)`

- `resolve()`

- `unresolve()`

- `remove()`

```js

await currentDoc.transaction(async ({ draft }) => {
  const [thread] = draft.commentThreads().all({ status: 'open' });

  if (thread) {
    thread.reply('Checked by automated review.');
    thread.resolve();
  }

  return true;
});

```

## Author edits as tracked changes

Use [`TransactionOptions`](https://www.nutrient.io/api/document-authoring/types/transactionoptions/) `review` to run supported mutations in review mode. Document Authoring records those mutations as tracked revisions for the supplied author:

```js

const currentDoc = editor.currentDocument();

await currentDoc.transaction(
  async ({ draft }) => {
    const paragraph = draft.body().content().addParagraph();
    paragraph.asTextView().setText('This change is authored in review mode.');

    return true;
  },
  {
    review: {
      author: 'Nutrient Docs',
    },
  },
);

```

Supported mutations create revisions for the supplied author. Mutations without tracked-change support still apply directly. Comment-thread changes also work inside a review transaction, but they don’t create tracked-change revisions.

## Learn more

**[Tracked changes and editor modes](https://www.nutrient.io/guides/document-authoring/review-and-collaboration/tracked-changes-and-editor-modes.md)**\
Use review mode and tracked changes in the editor.

**[Comments and review workflows](https://www.nutrient.io/guides/document-authoring/review-and-collaboration/comments-and-review-workflows.md)**\
Use the in-editor comment experience.

**[Programmatic editing](https://www.nutrient.io/guides/document-authoring/editing-content/programmatic-editing.md)**\
Learn how transactions work and how to use the document model.
---

## Related pages

- [Review documents with comments](/guides/document-authoring/review-and-collaboration/comments-and-review-workflows.md)
- [Use tracked changes and editor modes](/guides/document-authoring/review-and-collaboration/tracked-changes-and-editor-modes.md)

