Nylas Node.js Bindings
Installation
npm install nylas
Example Express App
A small example Express app is included in the example
directory. you can run the sample app to see how an authentication flow might be implemented:
cd example
npm install
DEBUG=example:* ./bin/www
Note that you'll need to replace the Nylas App ID and Secret in app.js
with your application's credentials.
API Overview
Every resource is accessed via an instance of Nylas
. Before making any requests, be sure to call config
and initialize the Nylas
with your App ID
and App Secret
.
var Nylas = require('nylas').config({
appId: 'c96gge1jo29pl2rebcb7utsbp',
appSecret: 'l2rebcb7utsbpc96gge1jo29p'
});
Every resource method accepts an optional callback as the last argument:
Nylas.with(accessToken).threads.list({}, function(err, threads){
console.log(threads.length);
});
Additionally, every resource method returns a promise, so you don't have to use callbacks if the rest of your code is promise-friendly:
Nylas.with(accessToken).threads.list({}).then(function(threads){
console.log(threads.length);
});
Authentication
The Nylas REST API uses server-side (three-legged) OAuth, and the Node.js bindings provide convenience methods that simplifiy the OAuth process. For more information about authenticating users with Nylas, visit the Developer Documentation
Step 1: Redirect the user to Nylas:
var Nylas = require('nylas').config({
appId: 'c96gge1jo29pl2rebcb7utsbp',
appSecret: 'l2rebcb7utsbpc96gge1jo29p'
});
router.get('/connect', function(req, res, next) {
options = {
redirectURI: 'http://localhost:3000/oauth/callback',
trial: false
}
res.redirect(Nylas.urlForAuthentication(options));
});
Step 2: Handle the Authentication Response:
router.get('/oauth/callback', function (req, res, next) {
if (req.query.code) {
Nylas.exchangeCodeForToken(req.query.code).then(function(token) {
});
} else if (req.query.error) {
res.render('error', {
message: req.query.reason,
error: {
status: 'Please try authenticating again or use a different email account.',
stack: ''
}
});
}
});
Fetching Threads, Messages, etc.
The Javascript SDK exposes API resources as attributes of the nylas
object. You can query these resources in several ways. Available filters can be found in the API Documentation
var nylas = Nylas.with(accessToken);
nylas.threads.first({from: 'ben@nylas.com'}).then(function(thread) {
console.log(thread.subject);
console.log(thread.snippet);
})
nylas.threads.count({tag: 'inbox'}).then(function(count) {
console.log('There are ' + count + 'threads in your Nylas.');
})
nylas.threads.find('c96gge1jo29pl2rebcb7utsbp').then(function(thread) {
console.log(thread.subject);
}).catch(function(err) {
console.log('Thread not found! Error: ' + err.toString());
});
nylas.threads.find('c96gge1jo29pl2rebcb7utsbp', function(err, thread) {
if (err) {
console.log('Thread not found! Error: ' + err.toString());
return;
}
console.log(thread.subject);
});
nylas.threads.forEach({tag: 'unread', from: 'no-reply@sentry.com'}, function(err, thread) {
console.log(thread.subject);
}, function (err) {
console.log('finished iterating through threads');
});
nylas.threads.list({tag: 'inbox'}).then(function(threads) {
});
Folders and labels
The new folders and labels API replaces the now deprecated Tags API. It allows you to apply Gmail labels to whole threads or individual messages and, for providers other than Gmail, to move threads and messages between folders.
var nylas = Nylas.with(accessToken);
nylas.labels.list({}).then(function(labels) {
console.log(label.displayName);
console.log(label.id);
})
nylas.folders.list({}).then(function(folders) {
console.log(folder.displayName);
console.log(folder.id);
})
fld = nylas.folders.build({ displayName: 'Reminders'});
fld.save();
var label = undefined;
nylas.labels.list({}).then(function(labels) {
for(var i = 0; i < labels.length; i++) {
label = labels[i];
if (label.displayName == 'Junk Email') {
break;
}
}
nylas.threads.list({}, function(err, threads) {
var thread = threads[0];
thread.labels.push(label);
thread.save();
})
Uploading files
var nylas = Nylas.with(accessToken);
var fs = require('fs');
fs.readFile(filePath, 'utf8', function (err, data) {
f = nylas.files.build({
filename: filePath,
data: data,
contentType: 'text/plain'
});
f.upload(function(err, file) {
var draft = nylas.drafts.build({
subject: 'Ice-cream',
to: [{email: 'helena@nylas.com'}],
body: 'Hey, find the file attached.',
});
draft.files = [file];
draft.send().then(function(draft) {
console.log(draft.id + ' was sent');
});
});
});
Creating and Sending Drafts
var nylas = Nylas.with(accessToken);
var draft = nylas.drafts.build({
subject: 'My New Draft',
to: [{email: 'ben@nylas.com'}]
replyToMessageId: "8i4u7sid2ygvfso9gomu9rcw3"
});
draft.send().then(function(draft) {
console.log(draft.id + ' was sent');
});
draft.save().then(function(draft) {
console.log(draft.id + ' was saved');
});
var savedId = '1234';
nylas.drafts.find(savedId).then(draft) {
draft.send().then(function (draft) {
console.log('sent!');
});
});
Using the Delta Streaming API
var DELTA_EXCLUDE_TYPES = ['contact', 'calendar', 'event', 'file', 'tag'];
var nylas = Nylas.with(accessToken);
nylas.deltas.latestCursor(function(error, cursor) {
persistCursor(cursor);
var stream = nylas.deltas.startStream(cursor, DELTA_EXCLUDE_TYPES);
stream.on('delta', function(delta) {
console.log('Received delta:', delta);
persistCursor(delta.cursor);
}).on('error', function(err) {
console.error('Delta streaming error:', err);
});
stopButton.addEventListener('click', function() {
stream.close();
});
})
Interacting with Events
var nylas = Nylas.with(accessToken);
var ev = nylas.events.build({
title: 'Out of time',
calendarId: 'c4y597l3adg8mskfqxxns8hsj',
when: {'start_time': 1437500000, 'end_time': 1437501600},
participants: [{email: 'karim@nylas.com', name: "Karim Hamidou"}]
});
ev.save({'sendNotifications': true}, function(err, event) {
console.log('Sent an invite to the participants');
});
nylas.events.find('30xunbe3033d44kip9bnau5ph').then(function(ev) {
ev.rsvp('maybe', 'I may attend this event').then(function(ev) {
console.log('RSVP sent!');
});
});
Open source API
The Nylas Sync Engine is open source, and you can also use the Node SDK with the open source API. Since the open source API provides no authentication or security, connecting to it is simple.
Nylas = require('nylas').config({
appId: 'appId---doesnt matter when running locally',
appSecret: 'appSecret---same',
apiServer: 'http://localhost:5555'
});
Nylas.with(null).accounts.first({}, function(err, account) {
var nylas = Nylas.with(account.id).messages.list({limit: 20}, function(err, messages) {
for(var i = 0; i < messages.length; i++) {
console.log(messages[i].subject);
}
});
});
Contributing
We'd love your help making the Nylas Node.js bindings better. Join the Google Group for project updates and feature discussion. We also hang out on the Nylas community Slack channel, or you can email support@nylas.com.
Please sign the Contributor License Agreement before submitting pull requests. (It's similar to other projects, like NodeJS or Meteor.)
Tests can be run with:
npm test