← All use cases

Server alert SMS

Push notifications and chat messages are easy to miss at night, and they need mobile data. An SMS reaches any phone with signal. With NS SMS a health check or a cron job sends the alert in one curl call.

The problem

Monitoring tools that text you usually sell SMS as an add-on with credits per country.

Email and chat alerts depend on the same internet connection and apps that are muted at night.

Why sending from your own number works

  • One curl call from any script, cron job or CI step, with no SDK.
  • to accepts a list of up to 100 numbers, so the whole on-call team gets the alert from one request.
  • The NS SMS phone sends over the mobile network, independent of the network of the server that has the problem.
  • device.offline webhooks tell you when the SMS phone itself stops reporting.

Honest limits

  • Send on state changes (down, then back up), not every minute while something is down, or a long outage eats the daily cap.
  • The alert path needs our API and your phone to be reachable. For critical systems keep a second channel, such as email or a paging service.

SMS templates

Short, plain characters only (GSM-7), so each one fits a single SMS of up to 160 characters. Accents, emoji and curly quotes switch the message to Unicode, where one SMS holds only 70 characters.

ALERT: https://example.com/health answers 503 since 03:12

57 characters, 1 SMS

OK: https://example.com/health is back up.

42 characters, 1 SMS

ALERT db-1: disk / is 93% full

30 characters, 1 SMS

Code example

No account, email or password. Install the app, turn on the API and copy your phone ID and API key.

Health check that texts on down and up (shell)

A state file makes sure you get one text when the site goes down and one when it recovers.

healthcheck.sh
#!/bin/sh
# Texts the on-call team when the site does not answer with 200.
URL="https://example.com/health"
STATE=/tmp/healthcheck.down

CODE_HTTP=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL")
if [ "$CODE_HTTP" != "200" ] && [ ! -f "$STATE" ]; then
  touch "$STATE"
  curl -s -X POST https://sms-api.n-soft.pl/v1/sms/send \
    -H "Authorization: Bearer $NS_SMS_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"device_id":"'"$NS_SMS_DEVICE_ID"'","to":["+14155550123","+14155550124"],"text":"ALERT: '"$URL"' answers '"$CODE_HTTP"' since '"$(date +%H:%M)"'"}'
elif [ "$CODE_HTTP" = "200" ] && [ -f "$STATE" ]; then
  rm "$STATE"
  curl -s -X POST https://sms-api.n-soft.pl/v1/sms/send \
    -H "Authorization: Bearer $NS_SMS_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"device_id":"'"$NS_SMS_DEVICE_ID"'","to":["+14155550123","+14155550124"],"text":"OK: '"$URL"' is back up."}'
fi

Run it every 2 minutes (cron)

Set the keys in the crontab, or in a file the script reads.

crontab -e
NS_SMS_API_KEY=nss_YOUR_API_KEY
NS_SMS_DEVICE_ID=YOUR_DEVICE_ID
*/2 * * * * /usr/local/bin/healthcheck.sh

Disk space alert (Python)

The same idea for any metric: check, and text when a limit is crossed.

disk_alert.py
import os
import shutil
import socket

import requests

LIMIT = 90  # percent

usage = shutil.disk_usage("/")
percent = usage.used * 100 // usage.total
if percent >= LIMIT:
    requests.post(
        "https://sms-api.n-soft.pl/v1/sms/send",
        headers={"Authorization": f"Bearer {os.environ['NS_SMS_API_KEY']}"},
        json={
            "device_id": os.environ["NS_SMS_DEVICE_ID"],
            "to": ["+14155550123"],
            "text": f"ALERT {socket.gethostname()}: disk / is {percent}% full",
        },
        timeout=15,
    ).raise_for_status()

Without code

The same thing works from these tools:

Other use cases