Incoming messages

Receive an iMessage or any other incoming message. Read what the person sent, download their files, and reply in the same conversation

When someone writes to one of your lines, a message event arrives on the stream, and a message.received event reaches your webhook endpoint. Both name the same message.

message.received data
{
  "messageId": "0192a7c4-6a02-7e11-8c4d-2b9f7e3a5d22",
  "conversationId": "a0000000-0000-4000-8000-000000000001",
  "lineId": "0192a7b0-3e4f-7051-8c6d-7e8f9a0b1c2d",
  "channel": "imessage",
  "from": "+14155550100",
  "text": "Is my table ready?",
  "contactId": "c0000000-0000-4000-8000-000000000003",
  "optedOut": false,
  "attachments": []
}
You wantRead
Somewhere to replyconversationId: send to it with message.send
A message to quote in a replymessageId: send it as target.replyToMessageId
Who wrotefrom, their address, and contactId, Mapier's id for them
Which line got itlineId, as GET /v1/whoami lists it
Their filesattachments
Whether it was a groupisGroup: true

Reply to a message

Send to the message's conversationId. Your reply goes out from the same line, on the same channel.

const BASE = "https://api.mapier.ai";

type Incoming = {
  conversationId: string;
  messageId: string;
  text: string | null;
  attachments: unknown[];
};

export async function reply(message: Incoming, text: string) {
  // No text and no attachment: nobody wrote anything, so send nothing.
  if (!message.text?.trim() && message.attachments.length === 0) return;
  const res = await fetch(`${BASE}/v1/commands/message.send`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.MAPIER_API_KEY}`,
      "Content-Type": "application/json",
      "Idempotency-Key": `reply-${message.messageId}`,
      "x-mapier-version": "2026-12-01",
    },
    body: JSON.stringify({
      target: { conversationId: message.conversationId },
      payload: { kind: "text", text },
    }),
  });
  if (res.status !== 202) throw new Error(`send refused: ${res.status}`);
  return res.json();
}

Using the incoming messageId in the Idempotency-Key means a retry of the same reply is never sent twice. Your own messages never come back as incoming messages, so a reply cannot set off another reply.

Not every message has text

text is null when the message is only a file. Check text and attachments together before you read either.

Skip a message with no text and no attachment

Some iMessage lines, on older software, report some changes, such as a new chat background, as a message with no text and no attachment. Nobody wrote anything: skip a received message that has no text and no attachment rather than reply to it.

An app card a person sends from an iMessage app has no text and no attachment either, but it carries kind: "app_card" and an appCard block. Read the card there; App cards shows how.

text arrives with the channel's own markup. textFormat names that markup, and textPlain holds the same words without it:

text hastextFormat
No markupplain, or absent

Download a file

Each entry in attachments has an id, and mimeType, filename and byteSize as the person's device declared them, or null. A voice note adds "kind": "voice_note", and a sticker adds "isSticker": true. Download a file with GET /v1/attachments/{id}:

curl https://api.mapier.ai/v1/attachments/0192e4f2-0000-4000-8000-000000000001 \
  -H "Authorization: Bearer $MAPIER_API_KEY" \
  -H "x-mapier-version: 2026-12-01" \
  -o receipt.jpg
  • A 404 for a file that arrived moments ago usually means it is still uploading. Try again shortly.
  • The download's content-type is read from the file itself. A HEIC photo stays HEIC, so convert it before you show it in a browser.
  • The link needs your API key, so do not hand it to a browser or another service.

When it was sent, and in what order

occurredAt is when the person sent the message, from the channel. Sort by it to show a conversation. Messages can still arrive out of order, for example after a line reconnects.

Other things people do

Each of these is its own event. Ask for it with ?events= on the stream, or select it on a webhook endpoint.

A person…EventGuide
Reacts to a messagereaction.added, reaction.removedReactions
Edits or unsends their messagemessage.edited, message.unsentEdit and unsend
Votes in a pollpoll.votedPolls
Shares their locationlocation.share_started, location.updatedLocation
Changes the chat backgroundconversation.background_changedChat backgrounds
Asks you to stopcontact.opt_out_changedOpt-out

Not every channel sends all of these:

ChannelWhat arrivesOpting a person out
iMessageEverything aboveA typed STOP, or the API
WhatsAppReactionsA typed STOP, or the API

To opt a person out through the API, send POST /v1/contacts/{contactId}/consent.

Nothing reports that a person is typing.

Read what came before

The stream starts from now. To read a conversation's earlier messages, both ways, use GET /v1/conversations/{id}/messages.

Next