List events from a Google Chat space

This guide explains how to use the list() method on the SpaceEvent resource of the Google Chat API to list changes to resources in a space.

The SpaceEvent resource represents a change to the target space, including child resources of the space such as messages, reactions, and memberships. For more information about the list of event types and event payloads supported, see the eventType and payload fields of the SpaceEvent resource reference documentation.

You can list events up to 28 days before the time of the request. The server returns events that contain the most recent version of the affected resource. For example, if you list events about new space members, the server returns Membership resources that contain the latest membership details. If new members were removed during the requested period, the event payload contains an empty Membership resource.

To call this method, you must use user authentication. To list events from a space, the authenticated user must be a member of the space.

Prerequisites

Node.js

List space events

To list space events from a Chat space, pass the following in your request:

  • Specify one or more authorization scopes to support each event type in your request. As a best practice, choose the most restrictive scope that still allows your app to function. To choose a scope, see the Authentication and authorization overview.

  • Call the ListSpaceEvents() method, passing the filter of event types to list. You must specify at least one event type, and you can also filter by date. For a list of supported event types, see the eventType field of the SpaceEvent resource reference documentation.

The following example lists events about new memberships and messages in a space:

Node.js

chat/client-libraries/cloud/list-space-events-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

// Authorization scopes based on the event types
const USER_AUTH_OAUTH_SCOPES = [
  'https://www.googleapis.com/auth/chat.memberships.readonly',
  'https://www.googleapis.com/auth/chat.messages.readonly'
];

// This sample shows how to list space events with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    parent: 'spaces/SPACE_NAME',
    // A required filter. Filters events about new memberships and messages
    filter: 'eventTypes:"google.workspace.chat.membership.v1.created" OR eventTypes:"google.workspace.chat.message.v1.created"'
  };

  // Make the request
  const pageResult = chatClient.listSpaceEventsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

main().catch(console.error);

To run this sample, replace SPACE_NAME with the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.

The Chat API returns a list of paginated list of space events about new memberships and messages.