Find a group chat

This guide explains how to find group chats that contain the calling user and a specified list of other users. In the Google Chat API, group chats are Space resources that have spaceType set to GROUP_CHAT. To find a group chat, use the findGroupChats (RPC, REST) method on the Space resource.

Prerequisites

Node.js

Python

Java

Apps Script

Find a group chat

To find a group chat in Google Chat, pass the following in your request:

  • An authorization scope: chat.memberships.readonly or chat.memberships.
  • Call the findGroupChats (RPC, REST) method, passing the resource names of the other users.

Here's how to find a group chat with specific members:

Node.js

/**
 * This sample shows how to find a group chat with specific members.
 *
 * It relies on the @google-apps/chat npm package.
 */
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
const{ChatServiceClient}=require('@google-apps/chat');
const{auth}=require('google-auth-library');
asyncfunctionmain(){
// Create a client
constchatClient=newChatServiceClient({
authClient:awaitauth.getClient({
scopes:['https://www.googleapis.com/auth/chat.memberships.readonly']
})
});
// The users to find a group chat with.
// Don't include the caller.
constusers=[
'users/123456789',
'users/987654321'
];
// Create the request
constrequest={
users:users
};
// Call the API
constresponse=awaitchatClient.findGroupChats(request);
// Handle the response
if(response.spaces && response.spaces.length > 0){
console.log('Found group chat:',response.spaces[0].name);
}else{
console.log('No group chat found.');
}
}
main().catch(console.error);

Python

"""
This sample shows how to find a group chat with specific members.
"""
fromgoogle.appsimport chat_v1
importgoogle.auth
# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
deffind_group_chat():
 # Create a client
 scopes = ["https://www.googleapis.com/auth/chat.memberships.readonly"]
 credentials, _ = google.auth.default(scopes=scopes)
 client = chat_v1.ChatServiceClient(credentials=credentials)
 # The users to find a group chat with.
 # Don't include the caller.
 users_list = [
 "users/123456789",
 "users/987654321"
 ]
 # Create the request
 request = chat_v1.FindGroupChatsRequest(
 users=users_list
 )
 # Call the API
 response = client.find_group_chats(request)
 # Handle the response
 if response.spaces:
 print(f"Found group chat: {response.spaces[0].name}")
 else:
 print("No group chat found.")
if __name__ == "__main__":
 find_group_chat()

Java

/**
 * This sample shows how to find a group chat with specific members.
 */
importcom.google.api.gax.core.FixedCredentialsProvider;
importcom.google.auth.oauth2.GoogleCredentials;
importcom.google.chat.v1.ChatServiceClient;
importcom.google.chat.v1.ChatServiceSettings;
importcom.google.chat.v1.FindGroupChatsRequest;
importcom.google.chat.v1.FindGroupChatsResponse;
importjava.util.Arrays;
importjava.util.List;
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
publicclass FindGroupChat{
publicstaticvoidmain(String[]args)throwsException{
GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault()
.createScoped(Arrays.asList("https://www.googleapis.com/auth/chat.memberships.readonly"));
ChatServiceSettingssettings=ChatServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
try(ChatServiceClientchatServiceClient=ChatServiceClient.create(settings)){
List<String>users=Arrays.asList(
"users/123456789",
"users/987654321"
);
FindGroupChatsRequestrequest=FindGroupChatsRequest.newBuilder()
.addAllUsers(users)
.build();
FindGroupChatsResponseresponse=chatServiceClient.findGroupChats(request);
if(!response.getSpacesList().isEmpty()){
System.out.printf("Found group chat: %s\n",response.getSpacesList().get(0).getName());
}else{
System.out.println("No group chat found.");
}
}
}
}

Apps Script

/**
 * This sample shows how to find a group chat with specific members.
 */
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
functionfindGroupChat(){
// The users to find a group chat with.
// Don't include the caller.
constusers=[
'users/123456789',
'users/987654321'
];
try{
// Call the API
// In Apps Script, query parameters are passed as optional arguments
constresponse=Chat.Spaces.findGroupChats({
users:users
});
if(response.spaces && response.spaces.length > 0){
console.log('Found group chat: '+response.spaces[0].name);
}else{
console.log('No group chat found.');
}
}catch(err){
// Handle error
console.log('Failed to find group chat: '+err.message);
}
}

To run this sample, replace the user resource names with valid user IDs. You can obtain user IDs from the People API or the Directory API.

The Chat API returns an instance of FindGroupChatsResponse (RPC, REST) that contains the list of found spaces.

Find a group chat with details

By default, findGroupChats (RPC, REST) returns Space objects that only contain the name field, in the format spaces/SPACE_NAME. To get more details about the space, such as the displayName, spaceType, or createTime, specify the spaceView parameter as SPACE_VIEW_EXPANDED.

