Only show these results:

Node SDK Authentication

The Nylas REST API uses server-side OAuth, and the Node.js bindings provide convenience methods that simplify the OAuth process. For more information about authenticating users with Nylas, visit the guide on Authentication.

urlForAuthentication() takes in an options object, which must have a redirectURI property defined. Other supported, but optional, properties are:

  • loginHint - The user's email address, if known.
  • state - An arbitrary string that will be returned back as a query param in your redirectURI.
  • scopes - An array of which scopes you'd like to auth with. The Nylas API provides granular authentication scopes that empower users with control over what level of access your application has to their data. See supported Authentication Scopes for a full list of scopes and details behind the scopes. If omitted, defaults to all scopes.

Step 1: Redirect the User to Nylas

const Nylas = require('nylas');

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

router.get('/connect', (req, res, next) => {
options = {
redirectURI: 'http://localhost:3000/oauth/callback',
scopes: ['email.read_only', 'email.send'],
};
res.redirect(Nylas.urlForAuthentication(options));
});

Step 2: Handle the Authentication Response

router.get('/oauth/callback', (req, res, next) => {
if (req.query.code) {
Nylas.exchangeCodeForToken(req.query.code).then(token => {
// save the token to the current session, save it to the user model, etc.
});
} 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: '',
},
});
}
});

What's Next?