Read inbox messages and threads with the Nylas Node.js SDK
The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and the Nylas Node.js SDK makes it easy to read data directly from user email accounts.
This guide explains how to use the Nylas Node.js SDK and Email API to read inbox messages and threads with JavaScript.
What you'll learn
In this tutorial, you'll learn how to do the following:
- Read inbox messages.
- Read inbox threads.
Prerequisites
Prerequisites include installing the Nylas Node.js SDK and setting up your environment. Before you start this tutorial, make sure that you do the following:
- Complete the Getting started with the Nylas Node.js SDK tutorial.
- Authenticate users with the Nylas Node.js SDK.
Messages and 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 a message was sent, the sender's address, to whom it was sent, and the message body. Message can also contain files (attachments), calendar event invitations, and more.
Threads are first-class objects that represent a collection of messages. Messages are threaded together with a variety of heuristics. For Gmail and Microsoft Exchange accounts, messages are threaded together as close as possible to the representation in their environments. For all other providers, including generic IMAP, messages are threaded using a custom JWZ-inspired algorithm.
Step 1: Initialize the Nylas object
At its core, the Nylas Communication Platform is an API client that interfaces with all of the major email providers.
The Nylas
object provides access to every resource in Nylas APIs. Before making any request, you must initialize the Nylas
object with your client ID, client secret, and access token.
-
Call the
config
function and pass your client ID (<CLIENT_ID>
) and client secret (<CLIENT_SECRET>
). -
Call the
with
function and pass your access token (<ACCESS_TOKEN>
).
const Nylas = require('nylas');
Nylas.config({
clientId: <CLIENT_ID>,
clientSecret: <CLIENT_SECRET>
});
const nylas = Nylas.with(<ACCESS_TOKEN>);
Step 2: Change the base API URL
You can optionally change the base API URL depending on your location:
Location | API URL | Scheduler API URL |
---|---|---|
United States (Oregon) | https://api.nylas.com |
https://api.schedule.nylas.com |
Europe (Ireland) | https://ireland.api.nylas.com |
https://ireland.api.schedule.nylas.com |
For more information, see Data residency and Migration guide for data centers.
To change the base URL to the EU (Ireland) region, set apiServer: regionConfig[Region.Ireland].nylasAPIUrl
. The base API URL defaults to the US region.
const Nylas = require('nylas');
Nylas.config({
clientId: <CLIENT_ID>,
clientSecret: <CLIENT_SECRET>,
apiServer: regionConfig[Region.Ireland].nylasAPIUrl
});
const nylas = Nylas.with(<ACCESS_TOKEN>);
Step 3: Read inbox messages and threads
Learn how to read inbox messages and threads.
Read inbox messages
Call the .first()
function to return the most recent message in the account’s inbox.
The following example returns the most recent unread
message with its subject
and id
:
nylas.messages.first({in: 'inbox'}).then(message =>{
console.log(`Subject: ${message.subject} | ID: ${message.id} | Unread: ${message.unread}`);
});
You can return the email body, snippet, recipient email addresses, folders, and labels. Learn more about Messages.
Read inbox threads
Each thread
iterates through a list of message objects in the thread. Call the .list()
function to filter and paginate threads.
By default, the .list()
function returns the 100 most recent threads, but you can filter and paginate the results. The following examples reads the five most recent unread
threads from the email inbox and with their subject
:
nylas.threads.list({unread: true, limit: 5}).then(threads =>{
for (let thread of threads) {
console.log(thread.subject);
}
});
You can also retrieve the following:
- From addresses
- To addresses
- CC and BCC fields
- Timestamps
unread
andstarred
indicating whether the thread includes unread or starred messages
Learn more about Threads.
Example: Read inbox messages and threads
const Nylas = require('nylas');
Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);
// List the most recent unread message with its subject line
nylas.messages.first({in: 'inbox'}).then(message =>{
console.log(`Subject: ${message.subject} | ID: ${message.id} | Unread: ${message.unread}`);
});
// List the five most recent unread email threads with their subject lines
nylas.threads.list({unread: true, limit: 5}).then(threads =>{
for (let thread of threads) {
console.log(thread.subject);
}
});
More resources
If you’ve made it this far, congrats! You’ve successfully learned how to read inbox messages and threads with the Nylas Email API! There's plenty more that you can do with Nylas. Take a look at the following resources:
- Follow our Node.js SDK tutorials.
- Learn more about the Nylas API.
- Learn more about the Nylas Email API.
- Read about how the Nylas Communications Platform works.
- Read the blog post How to read emails with the Nylas Node.js SDK.