Embed Full Email CRUD for Folders and Labels with the Nylas Python SDK

Learn how to edit email folders and labels with the Nylas Python SDK.

python nylas api

Helping users stay organized is worth its weight in gold – but we all know that building full sync and CRUD features between your app and the backbone of your users’ daily life (their email inbox) is painstakingly slow and challenging without the help of an API.

With email usage and users only growing, there’s no better time to start working with an API to build full email features into your app. This is the year you help your users get organized—and what’s a better place to start than by creating or updating the units of organization? In this blog post, we’ll take a look at how to do just that by creating and updating inbox folders and labels using the Nylas Python SDK

Setup Steps

All you need are three tokens to follow along with the code examples. To get these, sign up for a Nylas developer account and follow our guide to get your API keys and authorize your first email account. At the end of the guide, you’ll have your CLIENT_ID (found on the dashboard page for your Nylas App), CLIENT_SECRET (found on the dashboard page for your Nylas App) and ACCESS_TOKEN (provided when you authenticate an account to your Nylas App).

Next, ensure you have pip installed on your development environment and create a virtual environment to install Nylas. You can find instructions on how to do this here.

Folders vs. Labels

You may be wondering: what’s the difference? If your user’s account is Gmail, the account’s organization unit is a label. All other providers, including Microsoft, Yahoo, and IMAP providers, use folders

Creating Folders and Labels

Before creating a folder or label, you may want to call the /account endpoint to simply check which organization unit is relevant for your purposes. 

In order to do this, we want to do what we always do when using the Python SDK: configure the API Client – this is how the Nylas Communication Platform interfaces with all of the major providers. You have the CLIENT_ID, CLIENT_SECRET, and ACCESS_TOKEN from setting up your account earlier in ‘Setup Steps’, and just need to pass those in here:

from nylas import APIClient

nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN
)

Now, you can simply instantiate the account object:

account = nylas.account

The following attributes are available for the Account object, so now you can  print account.provider and it will return the provider, e.g. gmail, as a string.

account.id
account.account_id
account.object
account.name
account.email_address
account.provider
account.organization_unit
account.sync_state
account.linked_at

Now, say your user, Carmela, wants to label all incoming emails from the very important Leon Tallparrot so that his missives don’t ever get lost in the mix. Let’s take a look at what you’ll implement in your application code to ensure she doesn’t ever miss an email from him.

 In your app’s interface, Carmela creates a label with the name ‘Emails from Tallparrot’ – so, using the Python SDK, we’re going to create a new label “Emails from Tallparrot” on the backend so that it’ll be available for Carmela to apply to any emails that come into the inbox from “mister.tallparrot@gmail.com”.

if nylas.account.organization_unit == 'label': 


    label = nylas.labels.create()

    label.display_name = ‘Emails from Tallparrot’

    label.save()


elif nylas.account.organization_unit == 'folder': 


    folder = nylas.folders.create()

    folder.display_name = ‘Emails from Tallparrot’

    folder.save()

The Python SDK’s .create() and .save() methods together are equivalent to calling the POST /events endpoint in the Nylas API. Store the ID of the label you just created  – we will use this later when applying the label to the email.

Searching and Labeling

Using the nifty .search() and .add_label() methods available to you via the Python SDK, it’s easy to enable Carmela when she applies the newly created label to an email received from Leon Tallparrot.  Simply pass in the pertinent email address to the ‘from’ field. In our case, it would be ‘mister.tallparrot@gmail.com’. Then, index in to grab the first email of the emails returned and store it in variable ‘message’. 

message = nylas.messages.search("from:mister.tallparrot@gmail.com")[0]

Next, recall that we were returned the ID of the newly created label when we created the label “Emails from Tallparrot” above.  This is what you pass into the .add_label() method in order to apply it to the email we stored in the message variable. 

message.add_label(label.id)

Now Carmela never has to worry about missing an email from Leon Tallparrot again! For more examples on managing your folders, check out our tutorial here. To learn more about how to read your email inbox with Python, check out our guide here.

Ready to Further Explore the Nylas API?

Get Started – Build your first integration with Nylas in 15 minutes.

Quickstart Guides – Get up to speed quickly with our SDKs for Python, Node.js, and Ruby or explore the Nylas Email, Calendar, and Contacts APIs.

How Nylas Works – Take a look at the Nylas architecture to see how we sync billions of emails.

Tutorials – Check out our tutorials to learn how to carry out common functionality like creating, reading and RSVP-ing to calendar events.

Integration Guides – Our integration guides cover what it takes to incorporate email functionality into your app. They cover best practices for using the Nylas Communications Platform and provider-specific advice for Google, Microsoft, IMAP, and more.

Set up Postman – Postman makes it easy to explore the Nylas Email API.

You May Also Like

How to create and read Webhooks with PHP, Koyeb and Bruno
How to create a scheduler with Python and Taipy
How to create and read Google Webhooks using Kotlin

Subscribe for our updates

Please enter your email address and receive the latest updates.