MessageOS
Guides

When your own system answers

The whole loop for a business whose own software answers its customers, from what MessageOS sets up to hearing a message, answering it, and what stays automatic.

Some businesses do not use the MessageOS assistant. Their own software reads each message a customer sends and writes the answer. MessageOS calls that setup API only: MessageOS carries the messages, keeps the messaging rules, and tells your system what happens, and your system does the talking.

This page is the whole loop in one place. The quickstart sends a first message, and the example app is working code for everything here. The Kitchen Sink showcase shows that app on a phone.

Who sets up what

MessageOS sets up:

  • Your agent: the name, logo, and details customers see on the conversation.
  • API only, so the assistant never answers your customers and they never get two replies. The Agent page of the dashboard shows it, with no switch. To change it, ask MessageOS.
  • Tester phones, before your agent is launched. Until launch it reaches only the phones MessageOS has added as testers, so give MessageOS the numbers you will test with. Testing says what happens next.
  • What your integration uses: replies, rich messages, and the extras in the checklist below.
  • The words of your STOP, START, and HELP replies, from wording you provide.

You set up:

  • A key. An owner or admin of your brand makes one under Developers in the dashboard. It needs messages:write, and contacts:write to send a first message to a new number with its consent. Add webhooks:manage to manage webhooks from code.
  • A webhook that takes reply.received, on the same Developers page or with POST /v1/webhooks. Take message.delivered, message.read, message.failed, contact.opted_out, and contact.opted_in too, so your system knows what became of each message. Its signing secret is shown once, when the webhook is added: on the page, or as secret in the answer.
  • A receiver: a public https address that verifies each event and answers it. Webhooks has a complete one in seven languages.

The loop

  1. A person writes to your agent, taps a button, or sends a file or a location.
  2. MessageOS stores the message, marks it read on the person's phone, and posts reply.received to your webhook.
  3. Your receiver checks the signature with the webhook's secret, and reads the event.
  4. Your system can show the typing indicator with POST /v1/typing while it works. This step is optional.
  5. Your system answers with POST /v1/messages, naming the person's message in inReplyTo.
  6. MessageOS posts message.delivered, and message.read when the person opens it, or message.failed if it could not be delivered.

Hearing a message

reply.received carries everything your system needs to answer:

{
  "id": "evt_2b7f4c1e-8a9d-4e3f-b6a1-5c0d9e8f7a6b",
  "type": "reply.received",
  "createdAt": "2026-10-28T23:00:10.000Z",
  "clientId": "example",
  "data": {
    "inboundMessageId": 981,
    "contactId": 42,
    "externalId": null,
    "phoneNumber": "+15555550123",
    "conversationId": 7,
    "text": "Is my order ready?",
    "kind": "text",
    "postbackData": null,
    "attachments": [],
    "location": null,
    "receivedAt": "2026-10-28T23:00:05.000Z"
  }
}
  • inboundMessageId is the id you send back as inReplyTo.
  • contactId is who to answer. Use it as to.contactId.
  • kind is text for typed words, chip for a tap on one of your buttons (with your value in postbackData), file for a file, and location for a shared place. A file or a location sent with words has kind text, so read attachments and location whenever they are not empty. Webhooks shows a body for each.

Showing that an answer is on its way

{ "to": { "contactId": 42 } }

Send that body to POST /v1/typing and the person sees the typing indicator. It is a signal, not a message: it is not counted in usage and needs no Idempotency-Key. The messaging network does not say how long a phone shows it, so send it again if your answer takes a while. Showing that an answer is on its way has the rules.

Answering

{ "to": { "contactId": 42 }, "text": "Yes. Order 8841 is ready for pickup.", "inReplyTo": 981 }

Send it to POST /v1/messages with an Idempotency-Key. Build the key from inboundMessageId, the id of the person's message, so an answer to one message always sends with the same key. Then an event that is delivered to you twice, or a send you retry after a timeout, reaches the person once.

Do not build the key from the event id. Each webhook endpoint gets its own copy of an event, with its own id, so two endpoints that both take reply.received, such as an old and a new address while you move, each deliver the message to you. Key an answer by the message, not by the event, and the person is still answered once.

The same key must carry the same body. A key sent again with different words returns 422 idempotency_key_reuse and sends nothing. If your answer can come out differently each time, as one written by a model can, that 422 means the first answer already went out, and the handler below treats it so.

An answer can be any kind of message: text with buttons, a card, a carousel, or a file. Message types has a body for each.

This handler does steps 4 and 5. It takes the place of the handle function in the Node.js receiver on the Webhooks page. Set MESSAGEOS_API_KEY to your client key before you start it, beside the MESSAGEOS_WEBHOOK_SECRET the receiver already reads:

const apiUrl = process.env.MESSAGEOS_API_URL ?? 'https://api.messageos.ai/v1';

