Only show these results:

Send email with the Nylas Node.js SDK

The Nylas 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 send email directly from users' email accounts.

This guide explains how to use the Nylas Node.js SDK and Email API to send email with JavaScript.

What you'll learn

In this tutorial, you'll learn how to do the following:

  • Create a draft.
  • Send the draft as an email message.

Prerequisites

Prerequisites include installing the Nylas Node.js SDK and setting up your environment. Before you start this tutorial, make sure that you do the following:

Step 1: Initialize the Nylas object

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 Nylas APIs. Before making any request, 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 (<CLIENT_ID>) and client secret (<CLIENT_SECRET>).

  2. Call the with function and pass your access token (<ACCESS_TOKEN>).

const Nylas = require('nylas');

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

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

Step 2: Change the base API URL

You can optionally 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.

To change the base URL to the EU (Ireland) region, set apiServer: regionConfig[Region.Ireland].nylasAPIUrl. The base API URL defaults to the US region.

const Nylas = require('nylas');

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

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

Step 3: Create a draft and send an email

  1. Create a draft object and assigns subject, body and the 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 contain names and email addresses. You can use to for cc and bcc.
    • You can also add file attachments, message tracking features, and reply-to values. Learn more about Drafts.
  2. Send the draft as an email message.

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

Example: Create a draft and send an email

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`);
});

Video walkthrough

Prefer video? You can watch a short video on getting set up with Nylas and sending your first email using the Nylas Node.js SDK.

More resources

If you’ve made it this far, congrats! You’ve successfully learned how to create a draft and send an email with the Nylas Email API! There's plenty more that you can do with Nylas. Take a look at the following resources: