SMS
The options, providers and errors of @dotpress/sms. For a guide, see SMS.
createSms()
send()
The phone number, the sender and the text are checked before the provider is called. An invalid number throws a SmsMessageError without the number in its message. messageId is the identifier given by the provider, if any.
A text in the GSM 03.38 alphabet (Latin letters, digits, é, è, à, €…) fits 160 characters per SMS; another character, such as ê, ç or an emoji, switches the whole text to Unicode: 70 characters per SMS.
Providers
The providers based on an HTTP API have no dependency, and accept baseUrl (region, proxy, tests) and fetch (replaces the global fetch); their errors are SmsSendError, with the HTTP status and the message of the API.
- Memory:
outbox.sentlists the messages,last()returns the last one,clear()empties it. Message IDs arememory-1,memory-2… - Twilio: the
Messagesresource of the account. The message ID is thesid; errors include the Twiliocode. - Telnyx: the API v2. The message ID is the
idof the message; errors include the code of the first Telnyx error. - Vonage: the Messages API, SMS channel, with the API key and secret (Basic authentication). Numbers are sent without their
+, as the API expects. The message ID is themessage_uuid. - OVHcloud:
POST /sms/{serviceName}/jobs, signed with the application secret and the consumer key, which needs this route. The timestamp of the signature follows the clock of the API, read once from/auth/time. The STOP clause, mandatory for commercial messages, is left out by default: setnoStopClause: falsefor marketing messages. A text outside the GSM alphabet is sent in 8 bits. A recipient refused by the API throws aSmsSendError. The message ID is the first of theidsof the response. - Brevo: transactional SMS (
/v3/transactionalSMS/send). The sender has 11 letters and digits, or 15 digits at most. A text outside the GSM alphabet is sent withunicodeEnabled. The message ID is themessageIdof the response; errors include the Brevocode. - Amazon SNS: the
Publishaction, to a phone number, signed with Signature Version 4 (Web Crypto). A sender in E.164 format is sent as the origination number (AWS.MM.SMS.OriginationNumber), any other one as the sender ID (AWS.SNS.SMS.SenderID). The message ID is theMessageIdof the response; errors include the SNS error code.credentialsas a function is called for each message: a credential provider of the AWS SDK (fromNodeProviderChain()) caches and refreshes them.
Custom providers
A provider has a name and a send(message) method, which returns { messageId } or nothing. The message is checked and frozen: to (E.164), from and text.
Throw a SmsSendError with the HTTP status of the API, and a SmsMessageError for a message the provider cannot send. Other errors are wrapped in a SmsSendError.
Logs
With a logger (a Dotpress or Pino logger, or any object with an info method taking (data, message)), each sent message is logged at the info level as SMS sent, with the provider and the message ID. Phone numbers and texts are not logged. Failures are thrown, not logged.
Errors
The error classes extend SmsError.