Version:
Only show these results:

Read email messages and threads with Node.js

This page explains how to use the Nylas Node.js SDK and Email API to read your end users' email messages and threads. For more information, see the Email documentation.

Messages versus Threads

Messages are the fundamental object of the Nylas platform and the core building block for most email applications. Messages contain several pieces of information, such as when the message was sent, the sender's address, to whom it was sent, and the message body. They can also contain files (attachments), calendar event invitations, and more.

Threads are first-class objects that represent collections of messages which are related to each other, and result from people replying to an email conversation. For Gmail and Microsoft Exchange accounts, Nylas threads messages together so that they are as similar as possible to their representation in those environments. For all other providers, including generic IMAP, Nylas threads messages using a custom JWZ-inspired algorithm.

For more information, see the Messages and Threads reference documentation.

Before you begin

Before you start, you must have done the following tasks:

Read email messages from an inbox

To read individual email messages from an end user's inbox, you must fetch one or more messages from their account. To do so, use the queryParams property to filter the email messages, and the limit property to return only a specific number of results, as in the example below.

async function fetchRecentEmails(): Promise<void> {
try {
const identifier = process.env.NYLAS_GRANT_ID as string;

const messages = await nylas.messages.list({
identifier,
queryParams: {
limit: 5,
in: ['inbox']
}
})

console.log('Recent Messages:', messages)
} catch (error) {
console.error('Error fetching emails:', error)
}
}

fetchRecentEmails();

You can also return the body content of the email message, its snippet, its recipients' email addresses, and any associated folders or labels. For more information, see the Messages reference documentation.

Read threads from an inbox

Along with individual email messages, you can read threads from an end user's email inbox. Each Thread object contains a list of messages which are part of the thread. Use the .list() function to filter and paginate your results. By default, Nylas returns the 100 most recent threads in the end user's inbox.

The following example lists the five most recent unread threads in the end user's inbox and their subject parameters.

async function fetchRecentThreads(): Promise<void> {
try {
const identifier = '<NYLAS_GRANT_ID>'

const threads = await nylas.threads.list({
identifier,
queryParams: {
limit: 5,
}
})

console.log('Recent Threads:', threads)
} catch (error) {
console.error('Error fetching threads:', error)
}
}

fetchRecentThreads()

You can also retrieve the senders' email addresses, the recipients' email addresses, the email addresses in the CC and BCC fields, and the unread and starred parameters. See the Threads reference documentation for more information.

Explore the Email API

If you've made it to this point, congratulations! You've learned how to read email messages and threads with the Nylas Node.js SDK and Email API! 🎉

There's plenty more that you can do with Nylas. Take a look at the following resources to learn more.