Only show these results:

Node SDK

Star

The Node SDK guide will take you through set up and making requests with the Node SDK. The SDK is open-source. If you want to contribute file an issue or open a pull request.

Prerequisites

Install the Nylas Node.js SDK

Run npm install nylas or yarn add nylas.

Congrats! You’re now ready to write code with the Nylas Node.js SDK.

Initializing Nylas

Every resource is accessed using an instance of Nylas. Before making any requests, be sure to call config and initialize the Nylas instance with your <CLIENT_ID> and <CLIENT_SECRET>. Then, call with and pass it your <ACCESS_TOKEN>. The <ACCESS_TOKEN> allows Nylas to make requests for a given account's resources.

const Nylas = require('nylas');

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

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

Test Your Installation

Let’s return your account details.

  const Nylas = require('nylas');

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

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

nylas.account.get().then(account => console.log(account));
  {  
id: '5tgncdmczat02216u7d6uypyi',
object: 'account',
accountId: '5tgncdmczat02216u7d6uypyi',
name: 'Katherine Johnson',
emailAddress: 'kat@spacetech.com',
provider: 'gmail',
organizationUnit: 'label',
syncState: 'running',
linkedAt: 2020-12-14T18:10:14.000Z
}

Enviroment Variables

Before going to production, make sure you use environment variables.

Callbacks

Every resource method accepts an optional callback as the last argument.

  nylas.threads.list({}, (err, threads) => {
console.log(threads.length);
});

Promises

Every resource method returns a promise, so you don't have to use callbacks if your code is promise-friendly.

nylas.threads.list({}).then(threads => {
console.log(threads.length);
});

Async/Wait

You can also use async and wait.

const getThreadCount = async nylas => {
const threads = await nylas.threads.list({});
return threads.length;
};

Access Tokens and Client IDs

Some endpoints don’t need an access token since they only access application-level information. You can skip adding the access token if you don’t need account information.

const Nylas = require('nylas');

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

// Return all accounts connected to your Nylas App.
Nylas.accounts.list().then(accounts => {
for (let account of accounts) {
console.log(
`Email: ${account.emailAddress} | `,
`Billing State: ${account.billingState} | `,
`Sync State: ${account.syncState}`,
`ID: ${account.id} | `
);
}
});

Learn More

Read the API reference for the accounts endpoint to learn more about the information you can access for accounts.

Using the Node SDK

View Messages and Threads

const Nylas = require('nylas');

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

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

//Get the most recent message
nylas.messages.first({in: 'inbox'}).then(message =>{
console.log(message.subject);
// Log the Nylas global ID for the message
console.log(message.id);
// Log the service provider ID of the message
console.log(message.unread);
});

// Return the 10 most recent threads
nylas.threads.list({limit: 10}).then(threads =>{
for (let thread of threads) {
console.log(thread.subject);
console.log(thread.participants);
}
});

// List the 5 most recent unread email threads
nylas.threads.list({unread: true, limit: 5}).then(threads =>{
for (let thread of threads) {
console.log(thread.subject);
}
});

Learn More

View the API reference for messages and threads to learn more. You can also check out the Email API guide to learn more about the functionality of the Nylas Email API.

Search Messages and Threads

const Nylas = require('nylas');

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

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

nylas.messages.search("from:swag@nylas.com").then(messages => {
for (let message of messages) {
console.log(message.subject);
}
});

nylas.threads.search("A really important thing").then(threads => {
for (let thread of threads) {
console.log(thread.subject);
}
});

Learn More

View the API reference for search to learn more. You can also check out the Email API guide to learn more about the functionality of the Nylas Email API.

Create an Event

const Nylas = require('nylas');

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

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

const event = nylas.events.build({
title: 'New Years Party!',
// calendarID must be the ID for a calendar the user has write access to.
calendarId: <CALENDAR_ID>,
// Event times use UTC timestamps
// This example creates an event on December 31, 2018
when: { start_time: 1546290000, end_time: 1546300800 },

// Participants are stored as an array of participant subobjects
participants: [{ email: 'swag@nylas.com', name: 'My Nylas Friend' }],
location: 'My House!'
});

// Event notification emails are not sent by default
// Enable notify_participants to send an email notification to participants
event.save({ notify_participants: true }).then(event => {
console.log(event);
});

Learn More

Check out the API reference docs for calendars and events to learn more about the features of these endpoints. You can also take a look at the Calendar API guide to learn more about the functionality of the Nylas Calendar API.

Create and Update Webhooks

const Nylas = require('nylas');

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

// create a new webhook
const newWebhook = Nylas.webhooks.build({
callbackUrl: 'https://wwww.myapp.com/webhook',
state: 'active',
triggers: ['event.created', 'event.updated'],
});
newWebhook.save().then(webhook => console.log(webhook.id));

// get and update the state of an existing webhook
// note: a webhook's callbackUrl & triggers are not updateable, only its state.
Nylas.webhooks.find('existingWebhookId').then(existingWebhook => {
existingWebhook.state = 'active';
existingWebhook.save();
})

// delete an existing webhook
Nylas.webhooks.delete(existingWebhook);

What’s Next?