|
| 1 | +--- |
| 2 | +title: "Using the Nylas Email API" |
| 3 | +rootNav: "Email" |
| 4 | +sidebar: |
| 5 | + label: Using the Email API |
| 6 | + parent: Email |
| 7 | + order: 1 |
| 8 | +description: "Use the Nylas Email API to access and work with email data." |
| 9 | +--- |
| 10 | + |
| 11 | +:::success |
| 12 | +**Looking for the Email API references?** [You can find them here](/docs/api/v3/ecc/#tag/messages)! |
| 13 | +::: |
| 14 | + |
| 15 | +This page explains how to use the Nylas Email API. You'll learn how to do the following tasks: |
| 16 | + |
| 17 | +- Read email messages from an account's inbox. |
| 18 | +- Search for email messages. |
| 19 | +- Update an email message's labels, file attachments, unread status, stars, folders, and more. |
| 20 | +- Delete drafts, files, and folders. |
| 21 | + |
| 22 | +## How the Email API works |
| 23 | + |
| 24 | +The Nylas Email API interacts with users' providers using their original SMTP/ActiveSync gateways. This means that when you make a [Send Message request](/docs/api/v3/ecc/#tag/messages/POST/v3/grants/{grant_id}/messages/send), for example, Nylas connects to the provider to send an email message as the user. Because of this, providers see the activity as _the user_ sending a message, rather than an external platform making the request _on the user's behalf_. |
| 25 | + |
| 26 | +Email messages sent through Nylas have very high deliverability, but might be subject to rate-limiting and abuse detection from the provider. See [Improve email deliverability](/docs/dev-guide/best-practices/improving-email-delivery/) for more information and a list of best practices. |
| 27 | + |
| 28 | +### Provider IDs for messages |
| 29 | + |
| 30 | +Nylas v3 uses an object's provider ID to refer to the object, and different providers return differently formatted IDs. For IMAP messages, Nylas extracts the ID from the `message-id` header. For Google and Microsoft, the object ID comes from the provider's internal ID. |
| 31 | + |
| 32 | +## Before you begin |
| 33 | + |
| 34 | +!!!include(src/\_docs_includes/v3-using-api-before-you-begin.md)!!! |
| 35 | + |
| 36 | +### One-click unsubscribe requirements for Google messages |
| 37 | + |
| 38 | +!!!include(src/\_docs_includes/email/v3-google-unsubscribe-headers.md)!!! |
| 39 | + |
| 40 | +## Read email messages from inboxes |
| 41 | + |
| 42 | +Messages are the fundamental object in the Nylas platform, and the core building block for most email applications. They contain several pieces of information, such as the message's timestamp, the sender's address, the recipients, and the body of the message. They can also contain file attachments, calendar invites, and more. |
| 43 | + |
| 44 | +By default, the Messages endpoint returns the 50 most recent messages, but the following examples use the `limit` parameter to reduce the number of results to 5. |
| 45 | + |
| 46 | +```bash [mostRecentMessages-Request] |
| 47 | +!!!include(v3_code_samples/messages/GET/curl.sh)!!! |
| 48 | +``` |
| 49 | + |
| 50 | +```json [mostRecentMessages-Response (JSON)] |
| 51 | +!!!include(v3_code_samples/messages/GET/response.json)!!! |
| 52 | +``` |
| 53 | + |
| 54 | +```js [mostRecentMessages-Node.js SDK] |
| 55 | +!!!include(v3_code_samples/messages/GET/node.js)!!! |
| 56 | +``` |
| 57 | + |
| 58 | +```python [mostRecentMessages-Python SDK] |
| 59 | +!!!include(v3_code_samples/messages/GET/python.py)!!! |
| 60 | +``` |
| 61 | + |
| 62 | +```ruby [mostRecentMessages-Ruby SDK] |
| 63 | +require 'nylas' |
| 64 | + |
| 65 | +nylas = Nylas::Client.new(api_key: '<NYLAS_API_KEY>') |
| 66 | +query_params = { limit: 5 } |
| 67 | +messages, _ = nylas.messages.list(identifier: '<NYLAS_GRANT_ID>', query_params: query_params) |
| 68 | + |
| 69 | +messages.each {|message| |
| 70 | + puts "[#{Time.at(message[:date]).strftime("%d/%m/%Y at %H:%M:%S")}] \ |
| 71 | + #{message[:subject]}" |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```java [mostRecentMessages-Java SDK] |
| 76 | +import com.nylas.NylasClient; |
| 77 | +import com.nylas.models.*; |
| 78 | +import java.text.SimpleDateFormat; |
| 79 | + |
| 80 | +public class ReadInbox { |
| 81 | + public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError { |
| 82 | + NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build(); |
| 83 | + ListMessagesQueryParams queryParams = new ListMessagesQueryParams.Builder().limit(5).build(); |
| 84 | + ListResponse<Message> message = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams); |
| 85 | + |
| 86 | + for(Message email : message.getData()) { |
| 87 | + String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). |
| 88 | + format(new java.util.Date((email.getDate() * 1000L))); |
| 89 | + |
| 90 | + System.out.println("[" + date + "] | " + email.getSubject()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```kt [mostRecentMessages-Kotlin SDK] |
| 97 | +import com.nylas.NylasClient |
| 98 | +import com.nylas.models.* |
| 99 | +import java.text.SimpleDateFormat |
| 100 | +import java.util.* |
| 101 | + |
| 102 | +fun dateFormatter(milliseconds: String): String { |
| 103 | + return SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(Date(milliseconds.toLong() * 1000)).toString() |
| 104 | +} |
| 105 | + |
| 106 | +fun main(args: Array<String>) { |
| 107 | + val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>") |
| 108 | + val queryParams = ListMessagesQueryParams(limit = 5, inFolder = listOf("Inbox")) |
| 109 | + val messages : List<Message> = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams).data |
| 110 | + |
| 111 | + for(message in messages) { |
| 112 | + println("[" + dateFormatter(message.date.toString()) + "] |" + message.subject + " | " + message.folders) |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +:::info |
| 118 | +**Your users get a lot of messages**. If you encounter [`429` errors](/docs/api/errors/400-response/) or [provider rate limits](/docs/dev-guide/platform/rate-limits/#provider-rate-limits) when listing all messages, try lowering your `limit` and adding other parameters to reduce the number of messages returned. |
| 119 | +::: |
| 120 | + |
| 121 | +## Search an inbox for email messages |
| 122 | + |
| 123 | +You can add query parameters to a [Get all Messages request](/docs/api/v3/ecc/#tag/messages/GET/v3/grants/{grant_id}/messages) to search for messages. |
| 124 | + |
| 125 | +:::warn |
| 126 | +**If you're using the `in` query parameter on a Google account to filter for email messages with a specific folder ("label" in the Google UI), you must reference the folder ID**. Nylas does not support filtering by folders using their name. |
| 127 | +::: |
| 128 | + |
| 129 | +```javascript [searchMessagesSDKs-Node.js SDK] |
| 130 | +!!!include(v3_code_samples/messages/GET/search-inbox-messages.js)!!! |
| 131 | +``` |
| 132 | + |
| 133 | +```python [searchMessagesSDKs-Python SDK] |
| 134 | +!!!include(v3_code_samples/messages/GET/search-inbox-messages.py)!!! |
| 135 | +``` |
| 136 | + |
| 137 | +```ruby [searchMessagesSDKs-Ruby SDK] |
| 138 | +require 'nylas' |
| 139 | + |
| 140 | +nylas = Nylas::Client.new(api_key: '<NYLAS_API_KEY>') |
| 141 | +query_params = {limit: 5, search_query_native: "subject: hello"} |
| 142 | +messages, _ = nylas.messages.list(identifier: '<NYLAS_GRANT_ID>', query_params: query_params) |
| 143 | + |
| 144 | +messages.each {|message| |
| 145 | + puts "[#{Time.at(message[:date]).strftime("%d/%m/%Y at %H:%M:%S")}] \ |
| 146 | + #{message[:subject]}" |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +```java [searchMessagesSDKs-Java SDK] |
| 151 | +import com.nylas.NylasClient; |
| 152 | +import com.nylas.models.*; |
| 153 | +import java.text.SimpleDateFormat; |
| 154 | +import com.nylas.models.Thread; |
| 155 | +import java.util.List; |
| 156 | + |
| 157 | +public class SearchInbox { |
| 158 | + public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError { |
| 159 | + NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build(); |
| 160 | + |
| 161 | + ListMessagesQueryParams queryParams = new ListMessagesQueryParams.Builder(). |
| 162 | + searchQueryNative("subject: hello"). |
| 163 | + limit(5). |
| 164 | + build(); |
| 165 | + |
| 166 | + ListResponse<Message> message = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams); |
| 167 | + |
| 168 | + for(Message email : message.getData()) { |
| 169 | + String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). |
| 170 | + format(new java.util.Date((email.getDate() * 1000L))); |
| 171 | + |
| 172 | + System.out.println("[" + date + "] | " + email.getSubject()); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +```kt [searchMessagesSDKs-Kotlin SDK] |
| 179 | +import com.nylas.NylasClient |
| 180 | +import com.nylas.models.* |
| 181 | +import java.text.SimpleDateFormat |
| 182 | +import java.util.* |
| 183 | + |
| 184 | +fun dateFormatter(milliseconds: String): String { |
| 185 | + return SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(Date(milliseconds.toLong() * 1000)).toString() |
| 186 | +} |
| 187 | + |
| 188 | +fun main(args: Array<String>) { |
| 189 | + val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>") |
| 190 | + val queryParams = ListMessagesQueryParams(limit = 5, searchQueryNative = "subject: hello") |
| 191 | + val messages : List<Message> = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams).data |
| 192 | + |
| 193 | + for(message in messages) { |
| 194 | + println("[" + dateFormatter(message.date.toString()) + "] |" + message.subject) |
| 195 | + } |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +## Modify and delete inbox content |
| 200 | + |
| 201 | +Most Nylas Email API endpoints allow you to modify objects using `PUT` and `POST` requests. You can make the following changes: |
| 202 | + |
| 203 | +- **Threads and Messages**: Modify labels, unread status, stars, and folders. See the [Threads](/docs/api/v3/ecc/#tag/threads) and [Messages](/docs/api/v3/ecc/#tag/messages) references for more information. |
| 204 | +- **Folders and Labels**: Update folder and label names. See the [Folders](/docs/api/v3/ecc/#tag/folders) references for more information. |
| 205 | +- **Files**: Upload files to use as attachments. See the [Attachments](/docs/api/v3/ecc/#tag/attachments) references for more information. |
| 206 | + |
| 207 | +You can also make `DELETE` requests to certain endpoints. This allows you to delete existing objects, such as Folders. |
0 commit comments