The Attachments APIs allow you to download or get the metadata about an existing attachment, but you use the Messages or Drafts APIs to add or modify the attachments.
What counts as an attachment?
Section titled “What counts as an attachment?”In messages, an attachment is any file that is included either inline as part of a message, or attached as a separate file to the message.
However, major email providers such as Google and Microsoft have their own cloud storage (“drive”) services, which usually appear as links in the message body instead of attachments on the message object. Google Drive allows users to attach files either as a link, or as an actual attachment. Microsoft One Drive attachments always appear as links in the message body.
Attachment schemas and size limits
Section titled “Attachment schemas and size limits”Choose how to attach files
Diagram as text
- Your app: Attach files to a message or draft: Your app adds files when it sends a message, creates a draft, or updates a draft. The same attachment options work in all three cases. Requests:
POST /,v3/ grants/ {grant_id}/ messages/ send POST /, andv3/ grants/ {grant_id}/ drafts PUT /.v3/ grants/ {grant_id}/ drafts/ {draft_id} - Also keep in mind: Inline images: with JSON, set
content_idon the attachment and usecid:<content_id>in an<img>tag. With multipart, the file's form field name is its content ID. - Also keep in mind: The recipient's mail server can still reject a message under these limits. Exchange Online accepts 35 MB per message by default.
- Also keep in mind: Agent Accounts use JSON or multipart. Upload sessions aren't available, and the account's outbound message size limit also applies.
- Also keep in mind: Inline images: with JSON, set
- Ends in one of:
- Recommended for most messages → Send files as separate parts: Multipart form data. The message and each file travel as separate parts of one request. Works for every provider, up to 25 MB. Request:
Content-Type: multipart/, with the message JSON as one form field and each file as its own part.form-data - Also supported, small files only → Embed small files in the message JSON: Each file is encoded as text (Base64) inside the message JSON. The whole request must stay under 3 MB. Request:
Content-Type: application/, with each file's Base64json contentin theattachmentsarray. - Only for Microsoft, files over 25 MB → Upload the file first, then send (beta): Upload session. Your app uploads the file in a separate step, then sends the message with a reference to it within 1 hour. For files from 25 MB to 150 MB. Request:
POST /, then send with itsv3/ grants/ {grant_id}/ attachment-uploads attachment_id. Other providers get a401(not authorized).
- Recommended for most messages → Send files as separate parts: Multipart form data. The message and each file travel as separate parts of one request. Works for every provider, up to 25 MB. Request:
Nylas supports two ways to add attachments to messages and drafts:
- Recommended: the
multipart/formschema. You break your message into parts, each with its own MIME format. It handles requests up to 25MB on every provider. - Also supported: the
application/jsonschema. You pass attachment content as Base64-encoded strings in theattachmentsobject on a Message or Draft. The whole request must stay under 3MB, so use it only for small files.
For one specific case, files over 25MB on a Microsoft grant, use upload sessions to send attachments up to 150MB.
JSON schema
Section titled “JSON schema”When you add attachments to messages and drafts using the application/json schema, you pass the contents of the attachments as Base64-encoded strings in the attachments object, as in the following example.
curl --request PUT \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/drafts/<DRAFT_ID>' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer <NYLAS_API_KEY>' \ --header 'Content-Type: application/json' \ --data '{ "subject": "Happy Birthday from Nylas!", "body": "Wishing you the happiest of birthdays. <img src=\"cid:123456hbd\"> \n Fondly, \n Nylas", "to": [{ "name": "Leyah Miller", "email": "[email protected]" }], "attachments": [ { "content_type": "image/png; name=\"nylas-hbd.png\"", "filename": "nylas-hbd.png", "content": "<Base64-encoded picture content>", "content_id": "123456hbd" }, { "content_type": "application/ics", "filename": "invite.ics", "content": "<Base64-encoded ICS file>", "content_id": "7890bdnyl" } ] }'Multipart schema
Section titled “Multipart schema”When you add attachments to messages or drafts using the multipart/form schema, you break the message into parts, each of which can have its own MIME format. The message schema at the start of a multipart request is the same as in a normal cURL request.
Nylas recommends using this schema if you’re adding a lot of attachments or if the files are large to avoid encountering provider limitations.
curl --request POST \ --url 'https://api.us.nylas.com/v3/grants/<GRANT_ID>/drafts' \ --header 'Authorization: Bearer <NYLAS_API_KEY>' \ --header 'content-type: multipart/form-data' \ --form 'message={ "subject": "Happy Birthday from Nylas!", "body": "Wishing you the happiest of birthdays. <img src=\"cid:123456aklkdfanl\"> \n Fondly, \n -Nylas", "to": [{ "name": "Leyah Miller", "email": "[email protected]" }] }' \ --form 'kldj234567hbd2u=@/file/path/to/attachment/invite.ics' \ --form '123456aklkdfanl=@/file/path/to/attachment/nylas-hbd.png'// Example Multipart Email:From: [email protected]Subject: Happy Birthday from Nylas!Content-Type: multipart/alternative; boundary="boundary-string"
--your-boundaryContent-Type: text/plain; charset="utf-8"Content-Transfer-Encoding: quoted-printableContent-Disposition: inline
Wishing you the happiest of birthdays. \n Fondly, \n -Nylas
//This section is the fallback in case the email client does not support HTML.--boundary-stringContent-Type: text/html; charset="utf-8"Content-Transfer-Encoding: quoted-printableContent-Disposition: inline
<h1>Happy Birthday!</h1><p>Wishing you the happiest of birthdays. <\br> Fondly, <\br> -Nylas</p><p>This section displays in most modern email clients.</p><img src="cid:the-cid-of-this-file-is-12345" alt="Happy Birthday from Nylas!"=>
--boundary-string--
--the-cid-of-this-file-is-12345Content-Type: image/pngContent-ID: <nylas-hbd.png>Content-Disposition: inline; filename="nylas-hbd.png"
[IMAGE content]
--the-cid-of-this-file-is-12345--
--this-is-an-invite-fileContent-Type: application/icsContent-ID: <invite.ics>Content-Disposition: attachment; filename="invite.ics"
[ICS file content]
--=_8ab337ec2e38e1a8b82a01a5712a8bdb--In most cases, you should use an SDK for your project language to prepare multipart form data, since most languages include a helper library that simplifies formatting.
Add attachments to messages or drafts
Section titled “Add attachments to messages or drafts”You can add attachments to a message when you send it, or when you create or update a draft, by adding the attachments as content to the attachments array.
If a draft already has attachments and you send an update request with an attachments array, Nylas replaces the existing attachments list on the draft with the exact list you provide.
The encoding format you use depends on the total size of the HTTP payload of your message, as noted above.
Nylas recommends using the multipart/form-data schema if you’re adding a lot of attachments, or if the attachments are large. Nylas can send up to 25MB of attachments using multipart before encountering provider limitations.
curl --request PUT \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/drafts/<DRAFT_ID>' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer <NYLAS_API_KEY>' \ --header 'Content-Type: application/json' \ --data '{ "attachments": [ { "content_type": "image/png; name=\"logo.png\"", "filename": "logo.png", "content": "<Base64-encoded picture content>" }, { "content_type": "application/ics", "filename": "invite.ics", "content": "<Base64-encoded ICS file>" } ] }'Get attachment information
Section titled “Get attachment information”Use the Return Attachment Metadata endpoint to get information about specific attachments by ID.
curl --compressed --request GET \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/attachments/<ATTACHMENT_ID>?message_id=<MESSAGE_ID>' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer <NYLAS_API_KEY>'{ "request_id": "5fa64c92-e840-4357-86b9-2aa123456789", "data": { "content_type": "image/png; name=\"pic.png\"", "filename": "pic.png", "grant_id": "<NYLAS_GRANT_ID>", "content_id": "185e56cb50e12e82", "size": 13068, "content_id": "<ce9b9547-9eeb-43b2-ac4e-58768bdf04e4>" }}To find the attachment ID, you can start with the ID of the message that the file is attached to, then make a Get Message request using field selection (?select=attachments) to return just the attachments object from the message. From there you can parse the response to find the ID of the attachment you want.
curl -L 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/messages/<MESSAGE_ID>?select=attachments' \ -H 'Authorization: Bearer <NYLAS_API_KEY>'{ "request_id": "041d52e4-fe02-49d9-aeb6-3f0987654321", "data": { "attachments": [ { "content_disposition": "attachment; filename=\"nylas-logo.png\"", "content_id": "ii_123456789", "content_type": "image/png; name=\"nylas-logo.png\"", "filename": "nylas-logo.png", "grant_id": "<NYLAS_GRANT_ID>", "id": "<ATTACHMENT_ID>", "is_inline": true, "size": 4452 }, { "content_disposition": "attachment; filename=\"invite.ics\"", "content_id": "ii_987654321", "content_type": "application/ics; name=\"invite.ics\"", "filename": "invite.ics", "grant_id": "<NYLAS_GRANT_ID>", "id": "<ATTACHMENT_ID>", "is_inline": false, "size": 2456 } ] }}Update or remove an attachment from a draft
Section titled “Update or remove an attachment from a draft”Nylas only updates the fields you include in your requests, so you can make an Update Draft request and only pass the attachments object to update just the attachments.
To update a file’s content or filename, update the attachments list with the corrected content. Nylas replaces the entire attachments array with your new values.
To remove a attachment from a draft, make an update draft request, include just the attachments object, and leave out the file you want to remove.
curl --request PUT \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/drafts/<DRAFT_ID>' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer <NYLAS_API_KEY>' \ --header 'Content-Type: application/json' \ --data '{ "attachments": [{ "content_type": "image/png; name=\"logo.png\"", "filename": "pic.png", "content": "<BASE_64_PICTURE_CONTENT>" }] }'Working with inline attachments
Section titled “Working with inline attachments”How you work with inline attachments depends on which schema you’re using to send your message.
If you’re using the application/json schema, set up <cid> references in the message body where the files should appear, then pass the CIDs as the content_id for the associated attachment. If Nylas can’t find the associated cid reference, the attachment is still added to the message, but does not display inline. The cid should only be alphanumeric characters.
curl --request PUT \ --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/drafts/<DRAFT_ID>' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer <NYLAS_API_KEY>' \ --header 'Content-Type: application/json' \ --data '{ "subject": "Happy Birthday from Nylas!", "body": "Wishing you the happiest of birthdays. <img src=\"cid:123456hbd\"> \n Fondly, \n Nylas", "to": [{ "name": "Leyah Miller", "email": "[email protected]" }], "attachments": [ { "content_type": "image/png; name=\"nylas-hbd.png\"", "filename": "nylas-hbd.png", "content": "<BASE_64_PICTURE_CONTENT>", "content_id": "<CID>" }, { "content_type": "application/ics", "filename": "invite.ics", "content": "<BASE_64_ICS_FILE>", "content_id": "<CID>" } ] }'If you include a content_id in an attachment, Nylas assumes it should be displayed inline.
If you’re using the multipart/form-data schema, the form field name of each file is its content ID. Reference it in an <img> tag in the message body, as in <img src="cid:123456aklkdfanl">, to display the file inline.