Manage comments and replies

Comments are user-provided feedback on a file, such as a reader of a word-processing document suggesting how to rephrase a sentence. There are two types of comments: anchored comments and unanchored comments. An anchored comment is associated with a specific location, such as a sentence in a word-processing document, within a specific version of a document. Conversely, an unanchored comment is just associated with the document.

Replies are attached to comments and represent a user's response to the comment. The Drive API lets your users add comments and replies to documents created by your app. Collectively, a comment with replies is known as a discussion.

Setting the fields parameter is required to list the fields to return in the response when calling every method listed on the comments resource. If you omit the parameter, the method returns an error. To return the exact fields you need, see Return specific fields.

Add an unanchored comment

To add an unanchored comment to a document, call the create() method with the fileId parameter and a comments resource containing the comment.

The comment is inserted as plain text, but the response body provides an htmlContent field containing content formatted for display.

Add a reply to a comment

To add a reply to a comment, use the replies.create() method on the replies resource with the fileId and commentId parameters. The request body uses the content field to add the reply.

The reply is inserted as plain text, but the response body provides an htmlContent field containing content formatted for display.

The method returns the fields listed in the fields field.

Show an example

Request

In this example, we provide the fileId and commentId path parameters and multiple fields.

POST https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID/replies?fields=id,comment

Request body

{
  "content": "This is a reply to a comment."
}

Resolve a comment

A comment can only be resolved by posting a reply to a comment.

To resolve a comment, use the replies.create() method on the replies resource with the fileId and commentId parameters.

The request body uses the action field to resolve the comment. You can also set the content field to add a reply that closes the comment.

When a comment is resolved, Drive marks the comment resource as resolved: true. Unlike deleted comments, resolved comments can include the htmlContent or content fields.

When your app resolves a comment, your UI should indicate that the comment has been addressed. For example, your app might:

  • Disallow further replies and dim all previous replies plus the original comment.
  • Hide resolved comments.

Show an example

Request

In this example, we provide the fileId and commentId path parameters and multiple fields.

POST https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID/replies?fields=id,comment

Request body

{
  "action": "resolve",
  "content": "This comment has been resolved."
}

Add an anchored comment to the latest revision of a document

When you add a comment, you might want to anchor it to a region in the file. An anchor defines the file revision and region in a file to which a comment refers. The comments resource defines the anchor field as a JSON string.

To add an anchored comment:

  1. (Optional). Call the revisions.list() method to list every revisionID for a document. Only follow this step if you want to anchor a comment to any revision other than the latest revision. If you want to use the latest revision, use head for the revisionID.

  2. Call the create() method with the fileID parameter, a comments resource containing the comment, and a JSON anchor string containing the revisionID (r) and region (a).

How you define a region depends on the type of document content you are working with. For further information, refer to Define a region.

Define a region

As mentioned earlier, the JSON anchor string contains a revisionID (r) and region (a). The region (a) is a JSON array containing region classifiers specifying the format and location to which a comment is anchored. A classifier might be a two-dimensional rectangle for an image, a line of text in a document, or a time duration in a video. To define a region, select the region classifier that matches the type of content you are trying to anchor to. For example, if your content is text, you are likely going to use either the txt or line region classifier.

For a list of region classifiers in the Drive API, refer to Region classifiers.

The following example shows a JSON anchor string that anchors comments to lines in two separate areas of a document:

  • The first area starts at line 12 ('n':12) and extends for three lines ('l':3).
  • The second area only covers line 18 ('n':18, 'l':1`).
    {
      'r': 'REVISION_ID',
      'a': [
      {
        'line':
        {
          'n': 12,
          'l': 3,
        }
      },
      {
        'line':
        {
          'n': 18,
          'l': 1,
        }
      }]
    }

Replace REVISION_ID with head or the ID of a specific revision.

Get a comment

To get a comment on a file, use the get() method on the comments resource with the fileId and commentId parameters. If you don't know the comment ID, you can list all comments using the list() method.

The method returns an instance of a comments resource.

To include deleted comments in the results, set the includedDeleted query parameter to true.

Show an example

Request

In this example, we provide the fileId and commentId path parameters and multiple fields.

GET https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID?fields=id,comment,modifiedTime,resolved

List comments

To list comments on a file, use the list() method on the comments resource with the fileId parameter. The method returns a list of comments.

Pass the following query parameters to customize pagination of, or filter, comments:

  • includeDeleted: Set to true to include deleted comments. Deleted comments don't include the htmlContent or content fields.

  • pageSize: The maximum number of comments to return per page.

  • pageToken: A page token, received from a previous list call. Provide this token to retrieve the subsequent page.

  • startModifiedTime: The minimum value of the modifiedTime field for the result comments.

Show an example

Request

In this example, we provide the fileId path parameter, the includeDeleted query parameter, and multiple fields.

GET https://www.googleapis.com/drive/v3/files/FILE_ID/comments?includeDeleted=true&fields=(id,comment,kind,modifiedTime,resolved)

Update a comment

To update a comment on a file, use the update() method on the comments resource with the fileId and commentId parameters. The request body uses the content field to update the comment.

The method returns the fields listed in the fields query parameter.

Show an example

Request

In this example, we provide the fileId and commentId path parameters and multiple fields.

PATCH https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID?fields=id,comment

Request body

{
  "content": "This comment is now updated."
}

Delete a comment

To delete a comment on a file, use the delete() method on the comments resource with the fileId and commentId parameters.

When a comment is deleted, Drive marks the comment resource as deleted: true. Deleted comments don't include the htmlContent or content fields.

Show an example

Request

In this example, we provide the fileId and commentId path parameters.

DELETE https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID