> Documentation index: https://developers.google.com/workspace/chat/llms.txt

# Update a user&#39;s thread read state

This guide explains how to use the
[`update`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.ChatService.UpdateThreadReadState)
method on the `ThreadReadState` resource of the Google Chat API to mark message
threads as read or unread.

The
[`ThreadReadState` resource](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.ThreadReadState)
is a singleton resource that represents details about a
specified user's last read message in a Google Chat message thread.

## Prerequisites


### Node.js

- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).

<!-- -->

- Set up your environment:
  - [Create a Google Cloud project](https://developers.google.com/workspace/guides/create-project.md.txt).
  - [Configure the OAuth consent screen](https://developers.google.com/workspace/guides/configure-oauth-consent.md.txt).
  - [Enable and configure the Google Chat API](https://developers.google.com/workspace/chat/configure-chat-api.md.txt) with a name, icon, and description for your Chat app.
  - Install the Node.js [Cloud Client Library](https://developers.google.com/workspace/chat/libraries.md.txt?tab=nodejs#cloud-client-libraries).
  - [Create OAuth client ID credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user.md.txt#step-2:) for a desktop application. To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.

  For guidance, complete the steps for setting up your environment in this [quickstart](https://developers.google.com/workspace/chat/api/guides/quickstart/nodejs
  #set-up-environment).
- [Choose an authorization scope](https://developers.google.com/workspace/chat/authenticate-authorize.md.txt#asynchronous-chat-calls) that supports user authentication.

### Python

- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).

<!-- -->

- Set up your environment:
  - [Create a Google Cloud project](https://developers.google.com/workspace/guides/create-project.md.txt).
  - [Configure the OAuth consent screen](https://developers.google.com/workspace/guides/configure-oauth-consent.md.txt).
  - [Enable and configure the Google Chat API](https://developers.google.com/workspace/chat/configure-chat-api.md.txt) with a name, icon, and description for your Chat app.
  - Install the Python [Cloud Client Library](https://developers.google.com/workspace/chat/libraries.md.txt?tab=python#cloud-client-libraries).
  - [Create OAuth client ID credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user.md.txt#step-2:) for a desktop application. To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.

  For guidance, complete the steps for setting up your environment in this [quickstart](https://developers.google.com/workspace/chat/api/guides/quickstart/python
  #set-up-environment).
- [Choose an authorization scope](https://developers.google.com/workspace/chat/authenticate-authorize.md.txt#asynchronous-chat-calls) that supports user authentication.


> [!NOTE]
> The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](https://developers.google.com/workspace/chat/api/reference.md.txt).

<br />

## Update the calling user's thread read state

To update a user's read state within a message thread, include the following in
your request:

- Specify the `chat.users.readstate` authorization scope.
- Call the [`UpdateThreadReadState`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.ChatService.UpdateThreadReadState) method.
- Pass `updateMask` with the value `lastReadTime`.
- Pass `threadReadState` as an instance of [`ThreadReadState`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.ThreadReadState) with the following:
  - The `name` field set to the thread read state to update, which includes a user ID or alias, a space ID, and a thread ID. Updating thread read state only supports updating the read state of the calling user, which can be specified by setting one of the following:
    - The `me` alias. For example, `users/me/spaces/SPACE/threads/THREAD/threadReadState`.
    - The calling user's Workspace email address. For example, `users/user@example.com/spaces/SPACE/threads/THREAD/threadReadState`.
    - The calling user's user ID. For example, `users/USER/spaces/SPACE/threads/THREAD/threadReadState`.
  - The `lastReadTime` field set to the timestamp that marks the calling user's last read position in the thread, such as the timestamp of the last read message or the current time:
    - **Mark as unread** : Set `lastReadTime` to a timestamp earlier than the creation time of the latest message in the thread. In Chat, the thread appears as unread from that timestamp onward, updating unread badges, bolding the thread, and surfacing it in **Home**.
    - **Mark as read** : Set `lastReadTime` to a timestamp equal to or later than the creation time of the latest message in the thread. If you specify a future timestamp, the API coerces `lastReadTime` to the current request time.
    - **Minimum timestamp** : The `lastReadTime` field must be on or after the Unix epoch (`1970-01-01T00:00:00Z`). Setting a negative timestamp (before `1970-01-01T00:00:00Z`) returns an `INVALID_ARGUMENT` (`400`) error.

The following example updates the calling user's thread read state:

### Node.js

```javascript
/**
 * This sample shows how to update the calling user's thread read state.
 *
 * It relies on the @google-apps/chat npm package.
 */
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rest/v1/users.spaces.threads/updateThreadReadState

const {ChatServiceClient} = require('@google-apps/chat');
const {auth} = require('google-auth-library');

async function main() {
  // Create a client
  const chatClient = new ChatServiceClient({
    authClient: await auth.getClient({
      scopes: ['https://www.googleapis.com/auth/chat.users.readstate']
    })
  });

  // Initialize request arguments.
  const request = {
    threadReadState: {
      name: 'users/me/spaces/SPACE_NAME/threads/THREAD_NAME/threadReadState',
      lastReadTime: {
        seconds: Math.floor(Date.now() / 1000),
        nanos: 0
      }
    },
    updateMask: {
      paths: ['last_read_time']
    }
  };

  // Make the request
  const response = await chatClient.updateThreadReadState(request);
  console.log(response);
}

main().catch(console.error);
```

### Python

```python
"""
This sample shows how to update the calling user's thread read state.
"""
from google.apps import chat_v1
import google.auth
from google.protobuf import timestamp_pb2

# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rest/v1/users.spaces.threads/updateThreadReadState

def update_thread_read_state():
    # Authenticate with Chat API using user credentials.
    credentials, _ = google.auth.default(
        scopes=["https://www.googleapis.com/auth/chat.users.readstate"]
    )
    client = chat_v1.ChatServiceClient(credentials=credentials)

    # Set timestamp for last read time.
    now = timestamp_pb2.Timestamp()
    now.GetCurrentTime()

    thread_read_state = chat_v1.ThreadReadState(
        name="users/me/spaces/SPACE_NAME/threads/THREAD_NAME/threadReadState",
        last_read_time=now,
    )

    request = chat_v1.UpdateThreadReadStateRequest(
        thread_read_state=thread_read_state,
        update_mask={"paths": ["last_read_time"]},
    )

    response = client.update_thread_read_state(request=request)
    print(response)

if __name__ == "__main__":
    update_thread_read_state()
```

To run this sample, replace the following:

- `SPACE_NAME`: the ID from the space's [`name`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.Space.FIELDS.string.google.chat.v1.Space.name). You can obtain the ID by calling the [`ListSpaces`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.ChatService.ListSpaces) method or from the space's URL.
- `THREAD_NAME`: the ID from the thread's [`name`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.Thread.FIELDS.string.google.chat.v1.Thread.name). You can obtain the ID from the response body returned after creating a message asynchronously with the Chat API, or with the [custom name](https://developers.google.com/workspace/chat/create-messages.md.txt#name_a_created_message) assigned to the message at creation.

The Chat API updates the specified thread read state and returns
an instance of
[`ThreadReadState`](https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1.md.txt#google.chat.v1.ThreadReadState).

## Related topics

- [Get the calling user's thread read state](https://developers.google.com/workspace/chat/get-thread-read-state.md.txt)
- [Update the calling user's space read state](https://developers.google.com/workspace/chat/update-space-read-state.md.txt)
- [Get the calling user's space read state](https://developers.google.com/workspace/chat/get-space-read-state.md.txt)