Skip to content Skip to sidebar Skip to footer

Nylas Mail We Were Unable to Apply the Changes to Your Thread Please Try Again Later

Nylas Node.js SDK Travis build status

Installation

Install the Nylas SDK:

npm install nylas or yarn add nylas

API Overview

Every resource (i.due east., letters, events, contacts) is accessed via an example of Nylas. Earlier making any requests, be sure to phone call config and initialize the Nylas instance with your clientId and clientSecret. Then, telephone call with and pass it your accessToken. The accessToken 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 ) ;

Every resources method accepts an optional callback every bit the final argument:

                

nylas . threads . listing ( { } , ( err , threads ) => {

panel . log ( threads . length ) ;

} ) ;

Additionally, every resource method returns a promise, then you don't have to utilize callbacks if your code is hope-friendly. Hither's an case using promises:

                

nylas . threads . listing ( { } ) . then ( threads => {

console . log ( threads . length ) ;

} ) ;

And here's an example using async/await:

                

const getThreadCount = async nylas => {

const threads = wait nylas . threads . listing ( { } ) ;

return threads . length ;

} ;

Authentication

The Nylas Rest API uses server-side (iii-legged) OAuth, and the Node.js bindings provide convenience methods that simplify the OAuth process. For more than information nigh authenticating users with Nylas, visit the API docs.

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

  • loginHint - The user'due south email address, if known.
  • state - An arbitrary string that will exist returned dorsum every bit a query param in your redirectURI.
  • scopes - An array of which scopes yous'd like to auth with. The Nylas API provides granular hallmark scopes that empower users with control over what level of admission your awarding 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 . go ( ' /connect ' , ( req , res , next ) => {

  options = {

    redirectURI : ' http://localhost:3000/oauth/callback ' ,

    scopes : [ ' email.read_only ' , ' electronic mail.send ' ] ,

} ;

res . redirect ( Nylas . urlForAuthentication ( options ) ) ;

} ) ;

Footstep 2: Handle the Authentication Response

                

router . get ( ' /oauth/callback ' , ( req , res , next ) => {

if ( req . query . code ) {

Nylas . exchangeCodeForToken ( req . query . code ) . then ( token => {

} ) ;

} else if ( req . query . error ) {

res . render ( ' error ' , {

      message : req . query . reason ,

      mistake : {

        status :

' Please effort authenticating again or use a unlike electronic mail account. ' ,

        stack : ' ' ,

} ,

} ) ;

}

} ) ;

Getting IP Addresses to Whitelist

To obtain a dynamic listing of IP addresses that Nylas might apply to connect.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

Nylas . accounts . offset ( )

. then ( account => account . ipAddresses ( ) )

. and then ( response => console . log ( response ) ) ;

Fetching Messages, Events, Contacts, etc.

The Node.js SDK exposes API resources (threads, letters, folders, labels, files, events, contacts, etc.) as attributes of the nylas object. You tin query these resources in several ways. Available filters can be establish in the API docs.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . threads . commencement ( {  from : EMAIL_ADDRESS } ) . and then ( thread => {

console . log ( thread . subject ) ;

console . log ( thread . snippet ) ;

} ) ;

nylas . threads . count ( {  in : ' inbox ' } ) . then ( count => {

console . log ( ` In that location are ${ count }  threads in your inbox. ` ) ;

} ) ;

nylas . threads

. detect ( THREAD_ID )

. then ( thread => {

console . log ( thread . field of study ) ;

} )

. catch ( err => {

panel . log ( ` Thread non constitute! Error: ${ err . toString ( ) } ` ) ;

} ) ;

nylas . threads . notice ( THREAD_ID , ( err , thread ) => {

if ( err ) {

console . log ( ` Thread not found! Error: ${ err . toString ( ) } ` ) ;

return ;

}

panel . log ( thread . subject area ) ;

} ) ;

nylas . threads . forEach (

{  unread : imitation ,  from : ' chaiskye@gmail.com ' } ,

thread => console . log ( thread . subject ) ,

err => console . log ( ' Finished iterating through threads. ' )

) ;

nylas . threads . listing ( {  in : ' inbox ' } ) . and so ( threads => {

panel . log ( threads ) ;

} ) ;

Folders and Labels

The folders and labels API allows y'all to utilize Gmail labels to whole threads or individual messages and, for providers other than Gmail, to motion threads and letters between folders.

Note that folders and labels are identical from the standpoint of the SDK. The only difference is that a message can have many labels only only a single folder.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . labels . list ( { } ) . then ( labels => {

for ( const characterization of  labels ) {

console . log ( label . displayName ) ;

console . log ( label . id ) ;

}

} ) ;

nylas . folders . list ( { } ) . and so ( folders => {

for ( const binder of  folders ) {

panel . log ( folder . displayName ) ;

console . log ( folder . id ) ;

}

} ) ;

