Skip to content

messages

Messages

Bases: ListableApiResource, FindableApiResource, UpdatableApiResource, DestroyableApiResource

Nylas Messages API

The messages API allows you to send, find, update, and delete messages. You can also use the messages API to schedule messages to be sent at a later time. The Smart Compose API, allowing you to generate email content using machine learning, is also available.

Source code in nylas/resources/messages.py
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
class Messages(
    ListableApiResource,
    FindableApiResource,
    UpdatableApiResource,
    DestroyableApiResource,
):
    """
    Nylas Messages API

    The messages API allows you to send, find, update, and delete messages.
    You can also use the messages API to schedule messages to be sent at a later time.
    The Smart Compose API, allowing you to generate email content using machine learning, is also available.
    """

    @property
    def smart_compose(self) -> SmartCompose:
        """
        Access the Smart Compose collection of endpoints.

        Returns:
            The Smart Compose collection of endpoints.
        """
        return SmartCompose(self._http_client)

    def list(
        self,
        identifier: str,
        query_params: Optional[ListMessagesQueryParams] = None,
        overrides: RequestOverrides = None,
    ) -> ListResponse[Message]:
        """
        Return all Messages.

        Args:
            identifier: The identifier of the grant to get messages for.
            query_params: The query parameters to filter messages by.
            overrides: The request overrides to apply to the request.

        Returns:
            A list of Messages.
        """
        return super().list(
            path=f"/v3/grants/{identifier}/messages",
            response_type=Message,
            query_params=query_params,
            overrides=overrides,
        )

    def find(
        self,
        identifier: str,
        message_id: str,
        query_params: Optional[FindMessageQueryParams] = None,
        overrides: RequestOverrides = None,
    ) -> Response[Message]:
        """
        Return a Message.

        Args:
            identifier: The identifier of the grant to get the message for.
            message_id: The identifier of the message to get.
            query_params: The query parameters to include in the request.
            overrides: The request overrides to apply to the request.

        Returns:
            The requested Message.
        """
        return super().find(
            path=f"/v3/grants/{identifier}/messages/{message_id}",
            response_type=Message,
            query_params=query_params,
            overrides=overrides,
        )

    def update(
        self,
        identifier: str,
        message_id: str,
        request_body: UpdateMessageRequest,
        overrides: RequestOverrides = None,
    ) -> Response[Message]:
        """
        Update a Message.

        Args:
            identifier: The identifier of the grant to update the message for.
            message_id: The identifier of the message to update.
            request_body: The request body to update the message with.
            overrides: The request overrides to apply to the request.

        Returns:
            The updated Message.
        """
        return super().update(
            path=f"/v3/grants/{identifier}/messages/{message_id}",
            response_type=Message,
            request_body=request_body,
            overrides=overrides,
        )

    def destroy(
        self, identifier: str, message_id: str, overrides: RequestOverrides = None
    ) -> DeleteResponse:
        """
        Delete a Message.

        Args:
            identifier: The identifier of the grant to delete the message for.
            message_id: The identifier of the message to delete.
            overrides: The request overrides to apply to the request.

        Returns:
            The deletion response.
        """
        return super().destroy(
            path=f"/v3/grants/{identifier}/messages/{message_id}", overrides=overrides
        )

    def send(
        self,
        identifier: str,
        request_body: SendMessageRequest,
        overrides: RequestOverrides = None,
    ) -> Response[Message]:
        """
        Send a Message.

        Args:
            identifier: The identifier of the grant to send the message for.
            request_body: The request body to send the message with.
            overrides: The request overrides to apply to the request.

        Returns:
            The sent message.
        """
        path = f"/v3/grants/{identifier}/messages/send"
        form_data = None
        json_body = None

        # Use form data only if the attachment size is greater than 3mb
        attachment_size = sum(
            attachment.get("size", 0)
            for attachment in request_body.get("attachments", [])
        )
        if attachment_size >= MAXIMUM_JSON_ATTACHMENT_SIZE:
            form_data = _build_form_request(request_body)
        else:
            # Encode the content of the attachments to base64
            for attachment in request_body.get("attachments", []):
                if issubclass(type(attachment["content"]), io.IOBase):
                    attachment["content"] = encode_stream_to_base64(
                        attachment["content"]
                    )

            json_body = request_body

        json_response = self._http_client._execute(
            method="POST",
            path=path,
            request_body=json_body,
            data=form_data,
            overrides=overrides,
        )

        return Response.from_dict(json_response, Message)

    def list_scheduled_messages(
        self, identifier: str, overrides: RequestOverrides = None
    ) -> Response[List[ScheduledMessage]]:
        """
        Retrieve your scheduled messages.

        Args:
            identifier: The identifier of the grant to delete the message for.
            overrides: The request overrides to apply to the request.

        Returns:
            Response: The list of scheduled messages.
        """
        json_response = self._http_client._execute(
            method="GET",
            path=f"/v3/grants/{identifier}/messages/schedules",
            overrides=overrides,
        )

        data = []
        request_id = json_response["request_id"]
        for item in json_response["data"]:
            data.append(ScheduledMessage.from_dict(item))

        return Response(data, request_id)

    def find_scheduled_message(
        self, identifier: str, schedule_id: str, overrides: RequestOverrides = None
    ) -> Response[ScheduledMessage]:
        """
        Retrieve your scheduled messages.

        Args:
            identifier: The identifier of the grant to delete the message for.
            schedule_id: The id of the scheduled message to retrieve.
            overrides: The request overrides to apply to the request.

        Returns:
            Response: The scheduled message.
        """
        json_response = self._http_client._execute(
            method="GET",
            path=f"/v3/grants/{identifier}/messages/schedules/{schedule_id}",
            overrides=overrides,
        )

        return Response.from_dict(json_response, ScheduledMessage)

    def stop_scheduled_message(
        self, identifier: str, schedule_id: str, overrides: RequestOverrides = None
    ) -> Response[StopScheduledMessageResponse]:
        """
        Stop a scheduled message.

        Args:
            identifier: The identifier of the grant to delete the message for.
            schedule_id: The id of the scheduled message to stop.
            overrides: The request overrides to apply to the request.

        Returns:
            Response: The confirmation of the stopped scheduled message.
        """
        json_response = self._http_client._execute(
            method="DELETE",
            path=f"/v3/grants/{identifier}/messages/schedules/{schedule_id}",
            overrides=overrides,
        )

        return Response.from_dict(json_response, StopScheduledMessageResponse)

    def clean_messages(
        self,
        identifier: str,
        request_body: CleanMessagesRequest,
        overrides: RequestOverrides = None,
    ) -> ListResponse[CleanMessagesResponse]:
        """
        Remove extra information from a list of messages.

        Args:
            identifier: The identifier of the grant to clean the message for.
            request_body: The values to clean the message with.
            overrides: The request overrides to apply to the request.

        Returns:
            The list of cleaned messages.
        """
        json_resposne = self._http_client._execute(
            method="PUT",
            path=f"/v3/grants/{identifier}/messages/clean",
            request_body=request_body,
            overrides=overrides,
        )

        return ListResponse.from_dict(json_resposne, CleanMessagesResponse)

