Packages
Custom SMS transports
@notifkit/provider-twilio covers SMS. This
page is for Vonage, AWS SNS, Infobip or anything else: about thirty lines of transport code.
What the engine already handles
| Feature | State |
|---|---|
| Phone contacts on a user | Works: phone on an inline user, or POST /v1/users/:id/contacts |
channel: "sms" templates | Works: text is the key a transport reads |
| Targeting, segments, scheduling | Works |
| Quiet hours, throttling, opt-outs | Works. The engine gates SMS like any other channel |
| Suppression | Works, but only if your transport reports it (see below) |
| Fallback to or from SMS | Works |
| Actually sending | provider-twilio, or the transport below |
Writing one
The interface is small: declare the channel, read text, send it, map the result. The
example posts to Twilio's REST API because most people recognise it. Swap the endpoint and body
for your provider, and use @notifkit/provider-twilio if
Twilio really is your provider.
import type { Transport, NotificationDispatchedPayload, DeliveryResult } from "notifkit";
export class CustomSmsTransport implements Transport {
readonly channel = "sms" as const;
// Twilio's default is 1 message/second per number. Declaring it lets the
// delivery worker pace sends instead of collecting 429s.
readonly limits = { limit: 1, windowSeconds: 1 };
constructor(private opts: { accountSid: string; authToken: string; from: string }) {}
async send(task: NotificationDispatchedPayload): Promise<DeliveryResult> {
// destination is optional on the payload — report it, never substitute.
if (!task.destination) {
return { success: false, error: "no destination resolved for sms" };
}
const content = task.renderedContent.content as Record<string, unknown>;
const body = (content.text ?? content.body) as string | undefined;
if (!body) return { success: false, error: "template produced no text" };
const auth = Buffer.from(`${this.opts.accountSid}:${this.opts.authToken}`).toString("base64");
const res = await fetch(
`https://api.twilio.com/2010-04-01/Accounts/${this.opts.accountSid}/Messages.json`,
{
method: "POST",
headers: {
Authorization: `Basic ${auth}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({ To: task.destination, From: this.opts.from, Body: body }),
// The delivery worker races send() against its own 10s timeout and
// passes an AbortSignal — honour it instead of leaking the request.
signal: (task as any).signal,
},
);
const json = (await res.json()) as { sid?: string; message?: string };
if (!res.ok) return { success: false, error: json.message ?? `twilio ${res.status}` };
return { success: true, providerMessageId: json.sid ?? "" };
}
}
import { registerTransport } from "notifkit";
registerTransport(
new CustomSmsTransport({
accountSid: process.env.TWILIO_SID!,
authToken: process.env.TWILIO_TOKEN!,
from: "+15005550006",
}),
);
Without parseWebhook, nobody can opt out
Without parseWebhook, STOP replies are never recorded and people who opted out keep getting texts.