Skip to content
SMS SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

Handle STOP and HELP replies in your app

How to handle STOP and HELP SMS replies: record opt-outs in your own suppression list, block sends to those numbers on every provider, answer HELP, and treat provider opt-out refusals as STOPs.

When someone replies STOP, you must stop texting them on every provider you use. SMS SDK turns STOP, START, and HELP replies into events, and lets you block sends with beforeSend.

To handle STOP and HELP replies in your app: point your provider’s inbound webhook at a route that calls parseSmsWebhook(). On recipient.opted_out, add the sender’s number to a suppression list in your own database. On recipient.opted_in (START, which only Twilio reports), remove it. Check that list in beforeSend, so no provider texts a number that opted out on another one. On recipient.help, reply with who you are and how to stop, unless your provider already answers HELP for your sender type. Finally, add a number to the list when a provider refuses a send because the recipient opted out (Twilio 21610, Telnyx 40300, Vonage 1240), because a STOP webhook can be missed. Each step is below.

Opt-out and help events

An inbound message becomes one of these instead of message.received:

Event Comes from
recipient.opted_out Twilio OptOutType=STOP, or a whole-message keyword: STOP, STOPALL, UNSUBSCRIBE, CANCEL, END, QUIT
recipient.opted_in Twilio OptOutType=START only
recipient.help Twilio OptOutType=HELP, or a whole-message keyword: HELP, INFO

Each event has from (the person who texted), to (your number), the original body, keyword, providerId, and source, which says who detected it:

  • "provider": the provider flagged it. Only Twilio does this, in OptOutType, and only with Advanced Opt-Out enabled on the Messaging Service.
  • "keyword": SMS SDK matched the whole message, ignoring case, surrounding spaces, and a trailing ., !, or ?. “Stop!” matches. “Please stop texting me” does not.

Keyword matching is a convenience. Carriers and providers may treat other words or languages as opt-outs. Set detectKeywords: false to receive every inbound text as message.received, then apply your own rules with detectKeyword() or your own matcher.

Record opt-outs

import { parseSmsWebhook, WebhookSignatureError } from "@opencoredev/sms-sdk/webhooks";
import { db } from "./db";

export async function POST(request: Request): Promise<Response> {
  try {
    const event = await parseSmsWebhook({
      provider: "twilio",
      request,
      publicUrl: "https://example.com/webhooks/sms/twilio",
      credentials: { authToken: process.env.TWILIO_AUTH_TOKEN ?? "" },
    });

    if (event.type === "recipient.opted_out") {
      await db.suppressions.add(event.from);
    } else if (event.type === "recipient.opted_in") {
      await db.suppressions.remove(event.from);
    }
    return new Response("<Response></Response>", { headers: { "content-type": "text/xml" } });
  } catch (error) {
    if (error instanceof WebhookSignatureError) {
      return new Response("Invalid signature", { status: 401 });
    }
    throw error;
  }
}

The suppression list lives in your database, so it covers every provider you send through.

Block sends to suppressed recipients

import { createSmsClient } from "@opencoredev/sms-sdk";
import { telnyx } from "@opencoredev/sms-sdk/telnyx";
import { db } from "./db";

export const sms = createSmsClient({
  adapters: [telnyx({ apiKey: process.env.TELNYX_API_KEY ?? "", from: "+15550100002" })],
  beforeSend: async ({ message }) =>
    (await db.suppressions.has(message.to)) === false ? { kind: "allow" } : { kind: "reject", reason: "Recipient opted out." },
});

Sending to a suppressed recipient throws PolicyRejectedError before any request, and fallback never runs.

With the built-in helpers

@opencoredev/sms-sdk exports a SuppressionList interface (has, add, remove, each sync or async) and three helpers around it:

  • suppressionPolicy(list) returns a beforeSend that rejects suppressed recipients with reason "recipient suppressed".
  • applySuppressionEvent(list, event) adds the sender of a recipient.opted_out event, removes the sender of a recipient.opted_in event, ignores everything else, and returns "added", "removed", or "ignored".
  • memorySuppressionList() keeps the list in process memory. It is lost on restart and not shared between processes, so use it in tests and single-process tools, and back SuppressionList with your database in production.

Any object with has, add, and remove works as the list, such as a table in your database. has must return true or false; any other value, such as undefined from a failed read, makes suppressionPolicy() throw ConfigurationError, so the send fails and nothing is sent:

import { applySuppressionEvent, createSmsClient, suppressionPolicy } from "@opencoredev/sms-sdk";
import { telnyx } from "@opencoredev/sms-sdk/telnyx";
import { parseSmsWebhooks } from "@opencoredev/sms-sdk/webhooks";
import { db } from "./db";

export const sms = createSmsClient({
  adapters: [telnyx({ apiKey: process.env.TELNYX_API_KEY ?? "", from: "+15550100002" })],
  beforeSend: suppressionPolicy(db.suppressions),
});

export async function POST(request: Request): Promise<Response> {
  const events = await parseSmsWebhooks({
    provider: "telnyx",
    request,
    credentials: { publicKey: process.env.TELNYX_PUBLIC_KEY ?? "" },
  });
  for (const event of events) {
    await applySuppressionEvent(db.suppressions, event);
  }
  return new Response(null, { status: 200 });
}

Numbers are compared exactly, so store them in E.164, the form webhook events and send() use.

A provider may also refuse a message because the recipient opted out with it directly: Twilio 21610, Telnyx 40300, or Vonage 1240. That rejection has category: "compliance", and SMS SDK never retries it or tries another provider. See Fallback and retries. Add the number to your list when you see one:

import { ProviderRejectedError } from "@opencoredev/sms-sdk";
import { db } from "./db";
import { sms } from "./sms";

try {
  await sms.send({ to: "+14155550123", body: "Your weekly summary is ready." });
} catch (error) {
  if (error instanceof ProviderRejectedError && error.category === "compliance") {
    await db.suppressions.add("+14155550123");
  } else {
    throw error;
  }
}

Answer HELP

SMS SDK never replies on its own. Some providers answer HELP and STOP automatically for some sender types, so check your provider settings before adding a reply, or the recipient gets two.

import { isE164 } from "@opencoredev/sms-sdk";
import type { SmsEvent } from "@opencoredev/sms-sdk/webhooks";
import { sms } from "./sms";

export async function answerHelp(event: SmsEvent): Promise<void> {
  if (event.type !== "recipient.help" || !isE164(event.from) || !isE164(event.to)) return;
  await sms.send({
    to: event.from,
    from: event.to,
    body: "Acme order updates. Reply STOP to unsubscribe. Help: support@example.com",
    idempotencyKey: `help-reply:${event.dedupeKey}`,
  });
}

The idempotency key comes from the inbound event, so a repeated webhook cannot send a second reply.

What else is your job

Your app also collects and stores consent before texting anyone, registers senders (US A2P 10DLC, toll-free verification, short codes, and sender IDs where required), and follows quiet hours, content rules, and country requirements. Registration and consent links to each provider’s rules. They change, so check them directly.

Next steps

Last updated on