routa.messages covers the message lifecycle. Each method maps to a REST API endpoint.
send
Sends text or an approved template. Returns the message in the accepted state. The SDK does not send media, location or reactions: send() accepts only text and template.
Send a template
headerParameter and buttonParameters exist from version 0.5.0.
index is the position of the button in template.components.buttons, starting at 0, and every button counts. text is the value that replaces {{1}} in the URL. The API appends it as is, and the SDK does not encode it: percent-encode it yourself, only once.
For a media header, upload the file with media.upload() and pass its id:
mediaId is a MediaId. The sample used to create the template, a TemplateMediaId from templates.uploadMedia(), does not compile here.
The SDK only checks the shape of the types. The API refuses the send with a RoutaInvalidRequestError:
retrieve
Returns a message by its ID.
MessageId is a string at runtime, but a plain string is not assignable to it. See Types and IDs.
list
Lists messages with automatic pagination. Filter by direction to see only inbound or only outbound messages.
markRead
Marks an inbound message as read and sends the read receipt to its sender.
422 message_direction_invalid.
The Message object
The SDK fields are the API fields in camelCase.
Messages sent from the WhatsApp Business app
On a channel in coexistence mode, the business can also send messages from the WhatsApp Business app. Routa records them asoutbound with origin: 'business_app'. They are not sent through Routa, use none of your allowance and do not appear in usage. Use origin to tell them apart from the messages your code sent. See Messages.
Failed messages
failureCode is the reason for the failure of a message with status: 'failed', and null otherwise. The set of codes is closed and belongs to Routa, but the API publishes it as a string, so the SDK does not narrow it to a union. Compare it with the codes you handle and treat any other value as “failed, reason unknown”. Among them, media_unavailable means a file of the message became unavailable before sending, and provider_payment_issue, that WhatsApp refused the send because of the account’s payment method. See Failure reasons.
Messages with removed content
Routa removes the content of a message after the retention period of your plan. The API returns that message withcontent: null and content_redacted_at. The rest of it stays. See Content removed by retention.
Since 0.2.0, content is MessageContent | null and contentRedactedAt is string | null. These messages are returned as usual. Guard your reads of message.content:
In version 0.1.0,
messages.retrieve() and messages.list() throw a TypeError when they meet a message with removed content, and a single one of those messages breaks the whole listing. Upgrade to the current version. See Upgrade to 0.3.0.header_parameter and button_parameters, in snake_case like the rest of content. The keys are absent when the template asks for nothing there.
MessageContent also covers types that only arrive in inbound messages or in the history (media, location, reaction) and unsupported, so that reading a message does not throw just because its content type cannot be sent yet. This is separate from the null described above, which means removed content, not an unknown type.