How to use the Google chat /messages:search REST API endpoint?

Hi.

I’m trying to create an app that performs a simple search of the messages in one of my team’s Google Chat spaces using the following REST API:
https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/search

(Note: The docs say this endpoint is part of the Google Workspace Developer Preview Program. I have an email confirming that the project I’m using the credentials from is associated with the preview program.)

Sample code below. Am I missing something obvious here?

Any help would be greatly appreciated!

The sample Node.js code below shows what I’ve tried so far:

  • - The call to the /messages endpoint successfully returns messages, so that implies the space ID is right and authentication works, yes?
  • - The call to /messages:search gets HTTP 400
import { authenticate } from "@google-cloud/local-auth";
import ax from "axios";

const auth = await authenticate({ 
  "keyfilePath" : "<where-I-downloaded-credentials-file>",
  "scopes"      : [ "https://www.googleapis.com/auth/chat.messages.readonly" ]
});
const token_response = await auth.getAccessToken();
const access_token = token_response.token;
const config  = { "headers" : { "Authorization" : "Bearer " + access_token } };
const body = { "filter" : "deadlines" };
const url = "https://chat.googleapis.com/v1/spaces/<space-ID>";
let res;

try
{
  //res = await ax.get(  url + "/messages?pageSize=2", config ); // <-- WORKS
  res = await ax.post( url + "/messages:search", body, config ); // <-- FAILS
  console.log( res );  
}
catch( err )
{
    console.log( err );
}

According to the following guide:

…your space id must be a “-” for that specific endpoint. So you’ll need to change the base url.

Are your headers being called correctly from client side to server side?

Also I’d check to make sure if you are using cloud in your project that you have the correct IAM permissions and APIs enabled

Thank you for taking the time to read my question and make your suggestion.

Changing the URL and specifying space.name in the filter solved my problem (updated code below.)

As an aside, the topic you link to is the Cloud Client Library. With the Node.js version, I get the error “chatClient.searchMessagesAsync is not a function”. In chat_service_client.js, I can see listMessages and listMessagesAsync but I cannot find any searchMessages routines. (I think that’s a bug, but I don’t have the ability to open a support ticket.) That’s why I resorted to calling the REST API directly.

For completeness, updating the following lines of code made my repro work for both listing messages and searching them:

const body = { "filter" : '"deadlines" AND space.name = "spaces/<space-ID>"' };
const url = "https://chat.googleapis.com/v1/spaces/";
let res;

try
{
//res = await ax.get(  url + "<space-ID>/messages?pageSize=2", config );
res = await ax.post( url + "-/messages:search", body, config );

Thank you @Porter_Bates for also reading my question and making these suggestions. To anyone else reading this, at one point or another I definitely got these things wrong too. So check these points.

^^ You can see me erroneously trying to pass the space ID in the URL instead of the filter where it belongs.

Hey no worries I figured it had to do something with that :slight_smile: If anyone needs further help with there projects let me know