Using SPACE_VIEW_EXPANDED requires an additional authorization scope: https://www.googleapis.com/auth/chat.spaces or https://www.googleapis.com/auth/chat.spaces.readonly.

Here's how to find a group chat and retrieve its details:

Node.js

/**
 * This sample shows how to find a group chat with specific members and return details.
 *
 * It relies on the @google-apps/chat npm package.
 */
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
const{ChatServiceClient}=require('@google-apps/chat');
const{auth}=require('google-auth-library');
asyncfunctionmain(){
// Create a client
constchatClient=newChatServiceClient({
authClient:awaitauth.getClient({
scopes:[
'https://www.googleapis.com/auth/chat.spaces.readonly',
'https://www.googleapis.com/auth/chat.memberships.readonly'
]
})
});
// The users to find a group chat with.
// Don't include the caller.
constusers=[
'users/123456789',
'users/987654321'
];
// Create the request
constrequest={
users:users,
spaceView:'SPACE_VIEW_EXPANDED'
};
// Call the API
constresponse=awaitchatClient.findGroupChats(request);
// Handle the response
if(response.spaces && response.spaces.length > 0){
console.log('Found group chat:',response.spaces[0].displayName);
}else{
console.log('No group chat found.');
}
}
main().catch(console.error);

Python

"""
This sample shows how to find a group chat with specific members and return details.
"""
fromgoogle.appsimport chat_v1
importgoogle.auth
# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
deffind_group_chat_with_details():
 # Create a client
 scopes = [
 "https://www.googleapis.com/auth/chat.memberships.readonly",
 "https://www.googleapis.com/auth/chat.spaces.readonly"
 ]
 credentials, _ = google.auth.default(scopes=scopes)
 client = chat_v1.ChatServiceClient(credentials=credentials)
 # The users to find a group chat with.
 # Don't include the caller.
 users_list = [
 "users/123456789",
 "users/987654321"
 ]
 # Create the request
 request = chat_v1.FindGroupChatsRequest(
 users=users_list,
 space_view=chat_v1.SpaceView.SPACE_VIEW_EXPANDED
 )
 # Call the API
 response = client.find_group_chats(request)
 # Handle the response
 if response.spaces:
 print(f"Found group chat: {response.spaces[0].display_name}")
 else:
 print("No group chat found.")
if __name__ == "__main__":
 find_group_chat_with_details()

Java

/**
 * This sample shows how to find a group chat with specific members and return details.
 */
importcom.google.api.gax.core.FixedCredentialsProvider;
importcom.google.auth.oauth2.GoogleCredentials;
importcom.google.chat.v1.ChatServiceClient;
importcom.google.chat.v1.ChatServiceSettings;
importcom.google.chat.v1.FindGroupChatsRequest;
importcom.google.chat.v1.FindGroupChatsResponse;
importcom.google.chat.v1.SpaceView;
importjava.util.Arrays;
importjava.util.List;
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
publicclass FindGroupChatWithDetails{
publicstaticvoidmain(String[]args)throwsException{
GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault()
.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/chat.memberships.readonly",
"https://www.googleapis.com/auth/chat.spaces.readonly"
));
ChatServiceSettingssettings=ChatServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
try(ChatServiceClientchatServiceClient=ChatServiceClient.create(settings)){
List<String>users=Arrays.asList(
"users/123456789",
"users/987654321"
);
FindGroupChatsRequestrequest=FindGroupChatsRequest.newBuilder()
.addAllUsers(users)
.setSpaceView(SpaceView.SPACE_VIEW_EXPANDED)
.build();
FindGroupChatsResponseresponse=chatServiceClient.findGroupChats(request);
if(!response.getSpacesList().isEmpty()){
System.out.printf("Found group chat: %s\n",response.getSpacesList().get(0).getDisplayName());
}else{
System.out.println("No group chat found.");
}
}
}
}

Apps Script

/**
 * This sample shows how to find a group chat with specific members and return details.
 */
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
functionfindGroupChatWithDetails(){
// The users to find a group chat with.
// Don't include the caller.
constusers=[
'users/123456789',
'users/987654321'
];
try{
// Call the API
// In Apps Script, query parameters are passed as optional arguments
constresponse=Chat.Spaces.findGroupChats({
users:users,
spaceView:'SPACE_VIEW_EXPANDED'
});
if(response.spaces && response.spaces.length > 0){
console.log('Found group chat: '+response.spaces[0].displayName);
}else{
console.log('No group chat found.');
}
}catch(err){
// Handle error
console.log('Failed to find group chat: '+err.message);
}
}

To run this sample, replace the user resource names with valid user IDs. You can obtain user IDs from the People API or the Directory API.

The Chat API returns an instance of FindGroupChatsResponse (RPC, REST) that contains the list of found spaces including additional spaceView details.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年07月22日 UTC.