Code
js
// Delivery tracking: a 200 from the send step means "queued", not "delivered".
// Status names differ per provider. Adjust these two sets to match yours.
const FAILED = new Set(['failed', 'undelivered', 'bounced', 'rejected']);
const DONE = new Set(['delivered', 'read']);
const isFinal = (status) => FAILED.has(status) || DONE.has(status);
// Workflow 1: call right after the send step.
// store is a plain object keyed by message ID. Persist it in a database table.
function recordSent(store, messageId, recipient, now = Date.now()) {
if (!messageId) throw new Error('Send step returned no message ID');
store[messageId] = { recipient, status: 'queued', sentAt: now, updatedAt: now };
return store[messageId];
}
// Workflow 2: call for every event on the status webhook.
// Returns an alert object, or null when nothing needs attention.
function applyStatus(store, event, now = Date.now()) {
const messageId = event.messageId;
const status = String(event.status || '').toLowerCase();
const row = store[messageId];
if (!row) {
return { level: 'warn', messageId, status, reason: 'unknown message ID' };
}
if (isFinal(row.status)) return null; // late or duplicate event, already handled
row.status = status;
row.updatedAt = now;
if (FAILED.has(status)) {
return {
level: 'error',
messageId,
status,
recipient: row.recipient,
reason: event.error || 'delivery failed',
};
}
return null;
}
// Workflow 3 (schedule): messages that never reached a final status.
function findStale(store, now = Date.now(), maxAgeMs = 15 * 60 * 1000) {
return Object.entries(store)
.filter(([, row]) => !isFinal(row.status) && now - row.sentAt > maxAgeMs)
.map(([messageId, row]) => ({
level: 'warn',
messageId,
status: row.status,
recipient: row.recipient,
reason: 'no final status',
}));
}
// In an n8n Code node, load `store` from your table, then end with e.g.:
// return $input.all()
// .map((item) => applyStatus(store, item.json))
// .filter(Boolean)
// .map((json) => ({ json }));Why it happens
Sending a message is a two-stage job. First the provider accepts your request and replies 200 with an ID. Later it tries to hand the message to the carrier, mailbox or chat app, and that attempt can fail. The second result never returns to your send step, so the workflow run stays green. The provider reports it only through a separate status callback.
Steps
- After the send step, call
recordSentwith the message ID from the response. Save the row with statusqueued. - Create a second workflow with a Webhook trigger. Register its URL as the status callback in your provider settings.
- In that workflow, map the provider payload to
{ messageId, status, error }and pass each event toapplyStatus. - If
applyStatusreturns an object, send it to your alert channel. If it returnsnull, do nothing. - Add a scheduled workflow that runs
findStale. It catches messages whose callback never arrived. - Save the updated
storeback to the table after each run.
Pitfalls
- Workflow static data is not shared between workflows. Keep the store in a database table that all three workflows can read.
- Status names differ between providers, and events can arrive late, twice or out of order. The code ignores events once a message has a final status.
- The status webhook is a public URL. Verify the provider's signature or a secret header before trusting an event, and keep that secret in n8n credentials.
- Remove old final rows on a schedule, or the table grows forever.
Seen on: n8n Community ·
Related service: n8n, Make and Zapier workflow automation