smart_compose: SmartCompose property

Access the Smart Compose collection of endpoints.

Returns:

Type Description
SmartCompose

The Smart Compose collection of endpoints.

clean_messages(identifier, request_body, overrides=None)

Remove extra information from a list of messages.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to clean the message for.

required
request_body CleanMessagesRequest

The values to clean the message with.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Type Description
ListResponse[CleanMessagesResponse]

The list of cleaned messages.

Source code in nylas/resources/messages.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def clean_messages(
    self,
    identifier: str,
    request_body: CleanMessagesRequest,
    overrides: RequestOverrides = None,
) -> ListResponse[CleanMessagesResponse]:
    """
    Remove extra information from a list of messages.

    Args:
        identifier: The identifier of the grant to clean the message for.
        request_body: The values to clean the message with.
        overrides: The request overrides to apply to the request.

    Returns:
        The list of cleaned messages.
    """
    json_resposne = self._http_client._execute(
        method="PUT",
        path=f"/v3/grants/{identifier}/messages/clean",
        request_body=request_body,
        overrides=overrides,
    )

    return ListResponse.from_dict(json_resposne, CleanMessagesResponse)

destroy(identifier, message_id, overrides=None)

Delete a Message.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to delete the message for.

required
message_id str

The identifier of the message to delete.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Type Description
DeleteResponse

