Nylas Mail- We Were Unable to Apply the Changes to Your Thread

Nylas Node.js SDK Travis build status

Installation

Install the Nylas SDK:

npm install nylas or yarn add nylas

API Overview

Every resource (i.eastward., messages, events, contacts) is accessed via an case of Nylas. Before making whatever requests, exist sure to call config and initialize the Nylas case with your clientId and clientSecret. And then, call with and laissez passer it your accessToken. The accessToken allows Nylas to make requests for a given account'due south resource.

                

const Nylas = require ( ' nylas ' ) ;

Nylas . config ( {

  clientId : CLIENT_ID ,

  clientSecret : CLIENT_SECRET ,

} ) ;

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

Every resource method accepts an optional callback as the final argument:

                

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

console . log ( threads . length ) ;

} ) ;

Additionally, every resource method returns a promise, so yous don't have to apply callbacks if your code is promise-friendly. Hither's an example using promises:

                

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

console . log ( threads . length ) ;

} ) ;

And here'southward an example using async/expect:

                

const getThreadCount = async nylas => {

const threads = await nylas . threads . list ( { } ) ;

return threads . length ;

} ;

Authentication

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

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

  • loginHint - The user'south email address, if known.
  • land - An capricious cord that volition be returned back equally a query param in your redirectURI.
  • scopes - An array of which scopes y'all'd similar to auth with. The Nylas API provides granular authentication scopes that empower users with control over what level of access your awarding has to their data. Run across supported Hallmark Scopes for a full list of scopes and details behind the scopes. If omitted, defaults to all scopes.

Step i: Redirect the user to Nylas

                

const Nylas = crave ( ' nylas ' ) ;

Nylas . config ( {

  clientId : CLIENT_ID ,

  clientSecret : CLIENT_SECRET ,

} ) ;

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

  options = {

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

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

} ;

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

} ) ;

Step 2: Handle the Authentication Response

                

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

if ( req . query . lawmaking ) {

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

} ) ;

} else if ( req . query . mistake ) {

res . render ( ' fault ' , {

      message : req . query . reason ,

      error : {

        status :

' Please try authenticating once more or use a dissimilar email account. ' ,

        stack : ' ' ,

} ,

} ) ;

}

} ) ;

Getting IP Addresses to Whitelist

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

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

Nylas . accounts . commencement ( )

. and then ( business relationship => account . ipAddresses ( ) )

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

Fetching Messages, Events, Contacts, etc.

The Node.js SDK exposes API resource (threads, messages, folders, labels, files, events, contacts, etc.) as attributes of the nylas object. You can query these resources in several means. Available filters can exist found in the API docs.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . threads . beginning ( {  from : EMAIL_ADDRESS } ) . and so ( thread => {

console . log ( thread . subject ) ;

console . log ( thread . snippet ) ;

} ) ;

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

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

} ) ;

nylas . threads

. observe ( THREAD_ID )

. then ( thread => {

console . log ( thread . subject field ) ;

} )

. catch ( err => {

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

} ) ;

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

if ( err ) {

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

return ;

}

console . log ( thread . subject ) ;

} ) ;

nylas . threads . forEach (

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

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

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

) ;

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

panel . log ( threads ) ;

} ) ;

Folders and Labels

The folders and labels API allows you to apply Gmail labels to whole threads or individual letters and, for providers other than Gmail, to motion threads and messages between folders.

Note that folders and labels are identical from the standpoint of the SDK. The only difference is that a bulletin can have many labels but simply a unmarried folder.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

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

for ( const characterization of  labels ) {

panel . log ( label . displayName ) ;

console . log ( characterization . id ) ;

}

} ) ;

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

for ( const folder of  folders ) {

console . log ( folder . displayName ) ;

console . log ( folder . id ) ;

}

} ) ;

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

fld . save ( ) ;

let  spamLabel = undefined ;

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

for ( const label of  labels ) {

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

      spamLabel =  label ;

break ;

}

}

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

const thread =  threads [ 0 ] ;

thread . labels . push ( spamLabel ) ;

thread . relieve ( ) ;

panel . log ( thread ) ;

} ) ;

} ) ;

File Metadata

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const f = nylas . files . build ( {

  id :  fileId ,

} ) ;

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

console . log ( data ) ;

} ) ;

On success, the file metadata should look like:

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

Uploading Files

Considering of a bug in the library we apply to issue HTTP requests, nosotros can't laissez passer a stream to the file upload function, which is why we read the file straight.

                

const fs = require ( ' fs ' ) ;

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

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

  f = nylas . files . build ( {

    filename :  filePath ,

    data :  information ,

    contentType : ' text/plain ' ,

} ) ;

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

