Skip to content

Manage folders and labels with Python

This page explains how to organize an email inbox with folders and labels using the Nylas Python SDK and Email API.

Before you begin

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

List folders and labels

Depending on the end user’s email provider, there are two possible ways their inbox might be organized: using either folders or labels. Gmail uses labels, and all other providers use folders. Nylas consolidates both folders and labels under the Folders endpoint.

The following example lists all folders and labels from an end user’s inbox.

folders = nylas.folders.list(
'<NYLAS_GRANT_ID>'
)
print(folders)

Create folders and labels

The following example creates a folder and applies it to the most recent email message in the end user’s inbox.

from dotenv import load_dotenv
load_dotenv()
import os
import sys
from nylas import Client
nylas = Client(
os.environ.get('NYLAS_API_KEY'),
os.environ.get('NYLAS_API_URI')
)
folder = nylas.folders.create(
'<NYLAS_GRANT_ID>',
request_body={
"name": 'New Folder XYZ',
"parent": None,
}
)
print(folder)
message = nylas.messages.update(
'<NYLAS_GRANT_ID>',
'<MESSAGE_ID>',
request_body={
"folders": ['<FOLDER_ID>']
}
)
print(message)