How to send SMS from your app
Sending one text is ten lines of code. Sending texts people agreed to, that arrive, and that nobody can abuse, is the real work.

Key takeaways
- SMS always goes out through a server function holding your provider credentials, never from the browser.
- Most countries now expect a registered sender. In the US, texting from a regular 10-digit number needs A2P 10DLC registration, which takes days, so start early.
- Rate-limit every endpoint that triggers a text and restrict destination countries, or bots will use your form to run up your bill.
You send SMS from an app by calling an SMS provider's API (Twilio is the most common; Vonage, Plivo and MessageBird work the same way) from a server function that holds your account credentials. The browser asks your function to send a message, the function checks who is asking and whether the recipient agreed, then calls the provider. The code is short. Registration, consent and abuse limits are what take the time, and they decide whether your messages arrive at all.
What you'll build
The example is an appointment app that texts a reminder 24 hours before each booking and a confirmation when a booking is made. Customers can reply STOP to opt out, and the app records it. This covers the three message types most apps need: transactional (confirmation), scheduled (reminder) and inbound (replies).
What you need
- A provider account. With Twilio you get an account SID and an auth token (or, better, a scoped API key and secret), plus a phone number or a Messaging Service.
- Credentials in Backend → Secrets, for example `TWILIO_ACCOUNT_SID`, `TWILIO_AUTH_TOKEN` and `TWILIO_MESSAGING_SERVICE_SID`. Your edge functions read them as environment variables; they never reach the page.
- A registered sender for your audience. In the US, application-to-person texting from a 10-digit long code requires A2P 10DLC brand and campaign registration; toll-free numbers need their own verification. Other countries have their own sender ID rules. Check your provider's current requirements per country before you promise texts to anyone.
- A consent field. A `sms_opt_in` boolean and `sms_opt_in_at` timestamp on the customer, set by an unticked checkbox with clear wording.
Trial accounts are limited: Twilio trials, for example, only deliver to phone numbers you have verified yourself and add a trial notice to the message. That is fine for building, and a common source of 'it works for me but not for my customer' reports.
How a message moves
Outbound
- 01A booking is created. The app calls `send-sms` with `{ booking_id, kind: "confirmation" }`, never with a phone number or message text.
- 02The function loads the booking and customer from the database, checks `sms_opt_in` is true and the number is not in `sms_opt_outs`, and builds the text from a template.
- 03It calls the provider's messages API with the secret credentials and a status callback URL.
- 04It writes a row to `sms_log`: `booking_id`, `to`, `kind`, `provider_message_id`, `status: "queued"`.
- 05The provider later posts delivery updates (sent, delivered, failed, undelivered) to your status function, which updates the log row.
Passing ids instead of phone numbers matters. A function that accepts `{ to, body }` from the browser is a free SMS gateway for anyone who finds it.
Scheduled reminders
A scheduled job runs every 15 minutes, finds bookings starting between 23 and 24 hours from now with `reminder_sent_at` empty, sends each one through the same function and stamps `reminder_sent_at`. The stamp is what stops a job that runs twice from texting twice. Startbase Cloud runs these as database jobs on a cron schedule, shown in UTC, so store booking times in UTC and convert for display.
Inbound replies
Point your number's incoming message webhook at a public `sms-inbound` function. It verifies the provider's signature, then handles keywords: STOP, UNSUBSCRIBE and similar add the number to `sms_opt_outs`; START removes it; anything else can be forwarded to the business by email. Twilio handles standard opt-out keywords on its side by default too, but keep your own record so the app never tries to send.
Paste-ready brief
Add SMS to my booking app using Twilio. Credentials are in Secrets as TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN and TWILIO_MESSAGING_SERVICE_SID. Add an unticked 'Text me booking reminders' checkbox at checkout that sets sms_opt_in and sms_opt_in_at. Create an edge function send-sms that takes only a booking_id and a kind (confirmation or reminder), loads the phone number from the database, refuses if the customer has not opted in or is in sms_opt_outs, sends from a template, and logs to an sms_log table. Add a status callback function that updates sms_log, and an inbound function that verifies the Twilio signature and records STOP and START. Add a job every 15 minutes that sends reminders 24 hours ahead and stamps reminder_sent_at. Limit each customer to 5 texts a day and only allow numbers in the US and Canada.
Templates that read well on a lock screen
Keep the templates in a `sms_templates` table, not in code, so the business can fix wording without a deploy. A reminder that works: "Northside Physio: see you tomorrow at 2:30 PM with Dana. Reply C to confirm or call 555 0100 to move it. Reply STOP to opt out." It names the business first (people ignore unknown senders), gives the one fact that matters, offers one action and states the opt-out. Leave out links where you can: carriers filter messages with shortened links aggressively, and a full link eats a third of a segment.
What texting costs
SMS is priced per message segment, and the price depends on the destination country, the sender type and carrier fees, so look up the current numbers on your provider's pricing page rather than trusting a blog post. Two things are true everywhere. First, a single segment holds 160 plain GSM characters, but one emoji or accented character switches the message to a different encoding with a 70-character limit, so a friendly message can quietly cost two or three segments. Second, international texts cost many times more than domestic ones in most markets, which is exactly why fraudsters target them.
Pitfalls
- SMS pumping fraud. Bots submit premium-rate numbers to any form that sends a code or a link, and the attacker earns a share of the fees. Defend with per-number, per-IP and per-account rate limits, a captcha on public forms, and your provider's geographic permissions set to only the countries you serve.
- Keys in the frontend. Your Twilio auth token can send messages and read your logs. It belongs only in Secrets. The publish security check looks for leaked keys, but do not rely on it as the plan.
- Unsigned webhooks. Anyone can post to your inbound URL. Verify the provider signature against your auth token and reject anything that fails, or a stranger can opt your customers out or in.
- Texting without consent. Marketing texts without documented opt-in carry real legal risk in the US and EU. Keep the timestamp and the exact checkbox wording you showed.
- Quiet hours and time zones. A reminder at 3 a.m. local time gets you reported as spam. Store each customer's time zone and hold non-urgent texts until a sensible hour.
- Treating 'queued' as 'delivered'. Only the status callback tells you a message arrived. Show failures to the business so they can call instead.
Email is cheaper and needs no sender registration, so many apps send confirmations by email and keep SMS for the reminder that prevents a no-show. The email guide covers that side.
Frequently asked questions
Can I send SMS directly from JavaScript in the browser?
No. Sending requires your provider credentials, and anything in the browser is readable by every visitor. The browser calls your edge function, which holds the credentials in Secrets and does the sending.
Why are my texts not arriving in the US?
The most common causes are an unregistered 10-digit sender (A2P 10DLC registration is required), a trial account sending to an unverified number, or the recipient having replied STOP. The provider status callback and error codes tell you which.
How do I let users reply STOP to unsubscribe?
Point your number incoming message webhook at a public function that verifies the provider signature, records STOP in an opt-out table and START to undo it, and have your send function check that table every time.
Do I need custom code for SMS login codes?
Not on Startbase Cloud. Phone (SMS) is a built-in sign-in provider: add your Twilio SID, token and message service SID in the sign-in settings.

