- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
This blog post contains code for the Nylas API V2. Take a look at your latest docs to see Nylas API V3 Email Docs updated to work with Nylas API V3.
In this post, we are going to learn how to use the Nylas Node SDK to read emails. This is useful to simplify integrating with email providers with minimal coding. We’ll first set up your environment, and then we’ll start reading our inbox using JavaScript.
Sign up here if you need to create a Nylas account for free! Follow the Quickstart to receive your account credentials. Ensure to save the access token to use below.
If you have used the Nylas Node SDK before, continue to the next section. If you need to configure your environment, take a look at the Environment setup from our code samples repository. Alternatively, you can check out this post on How to Send Emails with Nylas Node SDK to configure your environment.
Now let’s read some emails from our Inbox! Let’s create readEmail.js
using a code editor:
// node-email-read/readEmail.js // Import your dependencies import dotenv from "dotenv/config.js"; import Nylas from "nylas"; // Configure your Nylas client Nylas.config({ clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, }); const nylas = Nylas.with(process.env.ACCESS_TOKEN); // Read your messages try { const messages = await nylas.messages; const messageList = await messages.list({ in: 'Inbox', limit: 5 }); messageList.map((message) => { const date = new Date(message.date).toLocaleDateString(); console.log(`[${date}] ${message.subject}`); }); } catch (err) { console.error("Error:\n", err); }
The Nylas Node SDK wraps the Nylas API to retrieve a list of 5 emails from our Inbox. Let’s try calling readEmails
via the terminal:
$ node readEmails.js [6/16/2022] Company A, Inc. has gone live on Nylas with their users [6/16/2022] Joe S has joined your meeting - using Nylas SDKs [6/16/2022] Ram has joined your meeting - shipping with Nylas APIs [6/16/2022] How to Send Notifications for Calendar Events using the Nylas SDK [6/16/2022] Accepted: shipping with Nylas @ Thu Jun 16, 2022 5pm - 5:30pm (EDT)
If everything worked, you will see the message in the terminal where five emails from your Inbox will be listed with a date and subject.
Now we’ve listed five messages using the Nylas Node SDK.
Let’s read all the participants in a thread from our inbox using the Nylas Node SDK. Let’s create readThread.js
:
// node-email-read/readThread.js // Import your dependencies import dotenv from "dotenv/config.js"; import Nylas from "nylas"; // Configure your Nylas client Nylas.config({ clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, }); const nylas = Nylas.with(process.env.ACCESS_TOKEN); // Read your first thread try { const threads = await nylas.threads; const singleThread = await threads.list({ limit: 1 }); singleThread.map(({ participants }) => participants.map(participant => console.log(participant.email)) ); } catch (err) { console.error("Error:\n", err); }
Let’s try calling readThread
via the terminal:
$ node readThread.js <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com <EMAIL>@nylas.com ...and many more
If everything worked, you will see the message in the terminal where all participants for a single thread will be listed.
Now we’ve accessed an email thread and listed all participants using the Nylas Node SDK.
Let’s try searching our inbox using the Nylas Node SDK. Let’s create searchInbox.js
:
// node-email-read/searchInbox.js // Import your dependencies import dotenv from "dotenv/config.js"; import Nylas from "nylas"; // Configure your Nylas client Nylas.config({ clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, }); const nylas = Nylas.with(process.env.ACCESS_TOKEN); // Read messages from a specific sender try { const messages = await nylas.messages; const messageList = await messages.search("from:<EMAIL>"); messageList.map((message) => { const date = new Date(message.date).toLocaleDateString(); console.log(`[${date}] ${message.subject}`); }); } catch (err) { console.error("Error:\n", err); }
As you see in the code, we can use the search operators available from your email search. Make sure you replace the <EMAIL>
field in the searchInbox
code to search for a relevant email address.
Let’s try calling searchInbox
via the terminal:
$node searchInbox.js [6/13/2022] Updated invitation: Nylas SDKs @ Mon Jun 20, 2022 12:30pm - 1pm (EDT) [4/28/2022] Invitation: Talk about Dev.to @ Wed May 4, 2022 12pm - 12:30pm (EDT) [4/19/2022] Join Nylas at Dev.to
If everything worked, you will see the message in the terminal where email messages from a specific sender will be listed with a date and subject.
You can find example code on the Nylas Samples code repository. Continue building with Nylas and learn more by visiting the Node SDK documentation.
Ram loves teaching, building and exploring technologies. He is passionate about empowering developers to ship amazing products to market as fast as possible ????. Ram is excited to share knowledge and help others. He’s a Relaxed Tomato ????.