A Raspberry Pi is a full Linux computer, which makes texting easy. Python's requests library is enough to call a REST API, and gpiozero reads a button, a motion sensor or a door switch. Put them together and your project can text you when the freezer warms up, the garage door opens or someone presses a doorbell.

We make Ready, so the example uses the Ready messaging API and only the request shape we document. If you're using a tiny microcontroller instead, our post on texting from an ESP8266 or Arduino explains why those boards should hand the job to a server. A Pi is different: it can run the whole thing itself, as long as you protect the key and cap the alerts.

What you need

  1. A Pi with network access and Python 3. Any recent Raspberry Pi OS has it.
  2. An API key, sent in an X-API-Key header.
  3. A card and a number on the Ready account. The number is $2 a month. While you wire up the hardware, use a test key (Settings, Developers, Environment: Test). It simulates every send with no card, number or charge, so a bouncing switch can't cost anything.
  4. Recipients who agreed. Each request carries consent_attested: true, your promise the person opted in. Texting yourself or family who asked for alerts is fine. Texting anyone else needs their agreement first.

An account can send up to 500 texts a calendar month until its 10DLC registration is approved, and most approvals take 2 to 7 days. A hobby project that texts a few people is small, but registration still applies. Our 10DLC explainer says why.

Install and read a sensor

sudo apt install python3-requests python3-gpiozero

Wire a door switch (or any button) between GPIO 17 and ground. gpiozero handles the pull-up resistor and debouncing for you. The script below sends one text when the switch opens.

The script

import os
import time
import uuid
import requests
from gpiozero import Button

API_URL = "https://api.tryready.com/v1/messages"
API_KEY = os.environ["READY_API_KEY"]
TO = os.environ["ALERT_PHONE"]  # E.164, like +12125550199

COOLDOWN_SECONDS = 15 * 60
last_sent = 0.0

door = Button(17, pull_up=True)


def send_text(body):
    key = str(uuid.uuid4())
    for attempt in range(3):
        try:
            resp = requests.post(
                API_URL,
                headers={
                    "X-API-Key": API_KEY,
                    "Idempotency-Key": key,
                },
                json={"to": TO, "body": body, "consent_attested": True},
                timeout=15,
            )
        except requests.RequestException:
            time.sleep(2 ** attempt)
            continue
        if resp.status_code < 300:
            return True
        if resp.status_code in (429, 500, 502):
            time.sleep(2 ** attempt)
            continue
        print("SMS refused with HTTP", resp.status_code)
        return False
    return False


def on_open():
    global last_sent
    now = time.monotonic()
    if now - last_sent < COOLDOWN_SECONDS:
        return
    if send_text("Garage door opened."):
        last_sent = now


door.when_released = on_open

while True:
    time.sleep(60)

A few details. The idempotency key is made once per alert and reused on every retry, so a timeout followed by a retry can't send two texts. Only 429 and a couple of server errors are retried; a 4xx like a refused number won't improve. After a 503 or 504, or a code ending in OUTCOME_UNKNOWN, check GET /v1/messages before you resend. The retries guide covers each case. The response fields are in the messages API reference.

If you want a quick test before any wiring, the curl guide shows the raw request from a shell. For a fuller Python walkthrough, see sending SMS in Python.

Cap the alerts

Sensors are noisy. A door that swings in the wind, a temperature that hovers at the limit or a motion sensor watching a pet can trigger hundreds of events a day, and each text bills per segment. The script above has a 15-minute cooldown, but other patterns are worth knowing:

SituationBetter approach
Value flaps around a thresholdAlert once when it crosses, then once when it clears
Pi loses power or networkSend one "back online" text on boot, so you notice a gap
Repeated alarmEscalate: the first text immediately, a second after an hour if it persists
Night-time eventsCheck quiet hours before you design around a 3 AM text

Quiet hours apply to API texts. If the recipient's window is closed and doesn't open within 6 hours, the text isn't sent and the API answers 409. For urgent alerts, read how to handle the quiet-hours 409 first, because a text you expected at 3 AM may not arrive when you think it will.

Protect the key on the SD card

The Pi holds the key itself, so treat the SD card like a password file. Anyone who can pull the card or log in can read whatever's on it.

  • Put the key in an environment file outside your project folder, such as /etc/door-alert.env, readable only by the service user (chmod 600).
  • Never commit it. A .gitignore entry isn't enough if you ever pushed the file once.
  • Use a key for this device, so you can rotate it without breaking anything else.
  • If you ship Pis to other people, don't put your key on them. Have each one call a server you control, as in the ESP8266 pattern, and let the server do the sending.

Our key security and rotation post has the rotation steps.

Run it as a service

A script in a terminal dies when you log out. A systemd unit starts it on boot and restarts it after a crash:

[Unit]
Description=Door alert
After=network-online.target
Wants=network-online.target

[Service]
User=pi
EnvironmentFile=/etc/door-alert.env
ExecStart=/usr/bin/python3 /home/pi/door_alert.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Save it as /etc/systemd/system/door-alert.service, put READY_API_KEY and ALERT_PHONE in the env file, then run sudo systemctl enable door-alert and sudo systemctl start door-alert. Read the log with journalctl -u door-alert.

What it costs

Each segment on the Standard rate is $0.0075 plus a $0.0045 carrier fee. As an example, a door alert that fires 60 times a month, one segment each, costs 60 × ($0.0075 + $0.0045) = $0.72, plus the $2 monthly number. That all-in figure excludes screening, the optional Scrub lists and Number validation switches. Keep alerts under 160 characters and avoid emoji, which cut a segment to 70 characters. See pricing.

Ready's all-in prices per segment include the carrier fee but not screening: Scrub lists and Number validation, the two switches in Settings under Compliance, each add $0.003 per segment when switched on, shown as screening on your bill, and both are off by default. Unless you turn the opt-out footer off, the first text to each contact, and one again from time to time, also gets "Reply STOP to opt out" and your business name if the message does not already have them, so a text close to the segment limit can bill one more segment.

Accounts opened before November 12, 2026 keep their signup prices, shown under Billing.

The short version

Read the sensor with gpiozero, post to the API with requests, reuse one idempotency key across retries, cap how often a device can text and keep the key in a locked-down env file. Build it on a test key first, then start texting today once the card, number and registration are in place.