const draft = nylas . drafts . build ( {

      subject : ' Ice Cream ' ,

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

      body : ' Hey, discover the file attached. ' ,

} ) ;

typhoon . files = [ file ] ;

draft . transport ( ) . then ( bulletin => {

panel . 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

Yous can create, save, and send drafts. To send, beginning create a draft object with the correct fields (To/CC/BCC, subject area, trunk, etc.), and then call send. When the draft is sent, the Nylas API will return a Message object.

If y'all want to ship a reply, set replyMessageId to the ID of the message to which you're replying. When that field is set, the Nylas API will set up electronic mail headers to mark your message as a respond.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const draft = nylas . drafts . build ( {

  subject : ' My New Draft ' ,

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

  replyToMessageId : MESSAGE_ID ,

} ) ;

const tracking = {

" links " : true ,

" opens " : truthful ,

" thread_replies " : true ,

" payload " : " 12345 "

}

draft . ship ( nada ,  tracking ) . then ( message => {

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

} ) ;

draft . save ( ) . so ( draft => {

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

} ) ;

const savedId = ' 1234 ' ;

nylas . drafts

. find ( savedId )

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

. then ( message => {

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

} ) ;

Searching Threads and Messages

You can run a full-text search on threads and messages using search and passing a string to query. By default, the Nylas API returns 40 results, but you can laissez passer a limit and offset to perform pagination.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

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

Creating and Updating Webhooks

You can programmatically create, read, update and delete webhooks.

                

const newWebhook = Nylas . webhooks . build ( {

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

  land : ' agile ' ,

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

} ) ;

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

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

existingWebhook . state = ' agile ' ;

existingWebhook . save ( ) ;

} )

Nylas . webhooks . delete ( existingWebhook ) ;

Using the Delta Streaming API

                

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

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

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

persistCursor ( cursor ) ;

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

  stream

. on ( ' delta ' , delta => {

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

persistCursor ( delta . cursor ) ;

} )

. on ( ' error ' , err => {

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

} ) ;

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

stream . shut ( ) ;

} ) ;

} ) ;

Interacting with Events

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

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

const result = 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 . salve ( {  notify_participants : true } ) . and then ( event => {

console . log ( event ) ;

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

} ) ;

nylas . events

. find ( EVENT_ID )

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

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

Sending and Retrieving Raw MIME

To send raw MIME, you lot 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's message, call getRaw on the message object, and the MIME will be returned in a promise.

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

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

draft . send ( ) . then ( message => console . log ( bulletin ) ) ;

nylas . messages

. first ( )

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

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

Accounts

It'southward possible to get details about the account y'all're accessing past using the account method:

                

const nylas = Nylas . with ( ACCESS_TOKEN ) ;

nylas . account . get ( ) . then ( account => console . log ( account ) ) ;

You can access the billing status and cancel/reactivate an business relationship for the accounts in your app by using the accounts method:

                

Nylas . accounts . list ( ) . then ( accounts => {

panel . log ( accounts . length ) ;

for ( const account of  accounts ) {

console . log (

account . id ,

account . billingState ,

business relationship . syncState

) ;

}

} ) ;

Nylas . accounts

. offset ( )

. then ( business relationship => business relationship . downgrade ( ) )

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

Nylas . accounts

. starting time ( )

. so ( account => account . upgrade ( ) )

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

Nylas . accounts

. start ( )

. and then ( account => account . revokeAll ( ' kept_access_token ' ) )

. and so ( response => panel . log ( response ) ) ;

Open-Source API

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

It requires usa to "auth" to it by passing the account id as an auth token. Here'due south an example of fetching the messages of the first account after getting the account ID:

                

const Nylas = crave ( ' nylas ' ) ;

Nylas . config ( {

  clientId : ' clientId ' ,

  clientSecret : ' clientSecret ' ,

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

} ) ;

Nylas . accounts . get-go ( ) . then ( account => {

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

{  limit : 20 } ,

( err , messages ) => {

for ( const message of  letters ) {

console . log ( message . subject ) ;

}

}

) ;

} ) ;

Example Apps

We take a few example Limited apps in the case directory that show examples for authentication and webhooks. You tin run them to meet how they're implemented:

npm install or yarn

npm offset or yarn get-go

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

Contributing

We'd love your assist making the Nylas Node.js SDK ameliorate. Come up chat in the Nylas community Slack channel or email back up@nylas.com.

Delight sign the Contributor License Understanding before submitting pull requests. (It'south similar to other projects, similar NodeJS or Meteor.)

Tests tin can be run with:

npm examination

Our linter can be 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 code that uses the package

sextonquelly.blogspot.com

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

0 Response to "Nylas Mail- We Were Unable to Apply the Changes to Your Thread"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel