An ESP8266 or Arduino board shouldn't hold your SMS API key. The safe pattern is to have the device send a small HTTPS request to a server you control, and let that server call the SMS API. The device only reports an event, and your server decides whether a text goes out.
Why not call the SMS API directly
- The key lives on the device. Anyone with the board can read it from flash.
- No consent or opt-out check. The device can't know who opted out.
- No retry safety. A flaky connection and a retry can double-send.
- TLS is a burden. Small boards have limited memory for certificate handling.
The pattern
- The board detects an event: a temperature limit, a door open, a water sensor.
- It posts a short message to your endpoint, signed with a per-device secret.
- Your server checks the secret, applies a rate limit and decides on a text.
- The server calls
POST /v1/messageswith anIdempotency-Key.
The server side
The request body is simple: to in E.164, body and consent_attested. The recipient must have agreed to receive alerts from you. Add your key in the X-API-Key header from an environment variable. See the messages docs and our Node.js guide.
Don't text on every reading
| Event | Better approach |
|---|---|
| Sensor flaps around a threshold | Alert once, then wait for it to clear |
| Device offline | Send one "offline" alert after a timeout |
| Repeated alarm | Cap texts per hour per device |
Each text bills per segment, and a noisy sensor can burn through a balance fast. Quiet-hours rules also apply to API texts, so a 3 AM alert to a recipient may be held or refused unless it's a reply. For urgent alerts, read how to handle the quiet-hours 409 before you design around it.
The takeaway
Keep the key, the consent check and the retry logic on your server, and let the device report events. For the alert-volume problem see SMS API rate limits.