Quickstart Python Guide

Nylas allows you to integrate your apps with your customers’ email, contacts, and calendars. The applications are as endless as your imagination.

This guide will take you through setting up your development environment and the basic use of the nylas-python module. We will connect to an email account, send a message, and retrieve an email thread.

Set up your development environment

To complete this guide, you will need to have the following installed

If you are already familiar with setting environments and python packages you can skip to the next step of connecting your email account.

Installing Python

If you are on a Mac or Linux machine, you probably already have Python installed. Open a Terminal window and verify this by issuing the following command:

$ which python

If you are using Windows or another platform, or Python is not installed, you can get further instructions on the official Python website.

Installing nylas-python

Create a new project directory, let’s call it first_nylas_app:

$ mkdir first_nylas_app

We recommend using virtual environments to keep module dependencies within your project. If you are not familiar with these, check out this site.

If you have the pip package manager installed, install the virtualenv module as follows:

$ pip install virtualenv

otherwise you can use easy_install:

$ easy_install virtualenv

If you get an error similar to “Permission Denied”, more common if you are not using virtual environments as described above, prepend the command with sudo, ex. sudo pip install virtualenv

From your project director, first_nylas_app, create a virtual environment and activate it:

$ virtualenv .
$ source ./bin/activate

Then, install the nylas module as follows:

$ ./bin/pip install nylas

We will wait until Part II to install the Flask module.

Connecting an Email Account

To connect our app to an email account, we will do the following:

  1. Create a Nylas account
  2. Create an app in your Nylas dashboard to get an app id and secret
  3. Authorize your email account to get a token

If you have not already, go to Nylas.com and sign up for an account. After you have done so, you will be taken to your “App Console”.

From here, select “Create a new app”. Just name it “Test App” for now and leave the other fields blank.

You now can have an App Id and App Secret. Note both of these down as we will be using them in an upcoming step.

Getting your token

The third piece of information we need is a token that we will get by authorizing your email client. Typically this will be done programmatically as outlined in the Authentication section of the API docs, but for our purposes we will keep it simple and get this token through your dashboard.

Click on “Accounts” and then “Add Account”. Use the email address you want to experiment with (it can be your personal one), and click “Authenticate” to go through the flow. After the authentication with your email provider is complete you will see a token, the final piece of information we need. Note this down now and keep it in a safe place.

Creating and Using a Client

Now that we have authorized our email account, it is time to use the Nylas API to do some electronic mail magic. First, we will create a client object using the id, secret, and token we got in the previous step and call methods on that object.

Set environment variables

We want to get in the habit of not hard coding your app secret or tokens. We will set environment variable for these.

$ export NYLAS_APP_ID=<Your App Id>
$ export NYLAS_APP_SECRET=<Your App Secret>
$ export NYLAS_ACCOUNT_TOKEN=<Your Email Account Token>

Using your favorite editor, open a file in this directory and name it app.py.


First we’ll import the client class from the nylas module and the os module

import os
from nylas import APIClient

Then get the app id, secret, and token from the environment variables we set

app_id = os.environ.get('NYLAS_APP_ID')
app_secret = os.environ.get('NYLAS_APP_SECRET')
token = os.environ.get('NYLAS_ACCOUNT_TOKEN')

Now create the client object.

client = APIClient(app_id, app_secret, token)

We now have our client object and call the Nylas API through it. Let’s write some code to send and email and get the subject of the latest thread in our inbox.

# Send an email
draft = client.drafts.create()
draft.to = [{'name': 'Ben', 'email': 'ben.bitdiddle@nylas.com'}]
draft.subject = "Nylas is awesome!"
draft.body = "Can't wait to build using this."

# Get the latest thread in your inbox
thread = client.threads.first()
print "SUBJECT: " + thread.subject

Save the file and run it.

$ python app.py

You should see in you email client that you have sent that email and also see the subject line output.

And so much more

There are many other things we can do through Nylas. Check out the API Documentation and the nylas-python git repo to explore more.

When you are finished exploring a bit, let’s move on to Part II of this guide where we will create a basic Flask app to authenticate an email account through it using callbacks, and use some more advanced features of the Nylas API.

Our first Nylas App

We will be using a python web framework called Flask to host a local webserver so we can demonstrate how to build an app that would call the Nylas API.

Installing Flask

From your project directory install the Flask module as follows:

$ ./bin/pip install flask

Now edit your application file, app.py, in this directory using your favorite editor. We will first just create an endpoint that will show your recent threads.


from flask import Flask, session, request, redirect, Response
from nylas import APIClient
import os

app = Flask(__name__)
app.debug = True
app.secret_key = 'secret'

# This will handle the endpoint http://yourapp.com/recent-threads
def recent_threads():
    app_id = os.environ.get('APP_ID')
    app_secret = os.environ.get('APP_SECRET')
    account_token = os.environ.get('ACCOUNT_TOKEN')

    # Connect to the Nylas API, creating a client object
    client = APIClient(app_id, app_secret, account_token)

    # List the 5 most recent unread threads
    response = ""
    for thread in client.threads.where(unread=True, limit=5):
        response += thread.subject
        response += "<br></br>"

    return response

if __name__ == '__main__':
    app.run(host='', port=5555)
Have more questions? Submit a request