The deletion response.

Source code in nylas/resources/messages.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def destroy(
    self, identifier: str, message_id: str, overrides: RequestOverrides = None
) -> DeleteResponse:
    """
    Delete a Message.

    Args:
        identifier: The identifier of the grant to delete the message for.
        message_id: The identifier of the message to delete.
        overrides: The request overrides to apply to the request.

    Returns:
        The deletion response.
    """
    return super().destroy(
        path=f"/v3/grants/{identifier}/messages/{message_id}", overrides=overrides
    )

find(identifier, message_id, query_params=None, overrides=None)

Return a Message.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to get the message for.

required
message_id str

The identifier of the message to get.

required
query_params Optional[FindMessageQueryParams]

The query parameters to include in the request.

None
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Type Description
Response[Message]

The requested Message.

Source code in nylas/resources/messages.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def find(
    self,
    identifier: str,
    message_id: str,
    query_params: Optional[FindMessageQueryParams] = None,
    overrides: RequestOverrides = None,
) -> Response[Message]:
    """
    Return a Message.

    Args:
        identifier: The identifier of the grant to get the message for.
        message_id: The identifier of the message to get.
        query_params: The query parameters to include in the request.
        overrides: The request overrides to apply to the request.

    Returns:
        The requested Message.
    """
    return super().find(
        path=f"/v3/grants/{identifier}/messages/{message_id}",
        response_type=Message,
        query_params=query_params,
        overrides=overrides,
    )

find_scheduled_message(identifier, schedule_id, overrides=None)

Retrieve your scheduled messages.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to delete the message for.

required
schedule_id str

The id of the scheduled message to retrieve.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Name Type Description
Response Response[ScheduledMessage]

The scheduled message.

Source code in nylas/resources/messages.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def find_scheduled_message(
    self, identifier: str, schedule_id: str, overrides: RequestOverrides = None
) -> Response[ScheduledMessage]:
    """
    Retrieve your scheduled messages.

    Args:
        identifier: The identifier of the grant to delete the message for.
        schedule_id: The id of the scheduled message to retrieve.
        overrides: The request overrides to apply to the request.

    Returns:
        Response: The scheduled message.
    """
    json_response = self._http_client._execute(
        method="GET",
        path=f"/v3/grants/{identifier}/messages/schedules/{schedule_id}",
        overrides=overrides,
    )

    return Response.from_dict(json_response, ScheduledMessage)

list(identifier, query_params=None, overrides=None)

Return all Messages.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to get messages for.

required
query_params Optional[ListMessagesQueryParams]

The query parameters to filter messages by.

None
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Type Description
ListResponse[Message]

A list of Messages.

Source code in nylas/resources/messages.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def list(
    self,
    identifier: str,
    query_params: Optional[ListMessagesQueryParams] = None,
    overrides: RequestOverrides = None,
) -> ListResponse[Message]:
    """
    Return all Messages.

    Args:
        identifier: The identifier of the grant to get messages for.
        query_params: The query parameters to filter messages by.
        overrides: The request overrides to apply to the request.

    Returns:
        A list of Messages.
    """
    return super().list(
        path=f"/v3/grants/{identifier}/messages",
        response_type=Message,
        query_params=query_params,
        overrides=overrides,
    )

list_scheduled_messages(identifier, overrides=None)

Retrieve your scheduled messages.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to delete the message for.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Name Type Description
Response Response[List[ScheduledMessage]]

The list of scheduled messages.

