Simplest way to retrieve messages (including trash)

I need to use a polling strategy (as opposed to webhooks) in order to sync emails into my app. I need to ensure that all emails to/from a contact are captured, including emails that have already been sent to TRASH (i.e. for Gmail). It appears that GET messages does not natively return any messages in TRASH. GET threads does include message id’s that are in trash. However, if I have to GET threads and then turn around and use a GET message for each and every message in each thread, the number of api calls is too much.

Is there any way to simply get all messages sent/received after a certain date (i.e. the last time a sync occurred) for a list of email addresses…including messages in TRASH?

Hi @neteffect!

To sync all emails to/from including TRASH for Gmail, you don’t need to GET /threads first. Instead:

Using these four requests you should be able to retrieve all the messages you’re interested in.

Also, you may be able to shrink this down to just two queries if you use the search_query_native parameter because I believe Gmail has an OR operator. See their native syntax docs here: Refine searches in Gmail - Computer - Gmail Help. And here is our full guide on using Nylas’ search_query_native: Searching with Nylas | Nylas Docs

Got it…thanks. In an effort to minimize the number of api calls (and make this applicable for all providers, not just Gmail), can you just confirm that the below approach is technically accurate:

  1. GET /threads (which also includes TRASH) where a) batch list of emails (contacts) included in the any_email and b) last_message_after is > the last time we ran the GET /threads for that user and c) select=id
  2. For each returned thread_id, run a GET /threads/[thread_id] and filter through the results for any new messages since last pull

Actually…let me rephrase the question:

  1. Does your original suggestion work for all providers (not just Gmail)?
  2. Instead of those 4 calls, can I use the any_email and batch 25 emails?
  3. Lastly, in the GET messages/ is the received_after parameter ONLY for received messages or is it both received and sent?

I’m just answering your second set of questions below:

  1. It should work for all providers.
  2. any_email batching should work for your use case, but for Google the messages in TRASH will still be excluded unless you specify otherwise.
  3. It’s for both received and sent.