Webhooks
Event types, their data, delivery and retries, and a complete receiver that verifies a signed webhook.
POST /v1/webhooks with webhooks:manage registers a public https address and the event types it receives. The response includes the endpoint id (a number), url, eventTypes, status, and the signing secret. The secret is returned only once.
Private and local addresses are rejected, so a receiver on your own computer needs a public https tunnel in front of it.
An agent can have 10 webhooks that are not disabled, counting the ones added in the dashboard. One more answers 409 conflict, so remove one first. Setting a disabled endpoint back to active or paused counts as adding one, and answers the same 409 at the limit.
The other calls, which need webhooks:manage too:
GET /v1/webhookslists endpoints without the secret.PATCH /v1/webhooks/{id}changesstatusoreventTypes.DELETE /v1/webhooks/{id}removes the endpoint, revokes its signing secret, and returns 204 with no body. A removed endpoint cannot be resumed or changed.POST /v1/webhooks/{id}/rotate-secretreplaces the signing secret once MessageOS has turned rotation on, as described below.POST /v1/webhooks/{id}/testsends awebhook.testevent. See Testing your receiver.GET /v1/webhooks/{id}/deliverieslists the 25 newest deliveries, without the payload.
An owner or admin of your brand can do the same on the Developers page of the dashboard: add a webhook, send it a test event, and read its deliveries. The page shows a new webhook's signing secret once, right after it is added.
The event body
Every event is a JSON object:
| Field | Meaning |
|---|---|
id | The event id: evt_ and a UUID. The same value is in the x-messageos-event-id header |
type | The event type |
createdAt | When the event was queued |
clientId | The client the event belongs to |
data | The fields for that type |
Each endpoint gets its own copy of an event, with its own id. The fields can come in any order, and new fields can be added, so read them by name.
Event types
An endpoint can subscribe to any of these. Webhook events lists the fields of each, with an example.
| Type | When |
|---|---|
reply.received | The person sent a message: words, a tap on a suggestion, a file, or a location. text is what they typed or the label they tapped, and postbackData is the value you gave the suggestion |
message.delivered | The phone received a message |
message.read | The person opened a message |
message.failed | A message that was sent could not be delivered. reason is the cause |
contact.opted_out | The person opted out |
contact.opted_in | The person opted back in |
workflow.completed | A guided workflow finished its task. An agent whose own system answers has no workflows, so it never gets this one |
The three message events carry id, the id the send answered with, and the reference you sent with the message, so you can match a report to your own record.
What a person sends
Every reply.received has the same fields. kind says what the person did: chip for a tap, text for words, 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.
A tap on a suggested reply. text is the label, and postbackData is the value you gave that suggestion:
{
"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": "crm-881",
"phoneNumber": "+15555550123",
"conversationId": 7,
"text": "On my way",
"kind": "chip",
"postbackData": "eta_now",
"attachments": [],
"location": null,
"receivedAt": "2026-10-28T23:00:05.000Z"
}
}A location the person shared, for example after a tap on a shareLocation action. text is empty and location has the place:
{
"id": "evt_7c1d9e2a-4b3f-4a6c-8d5e-0f1a2b3c4d5e",
"type": "reply.received",
"createdAt": "2026-10-28T23:02:10.000Z",
"clientId": "example",
"data": {
"inboundMessageId": 982,
"contactId": 42,
"externalId": "crm-881",
"phoneNumber": "+15555550123",
"conversationId": 7,
"text": "",
"kind": "location",
"postbackData": null,
"attachments": [],
"location": { "latitude": 40.7128, "longitude": -74.006, "label": null },
"receivedAt": "2026-10-28T23:02:05.000Z"
}
}A photo. text is empty and attachments has one entry for each file. url is where the file can be fetched, mediaType says what it is, and kind is image for a picture and file for anything else:
{
"id": "evt_9a8b7c6d-5e4f-4a3b-9c2d-1e0f9a8b7c6d",
"type": "reply.received",
"createdAt": "2026-10-28T23:04:10.000Z",
"clientId": "example",
"data": {
"inboundMessageId": 983,
"contactId": 42,
"externalId": "crm-881",
"phoneNumber": "+15555550123",
"conversationId": 7,
"text": "",
"kind": "file",
"postbackData": null,
"attachments": [
{ "url": "https://files.example.com/a1b2c3/photo.jpg", "mediaType": "image/jpeg", "kind": "image", "fileName": "photo.jpg" }
],
"location": null,
"receivedAt": "2026-10-28T23:04:05.000Z"
}
}Words the person types arrive with kind text, the words in text, and postbackData null. To answer any of them, send inboundMessageId as inReplyTo: Replying to a message has the rules.
STOP, START, HELP, and the other keywords arrive as reply.received too. MessageOS answers them itself, so your system must not answer them. When your own system answers lists them.
Delivery and retries
MessageOS sends each event with POST and these headers:
Content-Type: application/jsonUser-Agent: MessageOS-Webhooks/1x-messageos-event-id: the event idx-messageos-timestamp: unix seconds when this attempt was signedx-messageos-signature:sha256=and the lowercase hex HMAC-SHA256 of<timestamp>.<raw body>, keyed with the endpoint secret
Answer with a 2xx status to accept the event, within 10 seconds. An attempt that takes longer counts as failed. Redirects are not followed.
| Your response | What happens |
|---|---|
| 2xx | The delivery is done |
| 410 | The delivery stops, and the endpoint is disabled |
| Any other status, or no response in 10 seconds | The delivery is retried |
By default an event is tried up to 8 times. The wait after the first failed attempt is 30 seconds, and it doubles after each one: 30 seconds, then 1, 2, 4, 8, 16, and 32 minutes. After the eighth failed attempt the delivery is marked dead. When the 8 newest deliveries to an endpoint are all dead, the endpoint is paused. Set its status back to active to resume. An attempt can start a little after its wait ends.
A delivery can arrive more than once. Use the event id in the body to ignore a repeat. The x-messageos-event-id header carries the same value, but only the body is signed, so trust the one in the body.
Each endpoint gets its own copy of an event, with its own id, so two endpoints that take the same event type each deliver it. When you answer a reply.received, build the Idempotency-Key of the answer from inboundMessageId, which is the same on every copy and every repeat: key an answer by the message, not by the event. When your own system answers shows it.
Verifying the signature
- Read the raw request body, before any JSON parsing.
- Compute
sha256=and the lowercase hex HMAC-SHA256 of<x-messageos-timestamp>.<raw body>, keyed with your secret. - Compare it to
x-messageos-signaturein constant time. - Reject a timestamp more than 300 seconds from your clock. Each attempt is signed again with a new timestamp, so a retry still passes. MessageOS uses the same 300-second window for events you sign and send to it.
This sample body is the exact bytes used in the signature below:
{"id":"evt_1","type":"reply.received","createdAt":"2026-10-23T23:00:00.000Z","clientId":"example","data":{"contactId":1,"text":"Hello"}}With secret test-secret and timestamp 1700000000, the signature of that body is:
sha256=8f33db04011ef79a464e40a4f1d7438c399844a8636acb15ebe824f4fa46e4fcCheck your own code against it before you point a real endpoint at your server. Use the raw request body, not a re-encoded object: a parser can change spacing or key order, and the signature then fails.
A complete receiver
Each of these is a whole program. It takes the secret from MESSAGEOS_WEBHOOK_SECRET, listens for POST /webhooks/messageos, and does the five things every receiver must:
- Reads the raw body.
- Checks the signature in constant time, and refuses a timestamp more than 300 seconds off with 401.
- Skips an event
idit has already handled, and still answers 200 for it. - Does its work, which here is one printed line for each
reply.received. - Answers 200 when the work is done, and 500 when it failed so the event comes again.
The samples keep the handled ids in memory or in a file, which is enough for one server. With more than one, keep them in your database.
import { createHmac, timingSafeEqual } from 'node:crypto';
import { createServer } from 'node:http';
const secret = process.env.MESSAGEOS_WEBHOOK_SECRET;
// Without the secret no signature can be checked, so do not start.
if (!secret) throw new Error('MESSAGEOS_WEBHOOK_SECRET is not set');
// Use your database in production, so a repeat is caught on every server.
const done = new Set();
function verified(rawBody, timestamp, signature) {
if (typeof timestamp !== 'string' || typeof signature !== 'string') return false;
if (!/^\d+$/.test(timestamp) || Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
const digest = createHmac('sha256', secret).update(`${timestamp}.`).update(rawBody).digest('hex');
const expected = Buffer.from(`sha256=${digest}`);
const received = Buffer.from(signature);
return received.length === expected.length && timingSafeEqual(received, expected);
}
async function handle(event) {
if (event.type === 'reply.received') {
console.log(event.data.phoneNumber, event.data.kind, event.data.text);
}
}
createServer(async (req, res) => {
if (req.method !== 'POST' || req.url !== '/webhooks/messageos') {
res.writeHead(404).end();
return;
}
// The bytes as they were sent. Check the signature before any JSON parsing.
const chunks = [];
for await (const chunk of req) chunks.push(chunk);
const rawBody = Buffer.concat(chunks);
if (!verified(rawBody, req.headers['x-messageos-timestamp'], req.headers['x-messageos-signature'])) {
res.writeHead(401).end();
return;
}
const event = JSON.parse(rawBody.toString('utf8'));
try {
if (!done.has(event.id)) {
await handle(event);
done.add(event.id);
}
// 2xx tells MessageOS the event arrived. Answer within 10 seconds.
res.writeHead(200).end();
} catch (error) {
// Any other status is retried, so an event that failed here comes again.
console.error(error);
res.writeHead(500).end();
}
}).listen(Number(process.env.PORT ?? 3000));Where each one checks the signature in constant time: timingSafeEqual in Node.js, hmac.compare_digest in Python, hash_equals in PHP, OpenSSL.secure_compare in Ruby, hmac.Equal in Go, MessageDigest.isEqual in Java, and CryptographicOperations.FixedTimeEquals in C#. A plain == on the two strings can leak how much of a forged signature was right.
If your web framework parses JSON before your code runs, ask it for the raw body on this route. The signature covers the bytes as sent.
To answer the person from inside the handler, send POST /v1/messages with inReplyTo. When your own system answers shows the whole loop, and the example app is a receiver that answers with every kind of message.
Testing your receiver
POST /v1/webhooks/{id}/test sends webhook.test, with empty data, to that endpoint only. It is not one of the types an endpoint subscribes to, and it sends no message to anyone.
curl -X POST https://api.messageos.ai/v1/webhooks/3/test \
-H 'Authorization: Bearer mosc_...'The answer is 202 with the event id as soon as the event is queued. Its delivery starts at once, so it usually reaches your server within seconds. After 30 tests in an hour for one agent, or while webhook delivery is not turned on for your agent, the event stays queued and goes out on the next delivery run. The answer does not wait for your server: GET /v1/webhooks/3/deliveries lists the event with its status and the status code your server answered. A test your server did not accept is retried like any other event.
A paused or disabled endpoint answers 409 conflict, so set its status to active first.
Rotating the secret
POST /v1/webhooks/{id}/rotate-secret makes a new signing secret and returns it once, with the endpoint.
MessageOS turns rotation on. Until it is on, the route answers 404 not_found whatever the id. So if it answers 404 for an endpoint you have not removed, rotation is not turned on for you yet: ask MessageOS. Until then, get a new secret by removing the endpoint with DELETE /v1/webhooks/{id} and adding it again with POST /v1/webhooks. The new endpoint has a new id and its own delivery history.
When rotation is on, the old secret stops working at once, so put the new one in your receiver right away. An attempt already under way may still carry the old signature. If your receiver refuses it, the event is retried with the new secret. A paused endpoint can be rotated. A removed endpoint cannot.
When two rotations of one endpoint run at the same time, one secret is kept. The other call returns 409 conflict with no secret. Use the secret from the call that succeeded, or rotate again.
