This page explains how to organize an email inbox with folders and labels using the Nylas Python SDK and Email API.
Before you begin
Section titled “Before you begin”Before you start, you must have done the following tasks:
- Installed and set up the Nylas Python SDK.
- Authenticated one or more users.
List folders and labels
Section titled “List folders and labels”Depending on the 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 a user’s inbox.
folders = nylas.folders.list( '<NYLAS_GRANT_ID>')
print(folders)
Create folders and labels
Section titled “Create folders and labels”The following example creates a folder and applies it to the most recent message in the user’s inbox.
from dotenv import load_dotenvload_dotenv()
import osimport sysfrom 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)