const fld = nylas . folders . build ( {  displayName : ' Reminders ' } ) ;

fld . save ( ) ;

permit  spamLabel = undefined ;

nylas . labels . list ( { } ) . and then ( labels => {

for ( const characterization of  labels ) {

if ( label . displayName == ' Spam ' ) {

      spamLabel =  label ;

break ;

}

}

nylas . threads . list ( { } , ( err , threads ) => {

const thread =  threads [ 0 ] ;

thread . labels . push ( spamLabel ) ;

thread . save ( ) ;

console . log ( thread ) ;

} ) ;

} ) ;

File Metadata

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const f = nylas . files . build ( {

  id :  fileId ,

} ) ;

f . metadata ( ( err , data ) => {

panel . log ( data ) ;

} ) ;

On success, the file metadata should look like:

              {   "content_type": "application/msword",   "filename": "Reinstatement of Corporation.medico",   "id": "9tm2n206vdj29wrhcxfvmvo4o",   "message_ids": [     "93mtrpk4uo3wsvwcpb5yh57kp"   ],   "account_id": "6aakaxzi4j5gn6f7kbb9e0fxs",   "object": "file",   "size": 100864 }                          

Uploading Files

Because of a bug in the library we use to issue HTTP requests, we can't pass a stream to the file upload part, which is why we read the file direct.

                

const fs = require ( ' fs ' ) ;

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

fs . readFile ( filePath , ' utf8 ' , ( err , data ) => {

  f = nylas . files . build ( {

    filename :  filePath ,

    information :  data ,

    contentType : ' text/plain ' ,

} ) ;

f . upload ( ( err , file ) => {

const typhoon = nylas . drafts . build ( {

      subject : ' Water ice Foam ' ,

      to : [ {  email : ' helena@nylas.com ' } ] ,

      trunk : ' Hey, find the file attached. ' ,

} ) ;

draft . files = [ file ] ;

draft . send ( ) . then ( message => {

console . log ( ` ${ bulletin . id }  was sent ` ) ;

} ) ;

} ) ;

} ) ;

Downloading Files

                

const fs = crave ( ' fs ' ) ;

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const f = nylas . files . build ( {

  id :  fileId ,

} ) ;

f . download ( ( err , file ) => {

fs . writeFile ( ' /tmp/ ' + file . filename , file . body ) ;

} ) ;

Creating and Sending Drafts

You tin create, relieve, and send drafts. To send, showtime create a draft object with the correct fields (To/CC/BCC, subject area, body, etc.), and then call transport. When the draft is sent, the Nylas API will return a Message object.

If you want to send a answer, set replyMessageId to the ID of the message to which yous're replying. When that field is ready, the Nylas API volition set email headers to mark your message every bit a respond.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const draft = nylas . drafts . build ( {

  subject : ' My New Typhoon ' ,

  to : [ {  email : ' ben@nylas.com ' } ] ,

  replyToMessageId : MESSAGE_ID ,

} ) ;

const tracking = {

" links " : truthful ,

" opens " : truthful ,

" thread_replies " : truthful ,

" payload " : " 12345 "

}

draft . transport ( nothing ,  tracking ) . then ( bulletin => {

console . log ( ` ${ message . id }  was sent ` ) ;

} ) ;

typhoon . relieve ( ) . then ( draft => {

console . log ( ` ${ typhoon . id }  was saved ` ) ;

} ) ;

const savedId = ' 1234 ' ;

nylas . drafts

. find ( savedId )

. so ( draft => draft . transport ( ) )

. and so ( message => {

console . log ( ` Sent ${ bulletin . subject } ! ` ) ;

} ) ;

Searching Threads and Messages

You lot can run a full-text search on threads and messages using search and passing a string to query. By default, the Nylas API returns forty results, but y'all can pass a limit and offset to perform pagination.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . messages . search ( ' Hey! ' ) . then ( messages => console . log ( messages ) ) ;

Creating and Updating Webhooks

Y'all can programmatically create, read, update and delete webhooks.

                

const newWebhook = Nylas . webhooks . build ( {

  callbackUrl : ' https://wwww.myapp.com/webhook ' ,

  state : ' agile ' ,

  triggers : [ ' event.created ' , ' result.updated ' ] ,

} ) ;

newWebhook . save ( ) . then ( webhook => panel . log ( webhook . id ) ) ;

Nylas . webhooks . discover ( ' existingWebhookId ' ) . and so ( existingWebhook => {

existingWebhook . country = ' active ' ;

existingWebhook . save ( ) ;

} )

Nylas . webhooks . delete ( existingWebhook ) ;

Using the Delta Streaming API

                