Source code in nylas/resources/messages.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def list_scheduled_messages(
    self, identifier: str, overrides: RequestOverrides = None
) -> Response[List[ScheduledMessage]]:
    """
    Retrieve your scheduled messages.

    Args:
        identifier: The identifier of the grant to delete the message for.
        overrides: The request overrides to apply to the request.

    Returns:
        Response: The list of scheduled messages.
    """
    json_response = self._http_client._execute(
        method="GET",
        path=f"/v3/grants/{identifier}/messages/schedules",
        overrides=overrides,
    )

    data = []
    request_id = json_response["request_id"]
    for item in json_response["data"]:
        data.append(ScheduledMessage.from_dict(item))

    return Response(data, request_id)

send(identifier, request_body, overrides=None)

Send a Message.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to send the message for.

required
request_body SendMessageRequest

The request body to send the message with.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Type Description
Response[Message]

The sent message.

Source code in nylas/resources/messages.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def send(
    self,
    identifier: str,
    request_body: SendMessageRequest,
    overrides: RequestOverrides = None,
) -> Response[Message]:
    """
    Send a Message.

    Args:
        identifier: The identifier of the grant to send the message for.
        request_body: The request body to send the message with.
        overrides: The request overrides to apply to the request.

    Returns:
        The sent message.
    """
    path = f"/v3/grants/{identifier}/messages/send"
    form_data = None
    json_body = None

    # Use form data only if the attachment size is greater than 3mb
    attachment_size = sum(
        attachment.get("size", 0)
        for attachment in request_body.get("attachments", [])
    )
    if attachment_size >= MAXIMUM_JSON_ATTACHMENT_SIZE:
        form_data = _build_form_request(request_body)
    else:
        # Encode the content of the attachments to base64
        for attachment in request_body.get("attachments", []):
            if issubclass(type(attachment["content"]), io.IOBase):
                attachment["content"] = encode_stream_to_base64(
                    attachment["content"]
                )

        json_body = request_body

    json_response = self._http_client._execute(
        method="POST",
        path=path,
        request_body=json_body,
        data=form_data,
        overrides=overrides,
    )

    return Response.from_dict(json_response, Message)

stop_scheduled_message(identifier, schedule_id, overrides=None)

Stop a scheduled message.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to delete the message for.

required
schedule_id str

The id of the scheduled message to stop.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Name Type Description
Response Response[StopScheduledMessageResponse]

The confirmation of the stopped scheduled message.

Source code in nylas/resources/messages.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def stop_scheduled_message(
    self, identifier: str, schedule_id: str, overrides: RequestOverrides = None
) -> Response[StopScheduledMessageResponse]:
    """
    Stop a scheduled message.

    Args:
        identifier: The identifier of the grant to delete the message for.
        schedule_id: The id of the scheduled message to stop.
        overrides: The request overrides to apply to the request.

    Returns:
        Response: The confirmation of the stopped scheduled message.
    """
    json_response = self._http_client._execute(
        method="DELETE",
        path=f"/v3/grants/{identifier}/messages/schedules/{schedule_id}",
        overrides=overrides,
    )

    return Response.from_dict(json_response, StopScheduledMessageResponse)

update(identifier, message_id, request_body, overrides=None)

Update a Message.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to update the message for.

required
message_id str

The identifier of the message to update.

required
request_body UpdateMessageRequest

The request body to update the message with.

required
overrides RequestOverrides

The request overrides to apply to the request.

None

Returns:

Type Description
Response[Message]

The updated Message.

Source code in nylas/resources/messages.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def update(
    self,
    identifier: str,
    message_id: str,
    request_body: UpdateMessageRequest,
    overrides: RequestOverrides = None,
) -> Response[Message]:
    """
    Update a Message.

    Args:
        identifier: The identifier of the grant to update the message for.
        message_id: The identifier of the message to update.
        request_body: The request body to update the message with.
        overrides: The request overrides to apply to the request.

    Returns:
        The updated Message.
    """
    return super().update(
        path=f"/v3/grants/{identifier}/messages/{message_id}",
        response_type=Message,
        request_body=request_body,
        overrides=overrides,
    )