Skip to content

Working with folders and labels

Email providers use either folders or labels to organize and manage messages in an inbox. Nylas determines which method an account uses when it connects, and adjusts its behavior as necessary. This means developers can use the same commands to manage both folders and labels.

This page explains how to work with folders and labels in Nylas.

Folder and label behavior

Folders and labels behave similarly across providers, and Nylas v3 simplifies how you work with them by combining their functions into a single Folders API.

Because many providers structure folders differently, Nylas flattens nested folders and adds the parent_id field to child folders. You can use this data to re-create the folder hierarchy from the provider in your project.

Nylas doesn’t support using keywords or attributes to reference folders on the provider (for example, the in:inbox query returns a 400 error). Instead, you should use ids in your requests to get the data you need:

  1. Make a Get all Folders request.
  2. Inspect the folders Nylas returns, find the one you want to work with, and get its ID.
  3. Use the folder ID in your requests to work with the specific folder.

Common folder attributes

Nylas automatically maps folder attributes (the folder’s name or purpose) to a set of common values: \Archive, \Drafts, \Inbox, \Junk, \Sent, and \Trash. For IMAP providers, Nylas passes the attributes on directly. If a folder doesn’t have one of the standard attributes, Nylas returns an empty attributes array.

You can’t filter for folders based on these attributes, but you can use these values to identify the semantically-same folders across providers. For example, to identify the Sent folder…

  1. Make a Get all Folders request.
  2. Inspect the attributes field for each folder, find the one you want to work with (in this case, /Sent), and get its ID.
  3. Use the folder ID in your requests to work with the specific folder.

View an account’s folders and labels

To get a list of all folders and labels in an account, make a Get all Folders request. Nylas returns, among other things, an id that you can use later to reference specific folders or labels.

Nylas flattens all folders, including sub-folders, into a single list.

You can also use the v3 Nylas SDKs, as in the examples below.

Create folders and labels

To create a folder or label, make a Create Folder request. Depending on the provider, you can define specific parameters to customize the folder or label:

  • Google:
    • text_color: Set the text color for the label.
    • background_color: Set the background color for the label.
  • Microsoft and EWS:
    • parent_id: Set the parent folder. You can use this to create nested folders.

The following example creates a label for a Google account and defines its text_color and background_color.

You can also create folders and labels using the Nylas SDKs.

Organize email messages

To move an email message into a folder or apply a label to it, make an Update Message request that includes the id of both the folder or label and the email message you want to work with. This overwrites the folder the email message is currently organized in.

The following example moves a specific email message into a folder.

You can also do this using the v3 Nylas SDKs, as in the examples below.

Update folders and labels

You can make an Update Folder request to update a folder or label. Depending on the provider, you can update specific parameters to customize the folder or label:

  • Google:
    • text_color: Set the text color for the label.
    • background_color: Set the background color for the label.
  • Microsoft and EWS:
    • parent_id: Set the parent folder. You can use this to create a hierarchy of nested folders.

The following example updates a folder’s name.

You can also use the v3 Nylas SDKs, as in the following examples.

Delete folders and labels

You can delete folders and labels by making a Delete Folder request or using one of the Nylas SDKs.

Provider limitations on folders and labels

Keep the following limitations in mind when you’re working with folders and labels:

  • Because providers structure folders in different ways, Nylas doesn’t support nested folders. Instead, Nylas flattens sub-folders and displays them in the same list as top-level folders.
    • For Microsoft, you can use the parent_id to reflect the folder hierarchy in your project.
    • On IMAP, the hierarchy is reflected in the folder name (for example, Accounting.Taxes or INBOX\Accounting\Taxes).
  • Because of how IMAP providers handle folders and labels, the folder names that Nylas returns aren’t always the same as those listed in the provider’s UI (for example, the “Trash” folder might be “Deleted Messages” in a Nylas response). Instead of relying on names, you should use attributes and IDs to get the data you need.
  • IMAP servers use provider-specific formats to represent the folder name and hierarchy. When you make a Create Folder request for an IMAP account, Nylas creates a folder with the name you pass. If the name includes the IMAP separator that corresponds with the server settings, Nylas creates a sub-folder based on the folder name.
  • Nylas v3 doesn’t support using keywords to reference folders on the provider (for example, in:inbox returns a 400 error). Instead, you should use specific folder ids to get the data you need.
  • It might take up to 10 minutes for folders to be available in Nylas after you authenticate an IMAP grant.