Can Gmail API fetch email content in single list request

I’m working on retrieving email content and headers from Gmail messages. Currently, I need to make separate API calls for each message to get the full body text, which makes my application really slow.

Here’s what I’m currently doing:

const result = await gmail.users.messages.list({
  userId: "me",
  maxResults: messageLimit,
  pageToken: currentPageToken,
  includeSpamTrash: false,
});

// Trying these helper functions to get message content

function getMessageText(
  messagePart: GmailV1.Schema$MessagePart | undefined
): string | null {
  if (!messagePart) {
    return null;
  }

  if (messagePart.mimeType === "text/plain") {
    const content = messagePart.body?.data ?? "";
    return Buffer.from(content, "base64").toString("utf8");
  } else if (messagePart.mimeType === "multipart/alternative") {
    for (const section of messagePart.parts!) {
      const content = getMessageText(section);
      if (content) {
        return content;
      }
    }
  }

  return "";
}

function extractContent(email: GmailV1.Schema$Message) {
  const structure = email?.payload;

  let content = "";
  const bodyData = structure?.parts ? structure.parts[1].body : structure?.body;

  if (bodyData?.data) {
    const decoded = Buffer.from(bodyData.data, "base64");
    content = decoded.toString("utf-8");
  }

  if (bodyData?.attachmentId) {
    const textContent = structure?.parts[0]?.parts[1]?.body?.data;
    if (textContent) {
      const decoded = Buffer.from(textContent, "base64");
      content = decoded.toString("utf-8");
    }
  }

  return content;
}

I read that Gmail API supports batch operations. Could this help me get all message bodies at once? Any examples using the googleapis library would be helpful.

batch requests wont help much here since gmail api still requires individual message.get calls for full content. the list endpoint only returns metadata/ids. try using format=full parameter in your message.get calls tho - might reduce some overhead compared to default format.

You’re running into the fundamental limitation of Gmail API - there’s no way to fetch complete message bodies in bulk. The batch API helps with making multiple requests efficiently but you still need individual message.get calls for each email’s content. What actually improved performance in my projects was implementing concurrent requests with proper rate limiting. Instead of sequential API calls, I use Promise.all with chunks of 10-15 messages at a time. Also consider using format=‘metadata’ for the initial list if you only need headers, then fetch full content selectively. Another approach that worked well was caching message content locally and only fetching new/changed messages using the history API for incremental updates rather than re-fetching everything.

Unfortunately you’re stuck with individual API calls for message content - that’s just how Gmail API works. However, I’ve found that implementing a smart queueing system makes a huge difference. When I faced this same issue, I switched to using async/await with worker pools that respect the API quotas. The key insight was realizing you don’t always need full message bodies immediately. I implemented lazy loading where I fetch basic message info first, then only grab full content when users actually click to read emails. This reduced my API calls by about 70% in typical usage. Also worth mentioning that using the ‘minimal’ format for initial queries and then upgrading to ‘full’ only when needed helped significantly with response times.