The Clean Messages endpoint removes extra information from messages (for example, quoted replies, signatures, and images) and returns only the body text that you need. Cleaned messages are well suited to training AI and machine learning models with email data. You can also use them to trigger automations, create chat-like views of threads, and more.
How message cleaning works
Section titled “How message cleaning works”When you make a request to the Clean Messages endpoint, Nylas processes up to 20 messages and returns each one with its cleaned body text in the conversation field. Nylas always removes extraneous information such as HTML <script> and <style> tags. You can use the following configuration parameters to further specify the information Nylas returns:
ignore_links: Whentrue, removes links in the message or signature. Defaults totrue.ignore_images: Whentrue, removes images from the message or signature. Defaults totrue.images_as_markdown: Whentrue, Nylas returns images as Markdown links. Defaults totrue, orfalsefor Agent Accounts.ignore_tables: Whentrue, removes tables from the message or signature. Defaults totrue.remove_conclusion_phrases: Whentrue, removes phrases such as “Best” and “Regards” in the signature. Defaults totrue.html_as_markdown: Whentrue, converts the message to markdown. Defaults tofalse.
You set each of these options on a per-request basis, so no need to worry about deciding how you want all of your cleaned messages to look. For more information, see the Clean Messages references.
Use cleaned messages to train AI models
Section titled “Use cleaned messages to train AI models”Raw message bodies contain quoted replies, signatures, tracking images, and HTML markup that add noise to a training dataset. The Clean Messages endpoint returns only the text that the sender wrote in each message, which you can use to do the following tasks:
- Build datasets to train or fine-tune machine learning models.
- Generate embeddings for search and retrieval.
- Pass email content to a large language model (LLM) as context.
Set html_as_markdown and images_as_markdown to true to keep headings and lists in Markdown format. To collect cleaned text as new messages arrive, subscribe to the message.created.cleaned webhook trigger.
You train models with the cleaned text in your own systems. Nylas doesn’t use your messages or the cleaned text to train models.
How Nylas handles message data when cleaning
Section titled “How Nylas handles message data when cleaning”Nylas cleans messages when you make the request and returns the result in the response. It doesn’t cache or store the cleaned text, so each request processes the message again. If you need the same cleaned text more than once, store it in your application. A single request can include up to 20 message IDs.
Cleaning runs on Nylas infrastructure in the data region of your application: U.S. (Iowa) or Europe (London). Nylas doesn’t send message content to third-party services to clean it, and the endpoint doesn’t use generative AI or large language models (LLMs).
Nylas removes HTML, quoted replies, and forwarded content with fixed rules. When remove_conclusion_phrases is true, it also uses a pre-trained classifier to detect signatures and sign-offs. The classifier runs on Nylas infrastructure. Nylas doesn’t use your messages or the cleaned text to train this classifier or any other model.
Before you begin
Section titled “Before you begin”To follow along with the samples on this page, you first need to sign up for a Nylas developer account, which gets you a free Nylas application and API key.
For a guided introduction, you can follow the Getting started guide to set up a Nylas account and Sandbox application. When you have those, you can connect an account from a calendar provider (such as Google, Microsoft, or iCloud) and use your API key with the sample API calls on this page to access that account’s data.
You’ll also need to set up a provider auth app (Google or Microsoft Azure) and connector with at least the following scopes:
- Google:
gmail.readonly - Microsoft:
Mail.Read - EWS:
ews.messages
Clean messages
Section titled “Clean messages”Make a Clean Messages request that includes the IDs of up to 20 messages. Nylas returns the cleaned message in the conversation field and any images in the attachments object.
curl --compressed --request PUT \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/messages/clean' \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer <NYLAS_API_KEY>' \ --data '{ "message_id": ["<MESSAGE_ID>"], "ignore_links": true, "ignore_images": true, "images_as_markdown": false, "ignore_tables": true, "remove_conclusion_phrases": true, "html_as_markdown": false }'{ "request_id": "5fa64c92-e840-4357-86b9-2aa364d35b88", "data": [ { "body": "<div dir=\"ltr\"><div>Hello, I just sent a message using Nylas! <br></div><img src=\"cid:ii_ltppe5ph0\" alt=\"Nylas-Logo.png\" width=\"540\" height=\"464\"><br><div dir=\"ltr\" class=\"gmail_signature\" data-smartmail=\"gmail_signature\"><div dir=\"ltr\"></div></div></div>\r\n", "cc": [ { "name": "Leyah Miller", "email": "leyah@example.com" } ], "date": 1635355739, "attachments": [ { "is_inline": true, "id": "<ATTACHMENT_ID>", "grant_id": "<NYLAS_GRANT_ID>", "filename": "Nylas-Logo.png", "content_type": "image/png; name=\"Nylas-Logo.png\"", "content_disposition": "inline; filename=\"Nylas-Logo.png\"", "content_id": "<CID>", "size": 26044 } ], "folders": ["<FOLDER_ID>", "<FOLDER_ID>"], "from": [ { "name": "Nylas", "email": "nylas@example.com" } ], "grant_id": "<NYLAS_GRANT_ID>", "id": "<MESSAGE_ID>", "object": "message", "reply_to": [ { "name": "Nylas", "email": "nylas@example.com" } ], "snippet": "Hello, I just sent a message using Nylas!", "starred": true, "subject": "Hello from Nylas!", "thread_id": "<THREAD_ID>", "to": [ { "name": "Nyla", "email": "nyla@example.com" } ], "unread": true, "conversation": "Hello, I just sent a message using Nylas!" } ]}Before and after Clean Message request
Before

