Only show these results:

Send email messages with the Nylas Node.js SDK

The Nylas Node.js SDK is the quickest way to integrate email account data into your app with JavaScript and the Nylas Email API. The Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and the Nylas Node.js SDK makes it easy to read data directly from users' email accounts.

This page discusses how to send email with the Nylas Node.js SDK and the Email API.

What you'll learn

In this tutorial, you'll learn how to create a Draft and send it as an email message.

Prerequisites

Before you start, install the Nylas Node.js SDK and set up your environment:

Step 1: Initialize the Nylas object

At its core, the Nylas 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 API. Before you make any API requests, you must initialize the Nylas object with your client ID, client secret, and access token:

  1. Call the .config() function and pass your <CLIENT_ID> and <CLIENT_SECRET>.
  2. Call the .with() function and pass your <ACCESS_TOKEN>.

All together, your code should look like the example below.

const Nylas = require('nylas');

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

const nylas = Nylas.with(<ACCESS_TOKEN>);

Step 2: (Optional) Change the base API URL

You can choose to change the base API URL depending on your location:

Location API URL Scheduler API URL
United States (Oregon) https://api.nylas.com https://api.schedule.nylas.com
Europe (Ireland) https://ireland.api.nylas.com https://ireland.api.schedule.nylas.com

For more information, see Data residency and Migration guide for data centers.

If you're using the Node.js SDK v6.x and the Nylas API v3 Beta: The EU region is currently not available.

To change the API URL, pass the API_URL parameter to Nylas.config():

const Nylas = require('nylas');

Nylas.config({
clientId: <CLIENT_ID>,
clientSecret: <CLIENT_SECRET>,
apiServer: regionConfig[Region.Ireland].nylasAPIUrl
});

const nylas = Nylas.with(<ACCESS_TOKEN>);

If you're using the Nylas EU region: The base API URL defaults to the US region, but you can specify apiServer: regionConfig[Region.Ireland].nylasAPIUrl to use the EU region. See Data Residency for more information.

Step 3: Send an email message

You can create a Draft using Nylas, and send it as an email message. You can also reply to email messages, and attach files to an email message.

Create and send a Draft

Follow these steps to create a Draft and send it:

  1. Create a draft object and assign the subject, body, and recipient details (to):

    const { default: Draft } = require('nylas/lib/models/draft');

    const draft = new Draft(nylas, {
    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: '[email protected]' }]
    });
    • The to parameter is an array of email objects that contains names and email addresses. You can use to for cc and bcc.
  2. Use draft.send() to send the Draft as an email message.

Example: Create and send a Draft

The following example creates a Draft email message called "With Love, from Nylas" and sends it to a single recipient.

const Nylas = require('nylas');
const { default: Draft } = require('nylas/lib/models/draft');

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

const nylas = Nylas.with(ACCESS_TOKEN);

const draft = new Draft(nylas, {
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: '[email protected]' }]
});

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

More resources

If you’ve made it this far, congrats! You know now how to create a Draft and send it as an email message with the Nylas Email API! 🎉

There's plenty more that you can do with Nylas. Take a look at these resources:

Video walkthrough

Prefer video? You can watch our Send an email with Node.js/JavaScript tutorial.