Only show these results:

Read an Inbox with Node.js

The Nylas Node SDK is the quickest way to integrate email into your app with JavaScript and the Nylas Email API. Build your email integration in 15 minutes.

Node Email Create

Node.js has grown to be the most popular framework in the world, primarily due to its efficiency for data-intensive real-time applications and the ability to use JavaScript across the entire web stack. The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and our Node.js SDK makes it simple to read data directly from user email accounts.

This guide explains how to use the Nylas Node.js SDK and Email API to read an email inbox with JavaScript. It covers the following steps:

  1. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Node.js SDK
  3. Read data from an email inbox
  4. Explore the Nylas Email API

Prerequisites

Before you can start using the Nylas Node.js SDK, make sure you have done the following:

  • Sign up for your developer account.
  • Get your developer keys. You need to have your:
    • CLIENT_ID - The CLIENT ID found on the dashboard page for your Nylas App.
    • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas App.
  • Get Node.js installed on your machine.
  • Make sure you have npm or yarn.

Install the Nylas Node.js SDK

Run npm install nylas or yarn add nylas.

Congratulations!

You’re now ready to write code with the Nylas Node.js SDK.

Read Data From an Email Inbox

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 the Nylas Email API, so the first thing you need to do is to initialize it.

const Nylas = require('nylas');   

Then, call the config function and pass the CLIENT_ID and CLIENT_SECRET from earlier in this guide, replacing the placeholders with your values.

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

Be Careful with Secrets

Follow best practices to include secrets like this in your code. For increased security, store these values as environment variables.

Next, call the with function and pass it the ACCESS_TOKEN from earlier in this guide. This allows Nylas to access data for a specific user account.

const nylas = Nylas.with(ACCESS_TOKEN);   

Messages vs Threads

Before reading content from your email inbox, it’s important to understand the difference between messages and threads.

Messages are the fundamental object of the Nylas platform, and the core building block for most email applications. They 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. They 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 to match the format of third-party accounts as closely as possible.

Reading Messages

Now, let’s take a look at messages.

nylas.messages.first({in: 'inbox'}).then(message =>{
console.log(`Subject: ${message.subject} | ID: ${message.id} | Unread: ${message.unread}`);
});

This code uses the .first() function to return the most recent message in the account’s inbox.

This example returns the message subject, id, and unread status, and you can also return the email body, snippet, recipient email addresses, folders, and labels. Take a look at the API reference for a list of everything you can access from a message object.

Reading Threads

Next, let’s get the 5 most recent unread threads from our email inbox and print their subject lines.

nylas.threads.list({unread: true, limit: 5}).then(threads =>{
for (let thread of threads) {
console.log(thread.subject);
}
});

Each thread this code iterates through contains a list of message objects. The .list() function allows you to filter and paginate the results based on a handful of criteria. By default, it returns the 100 most recent threads. In this case, we are only returning threads that are unread, and are limiting the results to the 5 most recent threads.

In addition to the subject line: thread.subject, you can also return lists of email addresses from the to, from, cc, and bcc fields, as well as unread and starred status. To learn more about pagination, filtering, and the parameters that are available to you, take a look at the API reference for threads.

Finally, here is the entire code example for convenience.

const Nylas = require('nylas');

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

const nylas = Nylas.with(ACCESS_TOKEN);

// List the 5 most recent unread email threads and print their subject lines
nylas.threads.list({unread: true, limit: 5}).then(threads =>{
for (let thread of threads) {
console.log(thread.subject);
}
});

//Get the most recent message
nylas.messages.first({in: 'inbox'}).then(message =>{
console.log(`Subject: ${message.subject} | ID: ${message.id} | Unread: ${message.unread}`);
});

Explore the Nylas Email API

If you’ve made it this far, congrats, you’ve successfully synced email data with the Nylas Email API! 🎉 There is plenty more that you can do with it: take a look at the following resources to learn more about the Nylas Communications Platform capabilities.