Gmail API thread replies missing when fetching thread messages after UI response

My Setup:

I’m working with two Gmail accounts (let’s call them User X and User Y) from different Google Workspace domains.

What I’m doing:

  1. I send an email from User X to User Y using Gmail API through Google Apps Script:
function createAndSendMessage(toAddress, emailSubject, messageBody) {
  
  // Build the email message
  var messageText = [
    'To: ' + toAddress,
    'Subject: ' + emailSubject,
    '',
    messageBody
  ].join('\r\n');

  var encodedMessage = Utilities.base64EncodeWebSafe(messageText);

  // Send using Gmail API
  var response = Gmail.Users.Messages.send({
    raw: encodedMessage
  }, 'me');

  return response.threadId
}
  1. User Y replies to this email through the regular Gmail web interface
  2. The reply shows up correctly in Gmail UI and appears threaded with the original message
  3. I try to fetch all messages in the thread using the threadId from step 1

The Issue:

When I call users.threads.get with the threadId, I only get back the original message I sent programmatically. The reply sent through Gmail UI doesn’t appear in the API response, even though it’s clearly part of the same thread in the Gmail interface.

My Question:

What do I need to include in my initial email to ensure that replies get properly threaded when retrieved via the API? Are there specific headers I should add when sending the original message?

This happens when you’re missing the Message-ID header in your original email. Gmail creates its own Message-ID through the API, but replies don’t link back to your thread properly. I manually set a Message-ID header - it fixes threading issues. Also check your authentication. If you send as User X but fetch the thread as User X, you won’t see User Y’s replies. You need to fetch from User Y’s mailbox or make sure both users can access the thread.

yea, i had that problem too. just add a little delay before you fetch the thread; sometimes the api just needs time to catch up. also, check the oauth scopes, make sure you’ve got gmail.readonly or gmail.modify. without the right scopes, it can get messy.

I’ve hit this exact issue building automated email workflows. The problem is you’re fetching the thread from the wrong user’s perspective. When User X sends an email and User Y replies, the thread exists in both mailboxes but with different messages. Your threadId from the send operation belongs to User X’s mailbox, so users.threads.get only shows what User X can see. To get the full thread with User Y’s reply, you need to authenticate as User Y and fetch from their mailbox. Threading works fine - it’s just about API permissions and whose data you’re querying.