Skip to content

API Quickstart

This tutorial will walk you through creating a functional app with Nylas by setting up user authentication using the Nylas API in just a few simple steps. The guide primarily focuses on user authentication, but also provides code samples at the end to accomplish common tasks such as reading or sending emails and creating calendar events using Nylas APIs.

There are also Github repos of sample apps available in your preferred language towards the end of this guide, which you can clone and launch in seconds. 🚀

Prerequisites

Before you begin, make sure you have:

  • Nylas Developer Account: Sign up here if you don’t have one
  • OAuth Redirect URI: A web server URL that Nylas will redirect to after user login (local or production)
  • Nylas Client ID and API Key: Obtainable from your Nylas Dashboard

1. Bootstrap your app

Create your ENV file

Create an .env file. You’ll use this file to store credentials and constants that you want to avoid including in your source code.

Your Nylas API URI should be either https://api.us.nylas.com if you’re using the U.S. region, or https://api.eu.nylas.com if you’re using the E.U. region.

Install your dependencies

Install the Nylas SDK of your choice, along with the dependencies for this project.

Configure the Nylas SDK

Set up the Nylas SDK, using some of the environment variables from your .env file.

Next up, you’ll log an end user into your application.

2. Authentication

Register an authentication callback URI

Next, you will register a callback URI with Nylas, which Nylas will redirect the user to when the authentication flow completes. This callback route or page is where we will add logic to finalize the authentication.

  1. In the Nylas Dashboard, click Hosted Authentication in the left navigation, and click the Callback URI tab.
  2. Click Add a callback URI, and enter your application’s callback URI.
Hosted authentication screen showing the Callback URIs tab, and a freshly added entry for a localhost callback URI.

Redirect your user to the provider’s login screen

The first step of the authentication process is to start an authorization request. Usually, this is a button or link in your application that the end user clicks.

When the user initiates this process, your server should use the Nylas SDK to make an auth URL, which you will then redirect your user to. This will kick off the authentication process, with Nylas handling the user experience.

Example of Nylas hosted authentication experience, showing what the end-user will see

Nylas offers several ways to customize your OAuth flow. For this guide, we’ve kept it simple, but you can learn more in the Nylas API documentation.

Receive a callback from Nylas with the user’s grant ID

If the user successfully authenticates, they will be taken to your Redirect URI that you’ve specified, with a special one-time-use code in the URL query (i.e. http://localhost:3000/auth-success?code=12345). On your server you’ll then pass that code to Nylas (called “token exchange”) to finalize the authentication, which will give you a unique Grant ID that you can use for API calls.

Here are some examples of how you can execute the token exchange:

When this process completes successfully, Nylas marks the end user’s grant as verified and sends you their grant ID and email address. Phew!

The next section is the fun part! Now that you’ve authenticated your user and have a grant ID, you can use the Nylas Email, Calendar and Contacts API. In the following sections, we will see how we can achieve some common tasks like reading and sending emails and creating calendar events!

3. Use the Nylas APIs

Email API

Read email messages
Send email messages

Calendar API

List events on primary calendar
Create events on primary calendar

Contacts API

Read contacts
Create contacts