const DELTA_EXCLUDE_TYPES = [ ' contact ' , ' calendar ' , ' outcome ' , ' file ' , ' tag ' ] ;

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . deltas . latestCursor ( ( error , cursor ) => {

persistCursor ( cursor ) ;

const stream = nylas . deltas . startStream ( cursor , DELTA_EXCLUDE_TYPES ) ;

  stream

. on ( ' delta ' , delta => {

console . log ( ' Received delta: ' ,  delta ) ;

persistCursor ( delta . cursor ) ;

} )

. on ( ' fault ' , err => {

console . error ( ' Delta streaming error: ' ,  err ) ;

} ) ;

stopButton . addEventListener ( ' click ' , ( ) => {

stream . close ( ) ;

} ) ;

} ) ;

Interacting with Events

You lot can send calendar invites to events using the Nylas API. To send invites and updates to the event's participants, prepare notify_participants to true.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const event = nylas . events . build ( {

  title : ' Out of time ' ,

  calendarId : CALENDAR_ID ,

  when : {  start_time : 1437500000 ,  end_time : 1437501600 } ,

  participants : [ {  email : ' helena@nylas.com ' ,  name : ' Helena Handbasket ' } ] ,

} ) ;

event . save ( {  notify_participants : truthful } ) . and so ( event => {

console . log ( result ) ;

panel . log ( ' Sent an invite to the participants ' ) ;

} ) ;

nylas . events

. find ( EVENT_ID )

. then ( consequence => event . rsvp ( ' maybe ' , ' I may attend this effect ' ) )

. then ( event => console . log ( ' RSVP sent! ' ) ) ;

Sending and Retrieving Raw MIME

To send raw MIME, you can build a typhoon and, instead of providing the normal fields, pass the MIME in an object as rawMime.

To retrieve the raw MIME for an business relationship'south message, call getRaw on the message object, and the MIME will be returned in a promise.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const typhoon = nylas . drafts . build ( {  rawMime } ) ;

typhoon . ship ( ) . then ( message => console . log ( message ) ) ;

nylas . messages

. outset ( )

. and then ( bulletin => message . getRaw ( ) )

. then ( rawMessage => console . log ( rawMessage ) ) ;

Accounts

It'south possible to get details almost the business relationship you're accessing by using the account method:

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . account . go ( ) . then ( account => console . log ( business relationship ) ) ;

You can access the billing condition and cancel/reactivate an account for the accounts in your app by using the accounts method:

                

Nylas . accounts . list ( ) . and so ( accounts => {

console . log ( accounts . length ) ;

for ( const account of  accounts ) {

console . log (

business relationship . id ,

account . billingState ,

account . syncState

) ;

}

} ) ;

Nylas . accounts

. first ( )

. then ( account => account . downgrade ( ) )

. then ( response => console . log ( response ) ) ;

Nylas . accounts

. offset ( )

. then ( business relationship => account . upgrade ( ) )

. so ( response => console . log ( response ) ) ;

Nylas . accounts

. start ( )

. then ( account => business relationship . revokeAll ( ' kept_access_token ' ) )

. then ( response => console . log ( response ) ) ;

Open-Source API

The Nylas Sync Engine is open-source, and you tin also employ the Node.js SDK with the open-source API. Since the open-source API provides no authentication or security, connecting to information technology is simple.

It requires us to "auth" to it by passing the business relationship id as an auth token. Here's an example of fetching the messages of the kickoff business relationship after getting the business relationship ID:

                

const Nylas = require ( ' nylas ' ) ;

Nylas . config ( {

  clientId : ' clientId ' ,

  clientSecret : ' clientSecret ' ,

  apiServer : ' http://localhost:5555 ' ,

} ) ;

Nylas . accounts . first ( ) . then ( account => {

const nylas = Nylas . with ( account . id ) . messages . list (

{  limit : 20 } ,

( err , messages ) => {

for ( const message of  letters ) {

console . log ( message . bailiwick ) ;

}

}

) ;

} ) ;

Instance Apps

We have a few example Express apps in the case directory that show examples for authentication and webhooks. Yous can run them to see how they're implemented:

npm install or yarn

npm showtime or yarn outset

Note that you'll need to replace the Nylas app ID and app surreptitious in app.js or create a config.js file with your application'south credentials.

Contributing

We'd love your help making the Nylas Node.js SDK better. Come chat in the Nylas customs Slack channel or email support@nylas.com.

Please sign the Contributor License Agreement earlier submitting pull requests. (Information technology'southward similar to other projects, like NodeJS or Shooting star.)

Tests can be run with:

npm test

Our linter can exist run with:

npm run lint

To use the package during local development, symlink the directory:

npm link in the nylas-nodejs directory npm link nylas in the directory with your lawmaking that uses the package

leistpatents.blogspot.com

Source: https://www.npmjs.com/package/nylas/v/4.7.0?activeTab=readme

Post a Comment for "Nylas Mail We Were Unable to Apply the Changes to Your Thread Please Try Again Later"