After
Unsubscribe \n\n \nHi there,\n\nNylas API v3 is now generally available!
The new infrastructure introduces: \n\n * Instant email and event
functionality, boosting performance, security, and scalability.\n *
Simplified integrations such as API keys for auth, enhanced
notifications, Microsoft Graph, and more to streamline engineering
efforts even further.\n * New email features such as Bounce Detection,
Smart Compose, Scheduled Send, and custom domain for email tracking
links to improve email deliverability and efficiency.\n\nCheck out Nylas
docs and join our webinar on Feb 15 to learn more, and start\nmigrating
today!\n\nHave questions about migrating? Contact your dedicated Nylas
customer success\nmanager (CSM) or email customeronboarding@nylas.com.
\n\n© 2024 Nylas Inc. All rights reserved.\n\nPrivacy Policy |
Copyright | Unsubscribe\n\n2100 Geng Rd. #210, Palo Alto, CA 94303
Return a cleaned message as Markdown
Section titled “Return a cleaned message as Markdown”When you set html_as_markdown and images_as_markdown to true in your request, Nylas formats the cleaned message as Markdown and returns it in the conversation field. For Agent Accounts, Nylas returns the Markdown in the email_as_markdown field instead.
Clean messages with images
Section titled “Clean messages with images”When you specify a message that contains images in a Clean Message request, you can choose how Nylas handles the images. If you want to return them, set ignore_images to false. If you want to return the image tags as Markdown links, set images_as_markdown to true and ignore_images to false.
Nylas returns inline images in the conversation field as part of the cleaned message, and includes their content ID (cid):
- Inline image:
"conversation": "<img src='cid:1781777f666586677621' />\n\nImage from Gmail" - Inline image as Markdown:
"conversation": "\n\nImage from Gmail"
Nylas returns some inline images with a link to the original source, if available. If the link isn’t available, Nylas returns the cid.
Nylas doesn’t return image attachments as part of a cleaned message. You can find information about attached images in the attachments object.
Download images from a cleaned message
Section titled “Download images from a cleaned message”To download an image that was included in a cleaned message, make a Download Attachment request that includes the attachment and message IDs. Nylas returns the image as a binary data blob.
curl --compressed --request GET \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/attachments/<ATTACHMENT_ID>/download?message_id=<MESSAGE_ID>' \ --header 'Authorization: Bearer <NYLAS_API_KEY>'Get notifications for cleaned messages
Section titled “Get notifications for cleaned messages”Instead of polling the Clean Messages endpoint, you can subscribe to the message.created.cleaned webhook trigger to receive cleaned message content automatically when new messages are synced. The webhook payload includes the cleaned markdown in the body field, plus a cleaning_status field indicating whether cleaning succeeded.
Subscribing to message.created.cleaned does not suppress message.created notifications. If you subscribe to both triggers, Nylas sends two separate notifications for each new message: one with the original HTML body and one with the cleaned content.
To use cleaned message webhooks, configure Clean Conversation settings for your application and subscribe to the message.created.cleaned trigger. For more information, see the message.created.cleaned notification reference.
Keep in mind
Section titled “Keep in mind”Keep the following things in mind as you work with the Clean Message endpoint:
- Nylas currently detects English-language conclusion phrases only when you set
remove_conclusion_phrasestotrue. - Nylas removes any reply and forward content from the message, and returns only the latest message in the thread.