> For AI agents: the complete documentation index is available at https://dotpress.dev/llms.txt, the full documentation bundle is available at https://dotpress.dev/llms-full.txt.

# SMS

The options, providers and errors of `@dotpress/sms`. For a guide, see [SMS](https://dotpress.dev/services/sms.md).

## createSms()

| Option     | Description                                                                                              |
| ---------- | -------------------------------------------------------------------------------------------------------- |
| `provider` | Required: the provider that sends.                                                                       |
| `from`     | Required: the default sender, a phone number in E.164 format, a short code or an alphanumeric sender ID. |
| `logger`   | Optional: logs each sent message.                                                                        |

### send()

```ts
const { messageId } = await sms.send({ to, text, from })
```

| Option | Description                                                                                    |
| ------ | ---------------------------------------------------------------------------------------------- |
| `to`   | Required: a phone number in E.164 format, `+33612345678`.                                      |
| `text` | Required: the text, not empty. Long texts are split by the provider and billed as several SMS. |
| `from` | Replaces the default sender.                                                                   |

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.

| Provider                   | Import                   | Settings                                                                                                                                                                     |
| -------------------------- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `createMemoryProvider()`   | `@dotpress/sms`          | —                                                                                                                                                                            |
| `createConsoleProvider()`  | `@dotpress/sms`          | `text` (prints the text, off by default: messages contain secrets), `write`                                                                                                  |
| `createTwilioProvider()`   | `@dotpress/sms/twilio`   | `accountSid`, and `authToken`, or `apiKey` and `apiSecret`                                                                                                                   |
| `createTelnyxProvider()`   | `@dotpress/sms/telnyx`   | `apiKey`                                                                                                                                                                     |
| `createVonageProvider()`   | `@dotpress/sms/vonage`   | `apiKey`, `apiSecret`; `baseUrl: 'https://api-eu.vonage.com'` for the EU region                                                                                              |
| `createOvhcloudProvider()` | `@dotpress/sms/ovhcloud` | `applicationKey`, `applicationSecret`, `consumerKey`, `serviceName` (`sms-ab12345-1`); `noStopClause` (`true` by default); `baseUrl: 'https://ca.api.ovh.com/1.0'` in Canada |
| `createBrevoProvider()`    | `@dotpress/sms/brevo`    | `apiKey`                                                                                                                                                                     |
| `createSnsProvider()`      | `@dotpress/sms/aws-sns`  | `region`, `credentials` (`{ accessKeyId, secretAccessKey, sessionToken? }` or a function returning them); `smsType` (`Transactional` by default)                             |

- **Memory**: `outbox.sent` lists the messages, `last()` returns the last one, `clear()` empties it. Message IDs are `memory-1`, `memory-2`…
- **Twilio**: the `Messages` resource of the account. The message ID is the `sid`; errors include the Twilio `code`.
- **Telnyx**: the API v2. The message ID is the `id` of 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 the `message_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: set `noStopClause: false` for marketing messages. A text outside the GSM alphabet is sent in 8 bits. A recipient refused by the API throws a `SmsSendError`. The message ID is the first of the `ids` of 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 with `unicodeEnabled`. The message ID is the `messageId` of the response; errors include the Brevo `code`.
- **Amazon SNS**: the `Publish` action, 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 the `MessageId` of the response; errors include the SNS error code. `credentials` as 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

| Error             | When                                                        |
| ----------------- | ----------------------------------------------------------- |
| `TypeError`       | Invalid options of `createSms()`, `send()` or a provider.   |
| `SmsMessageError` | An invalid phone number or sender, or an empty text.        |
| `SmsSendError`    | The provider failed. Fields: `provider`, `status`, `cause`. |

The error classes extend `SmsError`.
