Only show these results:

Outlook and Exchange Folders with Node.js

Learn how to manage Outlook and Exchange folders with the Nylas Node SDK. 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 modify Outlook and Exchange Folders.

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. Modify Outlook and Exchange Folders
  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.

Manage Outlook and Exchange Folders

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 .with() 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);   

View Folders For an Outlook and Exchange Account

The following example logs the display_name and id for all folders an Outlook or Exchange account contains.

nylas.account.get().then(account =>{
if (account.organizationUnit == 'folder') {
nylas.folders.list({}).then(folders => {
console.log("This account contains the following folders:")
for (const folder of folders) {
console.log(`Name: ${folder.displayName} | ID: ${folder.id}`);
}
});
}
});

Let’s break this example down.

Outlook and Exchange organize email messages into folders like most other email providers. This is different from providers like Gmail that use labels for inbox organization. Nylas makes it easy to detect the organization method an account uses by providing an organizationUnit on the account, which returns either folder or label. In this example, if (account.organizationUnit == 'folder')checks to see if the account uses folders before proceeding. Take a look at the API reference for the Account endpoint to learn more.

Then, nylas.folders.list({}) returns all the folders the account has. Outlook and Exchange typically include a few default folders, including Inbox, Drafts, Trash, Sent Items, and more; this command also returns any custom folders the user has created. Finally, folder.displayName and folder.id are attributes the folders endpoint returns, and this example logs them to the console. Check out the API reference for the folders endpoint to learn more about what it returns.

For convenience, here is the entire code example to read email folders from an inbox.

const Nylas = require('nylas');
Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);

nylas.account.get().then(account =>{
if (account.organizationUnit == 'folder') {
nylas.folders.list({}).then(folders => {
console.log("This account contains the following folders:")
for (const folder of folders) {
console.log(`Name: ${folder.displayName} | ID: ${folder.id}`);
}
});
}
});

Organize an Email Inbox With Folders

First, define a name to use for the new label. We also need to define folderToUpdate to be used later, but don’t worry about it for now.

Next, check the account endpoint, which returns either folder or label in the organizationUnitfield. This code proceeds only if folder is the unit for the account.

nylas.account.get().then(account => {
if (account.organizationUnit == 'folder') {
nylas.folders.forEach({}, checkfolder, createAndApplyfolder);
}
});

Then, we’ll execute a few functions to check if a folder with the same name we defined already exists, creates a new folder if it doesn’t, and move the most recent email into the folder.

The next example includes two functions. The first checks if a folder exists with the name provided earlier. The second function creates the folder if it doesn’t exist and applies it to the most recent email.

const { default: Folder } = require('nylas/lib/models/folder');

function checkfolder (folder) {
if (folder.displayName == folderName) {
folderToUpdate = folder;
};
}
function createAndApplyFolder () {
if ( !folderToUpdate ) {
console.log(`Creating New folder: ${folderName}`)
folderToUpdate = new Folder(nylas, {displayName: folderName});
folderToUpdate.save().then(folder => {
addMostRecentMessageToFolder(folder);
});
} else {
console.log(`${folderName} already exists!`)
addMostRecentMessageToFolder(folderToUpdate);
}
}

The checkFolder (folder) function uses folder.displayName to return the name that is displayed for the folder in user interfaces; this is used to compare to the name that was defined earlier.

After checking to make sure the folder we want doesn’t already exist, createAndApplyFolder() creates a new folder, providing the name we defined earlier for the required displayName attribute. Then, .save() is used to save the changes to the end user inbox. Take a look at the API reference for Folders to learn more about creating folders.

Finally, this example calls addMostRecentMessageToFolder() to move the most recent email to this folder:

function addMostRecentMessageToFolder (folder) {
nylas.messages.first().then(msg => {
console.log(`${folder.displayName} applied to the most recent email.`)
msg.folder = folder;
msg.save().then(savedMsg => {
console.log(`Subject: ${savedMsg.subject}`);
console.log("This email contains the following folders")
console.log(savedMsg.folder);
})
})
}

This function uses nylas.messages.first() to get the most recent email from the inbox. Then, it assigns message.folder to the folder that was provided to this function to move the message to the folder we want. Next, .save() is used to save this change to the Outlook or Exchange account. Finally it logs the subject line and the folder the message is in so we can see the results of this script. Take a look at the API reference for messages to learn more about the features this endpoint provides.

Here is the entire code example to create an Outlook or Exchange folder and move the most recent email into it.

const Nylas = require('nylas');
const { default: Folder } = require('nylas/lib/models/folder');

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);

// Assign a string to use for the display name of the new folder
const folderName = 'Cat Pics!';
let folderToUpdate;
nylas.account.get().then(account => {
// Most email providers, like Outlook and Exchange, use folders to organize inbox content
// account.organizationUnit will return 'label' or 'folder' depending on provider capabilities
// Account object reference:/docs/api/#tag--Accounts
if (account.organizationUnit == 'folder') {
nylas.folders.forEach({}, checkfolder, createAndApplyFolder);
}
});
function checkfolder (folder) {
if (folder.displayName == folderName) {
folderToUpdate = folder;
};
}
function createAndApplyFolder () {
// Make sure that we aren't duplicating an existing folder name
if ( !folderToUpdate ) {
// If the folder doesn't exist, create it and pass it to the function that moves it to the most recent email
console.log(`Creating New folder: ${folderName}`)
folderToUpdate = new Folder(nylas, {displayName: folderName});
folderToUpdate.save().then(folder => {
addMostRecentMessageToFolder(folder);
});
} else {
// If the folder already exists, pass it to the function that assigns it to the most recent email
console.log(`${folderName} already exists!`)
addMostRecentMessageToFolder(folderToUpdate);
}
}
function addMostRecentMessageToFolder (folder) {
nylas.messages.first().then(msg => {
console.log(`${folder.displayName} applied to the most recent email.`)
// Add the folder to the most recent email and save it.
msg.folder = folder;
msg.save().then(savedMsg => {
console.log(`Subject: ${savedMsg.subject}`);
// Outlook, Exchange, and other providers have default folders that often include things like
// Inbox, Drafts, Trash, Sent Items, and more
console.log("This email contains the following folders")
console.log(savedMsg.folder);
})
})
}

Explore the Nylas Email API

If you’ve made it this far, congrats, you’ve successfully modified Outlook and Exchange folders 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.