// MessageOS answers these itself, so your system must not. See "Keywords are answered for you".
const keywords = new Set([
  'stop', 'stopall', 'stop all', 'unsubscribe', 'cancel', 'end', 'quit', 'revoke', 'optout', 'opt out',
  'start', 'unstop', 'join', 'subscribe', 'help', 'info',
]);
const isKeyword = (text) => keywords.has(text.trim().toLowerCase().replace(/[.!]+$/, '').replace(/\s+/g, ' '));

function post(path, body, idempotencyKey) {
  return fetch(`${apiUrl}${path}`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.MESSAGEOS_API_KEY}`,
      'Content-Type': 'application/json',
      ...(idempotencyKey ? { 'Idempotency-Key': idempotencyKey } : {}),
    },
    body: JSON.stringify(body),
  });
}

// Your own code goes here: look up the order, ask your own model, and so on.
async function writeAnswer(text) {
  return `You said: ${text}`;
}

async function handle(event) {
  if (event.type !== 'reply.received') return;
  const { inboundMessageId, contactId, text } = event.data;
  if (isKeyword(text)) return;

  // Optional. A typing indicator that fails must not stop the answer, so its status is not checked.
  await post('/typing', { to: { contactId } }).catch(() => null);

  const answer = await writeAnswer(text);
  const response = await post('/messages', { to: { contactId }, text: answer, inReplyTo: inboundMessageId }, `reply-to-${inboundMessageId}`);
  await settle(response);
}

// Reads the answer to POST /messages. It returns when there is nothing more to do, and throws when
// the same request is worth another try: the receiver then answers 500, MessageOS delivers the
// event again, and the same Idempotency-Key is used.
async function settle(response) {
  if (response.ok) return;
  const { code, reason } = (await response.json().catch(() => ({}))).error ?? {};
  // This key was already used with other words, so an answer to this message went out before.
  if (response.status === 422 && code === 'idempotency_key_reuse') return;
  const tryAgain =
    // MessageOS could not complete the request just now, or a request limit was passed.
    response.status >= 500 || response.status === 429 ||
    // The key is wrong or lacks a permission. Fix the key, and the event that comes again is answered.
    response.status === 401 || response.status === 403 ||
    // The first request with this key is still running, or the messaging network is busy.
    (response.status === 409 && (code === 'conflict' || reason === 'provider_busy'));
  if (tryAgain) throw new Error(`POST /messages answered ${response.status} ${reason ?? code ?? ''}`);
  // Not sent, and the same request would get the same answer: the person opted out, the phone
  // cannot be reached, or the body is not valid. Keep the code and the reason for your own record.
  console.error('not sent', response.status, code, reason ?? '');
}

settle reads the answer to the send and decides what becomes of the event:

The send answeredWhat the handler does
201 or 202Nothing more. The event is done
422 idempotency_key_reuseNothing more. An answer to this message already went out
409 conflict, 409 blocked with the reason provider_busy, 429, or 500 and aboveFails the event, so it is delivered again and the send is tried with the same key
401 or 403The same, so the events are answered once the key is fixed
Any other 409 blocked, and any other 4xxRecords the code and the reason, and accepts the event. The same request would get the same answer

A handler that fails every event for a body the API will never take would use up all 8 tries of each one, and an endpoint whose 8 newest deliveries are dead is paused.

The reply rules

A send that names a message in inReplyTo is a reply. A reply needs no consent record and no time zone, and quiet hours never hold it. Three rules decide whether a send counts as one:

  • One hour. The person's message must have arrived within the reply window, one hour. GET /v1/whoami returns it as replyWindowSeconds.
  • Three replies. Up to three sends can answer one message. Each new message from the person allows three more.
  • Not promotional. A send with "class": "promotional" is never a reply.

A send outside those rules is not refused for that. It is treated as a message you start, so the rules for a message you start apply: where GET /v1/whoami shows quietHoursEnforced true, quiet hours can hold it.

An inReplyTo that is not a message this contact wrote returns 422, and details[0].path is inReplyTo. Replying to a message has every case.

Quiet hours

An API-only agent keeps its own quiet hours. MessageOS does not hold a message it sends for the time of day, and a contact needs no time zone. That is true for a message your system sends through the API and for one your team sends from the dashboard. Your own system and your team decide when to send, so the timing is yours to get right.

  • GET /v1/whoami shows this as quietHoursEnforced false, and the Agent page of the dashboard shows Quiet hours as None. If the Agent page shows hours there, MessageOS still holds a message sent from the dashboard during them.
  • To have MessageOS hold one message, set onQuiet on that request. "reject" answers 409 quiet_hours during quiet hours, and "defer" answers 202 with deferredUntil and sends nothing. A draft written with onQuiet keeps that hold when it is approved, through the API or in the dashboard. A draft written with "defer" and approved during quiet hours is not sent later on its own: it waits for approval again, so approve it after quiet hours end, before it expires.
  • quietHours on GET /v1/whoami are the hours such a request is checked against. They are counted in the contact's time zone, or in timezone when the contact has none.
  • Opt-out, consent, and the message classes still decide every send.

Starting a conversation

A message your system starts is not a reply, so it has no inReplyTo. It needs a consent record for the person, or an earlier message from them. For a new phone number, send the consent you hold in the same request:

{
  "to": { "phoneNumber": "+15555550123" },
  "text": "Thanks for signing up. Reply STOP to opt out.",
  "consent": { "source": "checkout_form", "capturedAt": "2026-10-28T23:00:00.000Z" }
}

Keywords are answered for you

MessageOS answers these words itself, for every agent, API only included:

The person sendsMessageOS does
STOP, STOPALL, STOP ALL, UNSUBSCRIBE, CANCEL, END, QUIT, REVOKE, OPTOUT, or OPT OUTOpts them out, sends your opt-out confirmation, and posts contact.opted_out
START, UNSTOP, JOIN, or SUBSCRIBEOpts them back in, sends your opt-in confirmation, and posts contact.opted_in
YES, from a person who has opted outThe same as START
HELP or INFOSends your help message

A keyword counts only when it is the whole message. Letter case does not matter, and a period or exclamation point at the end is ignored. A tap on a button labeled with one of these words counts as that word.

Your webhook still receives each of them as reply.received. Do not answer them, or the person who asked to stop gets a second message. The handler above skips them. YES is not on its list, because your system cannot tell from the event whether the person had opted out: from anyone else YES is an ordinary answer.

After an opt-out, every send to that person returns 409 blocked with the reason opted_out, replies included, until they opt back in. On an API-only agent a person is opted back in only by one of the words above, or by subscribing again in their messaging app.

What MessageOS does without being asked

  • Read receipts. Each message from a person who has not opted out is marked read when it arrives. There is no call for it.
  • Keywords, as above.
  • Opt-out. A person who opted out is never sent anything, whatever your system asks for.
  • Delivery reports. message.delivered, message.read, and message.failed for every message you send, with the reference you sent. GET /v1/messages/{id} reads the same status whenever you ask.
  • The record. Every message, in and out, shows under Conversations in the dashboard and in GET /v1/conversations/{id}/messages.

And what it does not do for an API-only agent: the assistant does not answer, guided forms do not run, and scheduled messages and event playbook messages are not sent.

The checklist

Call GET /v1/whoami with your key. It says what is on for your agent, so your code can read it instead of assuming:

{
  "clientId": "example",
  "name": "Example Dental",
  "keyPrefix": "mosc_AbC123",
  "scopes": ["contacts:read", "contacts:write", "conversations:read", "messages:write", "webhooks:manage"],
  "environment": "production",
  "timezone": "America/New_York",
  "quietHours": { "start": "21:00", "end": "08:00" },
  "quietHoursEnforced": false,
  "richMessages": true,
  "rcsExtras": true,
  "allowedClasses": ["service", "transactional"],
  "workflowsEnabled": false,
  "replyWindowSeconds": 3600,
  "limits": {
    "requestsPerMinute": null,
    "sendsPerAgent": null,
    "sendsPerAgentWindowSeconds": 3600,
    "sendsPerContact": null,
    "sendsPerContactWindowSeconds": 3600
  }
}
FieldWhat you want to seeWhat depends on it
scopesmessages:write, and contacts:write if you send first messages with consentA request without the scope its route needs returns 403 forbidden
replyWindowSecondsA numberinReplyTo. While it is null, a body with inReplyTo returns 422
richMessagestrueButtons, cards, carousels, and files. While it is false, a body with one of them returns 422, and text still sends
rcsExtrastruePOST /v1/typing, GET /v1/capabilities, and DELETE /v1/messages/{id}, which answer 404 while it is false, and expiresAt, ttlSeconds, and trafficType, which return 422
quietHoursEnforcedfalseWhether a message you start is held for the time of day. For an API-only agent it is false: the time of day is yours to keep. A reply is never held either way
allowedClassesThe classes you sendA class that is not listed returns 422
environmentThe environment you mean to callWhich environment your key and address reached
limitsnull in each limitMessageOS does not cap requests or sends. A number is a limit MessageOS has set

If a field is not what you need, ask MessageOS to turn it on. Nothing here can be changed with the API.

Three things are not in that answer:

  • Webhook delivery. Send a test event with POST /v1/webhooks/{id}/test and read GET /v1/webhooks/{id}/deliveries. The Developers page of the dashboard also says whether webhook delivery is on.
  • Launch. Until your agent is launched, a send to a phone that is not a tester answers 409 blocked with the reason agent_not_available, or is reported afterwards as message.failed. GET /v1/capabilities answers rcs false for such a phone.
  • Secret rotation. POST /v1/webhooks/{id}/rotate-secret answers 404 until MessageOS turns it on.

Next

  • The example app: this whole page as running code, with every kind of message.
  • Kitchen Sink showcase: the example app on a phone, with screenshots of each message type.
  • Webhooks: event bodies, retries, and a receiver in your language.
  • Errors: every reason a send can be blocked.

On this page