Skip to content
Skip to main content

Using the Threads API

Last updated:

The Nylas Threads API gives you more control over how you detect collections of messages, and how you respond to them. This page explains how to work with the Threads API.

Most providers organize messages both as individual messages, and as threads. Threads represent collections of messages that are related to each other and result from participants replying to an email conversation.

Every message is associated with a thread, whether that thread contains only one message or many.

Threads don’t have a metadata object. Metadata lives on the messages in a thread, so filter messages with metadata_pair and group them by thread_id.

When you look at threads from a code perspective, you can detect a thread and group messages using several criteria (the message subject, recipients, senders, and so on). Grouping several messages into a thread provides a coherent view of an email conversation. In long or fast-moving conversations, threads help the reader follow the discussion.

For Google and Microsoft accounts, Nylas returns the provider’s own threads (Gmail threads and Outlook conversations), so they match what users see in their email client. For IMAP accounts, Nylas groups messages into threads itself, and Agent Accounts link replies to their thread using the In-Reply-To and References headers.

Threads are organized non-linearly, as collections of related messages. Each thread keeps track of the message_ids for every message in it.

Because all of a thread’s message_ids are logged, you can reply to a specific message by referencing its ID. This creates a new branch of the thread, similar to a tree structure, and the messages in the new branch continue to be associated with the thread.

How replies branch inside one thread

Replying to an earlier message starts a new branch, but every message in every branch stays in the same thread. The thread's message_ids lists them all, and latest_draft_or_message is the newest one.
Diagram as text
  • One thread: every message shares the same thread_id:
    • Original message (message 1): Starts the thread.
      • Reply (message 2): Replies to message 1.
        • Reply (message 3): Replies to message 2.
          • Reply (message 5): The branch keeps growing.
        • Reply (message 4): Also replies to message 2, so it starts a new branch.
      • Reply (message 6): Replies to the original again: a second branch.
  • How a message gets its thread_id:
    • Google and Microsoft grants: Nylas returns the provider's own thread: the Gmail thread or the Outlook conversation.
    • IMAP grants: Nylas groups the messages and assigns the thread_id.
    • Agent Accounts: Nylas links each reply to its thread using the In-Reply-To and References headers.
    • Start a branch: Send with reply_to_message_id set to the message you're answering.

Build a fully-fledged inbox with the Threads API

Section titled “Build a fully-fledged inbox with the Threads API”

You can use the /threads endpoint to emulate popular inbox UIs, like Gmail or Outlook, which group messages into threads. This gives users a simple, centralized view of their latest email conversation. By combining this view with calls to the /messages endpoint, you can build a fully-fledged inbox in your application.

Before you can use the Threads API, you need the following prerequisites:

  • A Nylas application.
  • A working authentication configuration. Either…
    • A Nylas Dashboard Sandbox application which includes a demonstration auth config, OR
    • A provider auth app (Google or Azure), and a connector for that auth app.
  • A Google or Microsoft grant with at least the following scopes:
    • Google: gmail.readonly
    • Microsoft: Mail.Read

The following examples show how to return the five most recent threads from an authenticated account. For more information on filtering requests, see Avoiding rate limits in Nylas.

The following request returns a specific thread.

Because threads are non-linear, you can reply to any message listed in the thread’s message_ids by passing its ID as reply_to_message_id when you send. This starts a new branch of the thread, as the thread diagram shows.

You can add query parameters to a Get all Threads request to search for threads using specific criteria.

You can also use the Nylas SDKs to search for threads, as in the following examples.