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
 23
 24
 25
 26
 27
 28
 29
 30
 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
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
    ) -> 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.

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

    def find(
        self,
        identifier: str,
        message_id: str,
        query_params: Optional[FindMessageQueryParams] = 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.

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

    def update(
        self,
        identifier: str,
        message_id: str,
        request_body: UpdateMessageRequest,
    ) -> 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.

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

    def destroy(self, identifier: str, message_id: str) -> 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.

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

    def send(
        self, identifier: str, request_body: SendMessageRequest
    ) -> 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.

        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:
            json_body = request_body

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

        return Response.from_dict(json_response, Message)

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

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

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

        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
    ) -> 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.

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

        return Response.from_dict(json_response, ScheduledMessage)

    def stop_scheduled_message(
        self, identifier: str, schedule_id: str
    ) -> 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.

        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}",
        )

        return Response.from_dict(json_response, StopScheduledMessageResponse)

smart_compose: SmartCompose property

Access the Smart Compose collection of endpoints.

Returns:

Type Description
SmartCompose

The Smart Compose collection of endpoints.

destroy(identifier, message_id)

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

Returns:

Type Description
DeleteResponse

The deletion response.

Source code in nylas/resources/messages.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def destroy(self, identifier: str, message_id: str) -> 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.

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

find(identifier, message_id, query_params=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

Returns:

Type Description
Response[Message]

The requested Message.

Source code in nylas/resources/messages.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def find(
    self,
    identifier: str,
    message_id: str,
    query_params: Optional[FindMessageQueryParams] = 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.

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

find_scheduled_message(identifier, schedule_id)

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

Returns:

Name Type Description
Response Response[ScheduledMessage]

The scheduled message.

Source code in nylas/resources/messages.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def find_scheduled_message(
    self, identifier: str, schedule_id: str
) -> 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.

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

    return Response.from_dict(json_response, ScheduledMessage)

list(identifier, query_params=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

Returns:

Type Description
ListResponse[Message]

A list of Messages.

Source code in nylas/resources/messages.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def list(
    self, identifier: str, query_params: Optional[ListMessagesQueryParams] = 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.

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

list_scheduled_messages(identifier)

Retrieve your scheduled messages.

Parameters:

Name Type Description Default
identifier str

The identifier of the grant to delete the message for.

required

Returns:

Name Type Description
Response Response[List[ScheduledMessage]]

The list of scheduled messages.

Source code in nylas/resources/messages.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def list_scheduled_messages(
    self, identifier: str
) -> Response[List[ScheduledMessage]]:
    """
    Retrieve your scheduled messages.

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

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

    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)

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

Returns:

Type Description
Response[Message]

The sent message.

Source code in nylas/resources/messages.py
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
def send(
    self, identifier: str, request_body: SendMessageRequest
) -> 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.

    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:
        json_body = request_body

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

    return Response.from_dict(json_response, Message)

stop_scheduled_message(identifier, schedule_id)

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

Returns:

Name Type Description
Response Response[StopScheduledMessageResponse]

The confirmation of the stopped scheduled message.

Source code in nylas/resources/messages.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def stop_scheduled_message(
    self, identifier: str, schedule_id: str
) -> 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.

    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}",
    )

    return Response.from_dict(json_response, StopScheduledMessageResponse)

update(identifier, message_id, request_body)

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

Returns:

Type Description
Response[Message]

The updated Message.

Source code in nylas/resources/messages.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def update(
    self,
    identifier: str,
    message_id: str,
    request_body: UpdateMessageRequest,
) -> 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.

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