nylas/nylas-nodejs

Question: how to get multiple messages by IDs?

KazimirPodolski opened this issue ยท 5 comments

Here it says one could request multiple messages by IDs in one request: https://developer.nylas.com/docs/api#get/messages/id. Hovewer I cannot understand how to do it with Node SDK.

messages connection has 4 methods which I can somehow relate to getting messages: list, find, range, getItems. However, find which seem to correspond to GET /messages/{id} returns single entity, and the other (corresponding to GET /messages? I don't understand) seem to be unable to request by the list of IDs according to https://developer.nylas.com/docs/api#get/messages.

So, how to get list of messages by their IDs? I can get them one-by-one or threads + single messages, but for the sake of performance and simplicity I would really like to get them in one request.

Seems related to #147.

Hey @KazimirPodolski thanks for opening this issue! Unfortunately we do not have an endpoint that returns messages by a list of ids. You can either fetch one message-at-a-time using NylasConnection.messages.find("{MESSAGE_ID}"); (GET /messages/{MESSAGE_ID}) or fetch all messages using NylasConnection.messages.list(); (GET /messages) and then maybe filtering the list by IDs.

If the messages you are looking to get are part of the same thread, you could be using Threads to get all the messages within a specific thread. The cool thing about this endpoint is if you provide an expanded: true parameter, it would return the Threads object with all the Message objects included (without the expanded: true set, you would get back a list of message_ids which would require you to get each message one-by-one).

For example if you wanted to fetch all of the messages in the thread abc-123 you can do the following,

const Nylas = require('nylas');
Nylas.config({clientId: 'clientId', clientSecret: 'clientSecret'});
const nylas = Nylas.with('access_token');

const thread = await nylas.threads.find('abc-123', {expanded: true});
const messagesInThread = thread.messages;

for(const message in messagesInThread) {
  console.log(`Message ID: ${message.id}, Subject: ${message.subject}, etc.`);
}

More documentation on threads can be found here.

Another approach would be to search messages/threads with a query specific to your provider. This is useful if you know all the messages you want to fetch have a similar theme. More details on this endpoint and the provider-specific query syntax can be found here. A code sample can look like this:

const Nylas = require('nylas');
Nylas.config({clientId: 'clientId', clientSecret: 'clientSecret'});
const nylas = Nylas.with('access_token');

// Getting all threads that were sent to 'nick'
const threads = await nylas.threads.search('to:nick', {expanded: true});
const messagesInThread = thread.messages;

for(const message in messagesInThread) {
  console.log(`Message ID: ${message.id}, Subject: ${message.subject}, etc.`);
}

// Getting all messages that were sent to 'nick'
const messages = await nylas.messages.search('to:nick'});

for(const message in messages) {
  console.log(`Message ID: ${message.id}, Subject: ${message.subject}, etc.`);
}

I hope this answer was sufficient enough to resolve your issue. If it doesn't feel free to re-open this issue!

Unfortunately we do not have an endpoint that returns messages by a list of ids.

@mrashed-dev You mean SDK doesn't have it or API reference is wrong https://developer.nylas.com/docs/api#get/messages/id?

The problem is messages whose IDs I have are related with our internal mechanism, and they could be a number of single messages and a number of threads. I guess I'll go with rest API or loading one-by-one with some cache.

@KazimirPodolski Ah, thanks for clarifying, I think I misunderstood your question.

The REST API does indeed support it but at this time the Nylas API does not, however, I'll add it as a feature request and try to get it released as soon as we can!

@KazimirPodolski Thank you for your patience, this feature is now included in the latest v6.2.0 release (along with #311 and #314). To get multiple messages by a list of message IDs you can invoke the new findMultiple() function as seen in the code sample below:

Nylas.config({clientId: 'clientId', clientSecret: 'clientSecret'});
const nylas = Nylas.with('access_token');

const messages = await nylas.messages.findMultiple(['message_id_1', 'message_id_2']);