Nylas Docs

The Nylas Developer Hub

Welcome to the Nylas developer hub. You'll find comprehensive guides and documentation to help you start working with Nylas as quickly as possible, as well as support if you get stuck. Let's jump right in!

Developer Guide

Send an Email With Node.js

The Nylas Node SDK is the quickest way to integrate email into your app with JavaScript and the Nylas Email API. Build your email integration in 15 minutes.

Node.js has grown to be the most popular framework in the world, primarily due to it’s efficiency for data-intensive real-time applications and the ability to use JavaScript across the entire web stack. The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and our Node SDK makes it simple to send emails directly from user email accounts.

This guide explains how to use the Nylas Node SDK and Email API to send an email with JavaScript. It covers the following steps:

  1. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Node.js SDK
  3. Create and send an email
  4. Explore the Nylas Email API

Create Your Free Developer Account

Ready to build your email integration? Create your Nylas developer account to get started.

Set up Your Developer Account

After signing up for your Nylas developer account, follow our guide to get your API keys and authorize your first email account. When you complete this guide, you will have three tokens that you need to run the code examples found below:

  • CLIENT_ID - The CLIENT ID found on the dashboard page for your Nylas App
  • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App
  • ACCESS_TOKEN - The Access token provided when you authenticate an account to your Nylas App

Install the Nylas Node.js SDK

Prerequisites

  • Get Node.js installed on your machine.
  • Make sure you have npm or yarn.

Install the SDK

Head to the nearest command line and run npm install nylas or yarn add nylas.

Congrats! You’re now ready to start using the Nylas Node.js SDK.

Send an Email

Create a Nylas API Client

At its core, the Nylas Communication Platform is an API client that interfaces with all of the major email providers. The Nylas object provides access to every resource in the Nylas Email API, so the first thing to do is to initialize it:

const Nylas = require('nylas');

Then, call the config function and pass the CLIENT_ID and CLIENT_SECRET from earlier in this guide, replacing the placeholders with your values.

Nylas.config({
    clientId: CLIENT_ID,
    clientSecret: CLIENT_SECRET,
});

Take care with your secrets

It’s not best practice to include secrets like this in your code. For increased security, store these values as environment variables.

Next, call the with function and pass it the ACCESS_TOKEN from earlier in this guide. This allows Nylas to access data for a specific user account.

const nylas = Nylas.with(ACCESS_TOKEN);

Create a Draft

Now, it’s time to build your first draft object:


const draft = nylas.drafts.build({
    subject: 'With Love, from Nylas',
    body: 'This email was sent using the Nylas email API. Visit https://nylas.com for details.',
    to: [{ name: 'My Nylas Friend', email: 'swag@nylas.com' }]
});

This code example creates a new draft object and assigns it a subject and body text. You can also add file attachments, message tracking features, and reply-to values. Take a look at the Nylas API reference to learn more about what you can add to an email draft with the Nylas Email API.

The example also assigns a recipient with the to parameter. It’s an array of email objects that contain names and email addresses. This also works for cc and bcc.

Finally, it’s time to send the email!


draft.send().then(message => {
    console.log(`${message.id} was sent`);
});

The following example combines all of this code into a single block, for easier reference.


const Nylas = require('nylas');

Nylas.config({
    clientId: CLIENT_ID,
    clientSecret: CLIENT_SECRET,
});
  
const nylas = Nylas.with(ACCESS_TOKEN);

const draft = nylas.drafts.build({
    subject: 'With Love, from Nylas',
    to: [{ name: 'My Nylas Friend', email: 'swag@nylas.com' }],
    body: 'This email was sent using the Nylas email API. Visit https://nylas.com for details.'
});

// Send the draft
draft.send().then(message => {
    console.log(`${message.id} was sent`);
});
Ready to Build Your Email Integration?

Explore the Nylas Email API

If you’ve made it this far, congrats, you’ve sent your very first email with the Nylas Email API! There is plenty more that you can do with it: take a look at the following resources to learn more about the Nylas Communications Platform capabilities.

Send an Email With Node.js


The Nylas Node SDK is the quickest way to integrate email into your app with JavaScript and the Nylas Email API. Build your email integration in 15 minutes.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.