Prompt
How do I build SMS notifications for account events?
Latest observation
To build SMS notifications for account events, you typically need 5 pieces:
-
An event source
Something in your app emits account events like:- account created
- password changed
- login from new device
- failed login attempts
- account locked / unlocked
- 2FA codes
- billing/payment events
-
A notification service A backend component listens for those events and decides:
- whether an SMS should be sent
- which template to use
- which phone number to send to
- whether to throttle or suppress messages
-
An SMS provider Use a service such as:
- Twilio
- Vonage/Nexmo
- AWS SNS
- MessageBird
- Telnyx
-
A phone number and consent system Store:
- verified phone number
- country/locale
- opt-in status
- notification preferences
- audit history
-
Deliverability + compliance handling Include:
- opt-out support where required
- rate limiting
- retry logic
- logging/monitoring
- message templates that are short and clear
Recommended architecture
A common flow:
Account event occurs
→ app publishes event to queue/webhook/log
→ notification worker consumes event
→ worker checks user preferences and policy
→ worker formats SMS
→ SMS provider sends message
→ delivery status stored in DB
This decouples your core app from SMS delivery and makes it easier to scale.
Example events to support
Security-related
- New login detected
- Password changed
- Email changed
- MFA code sent
- Suspicious activity detected
Account lifecycle
- Welcome message
- Phone number verified
- Account recovered
- Account locked due to too many attempts
Transactional
- Payment successful
- Subscription renewed
- Refund issued
- Invoice due reminder
Minimal data model
You’ll usually want something like:
users
- id
- phone_number
- phone_verified_at
- sms_opt_in
- locale
- timezone
notification_preferences
- user_id
- event_type
- sms_enabled
notification_log
- id
- user_id
- event_type
- channel
- status
- provider_message_id
- created_at
Example implementation pattern
1. Emit an event
When a user changes their password:
publish_event({
"type": "account.password_changed",
"user_id": 123,
"timestamp": "2026-10-14T12:00:00Z"
})
2. Worker handles it
def handle_event(event):
user = get_user(event["user_id"])
if not user.sms_opt_in:
return
if not user.phone_verified_at:
return
if event["type"] == "account.password_changed":
message = "Your password was changed. If this wasn't you, contact support immediately."
send_sms(user.phone_number, message)
3. Send via provider
Using Twilio as an example:
from twilio.rest import Client
client = Client(account_sid, auth_token)
client.messages.create(
body=message,
from_="+1234567890",
to=user.phone_number
)
Important best practices
1. Don’t send sensitive info
Avoid including:
- passwords
- full card numbers
- OTPs without expiration
- personal data that could be exposed
2. Verify phone numbers
Use SMS or phone verification before sending account-related alerts.
3. Respect user preferences
Let users choose:
- security-only alerts
- all account alerts
- no SMS, email only
4. Rate limit and deduplicate
Prevent spam by suppressing repeated alerts, for example:
- only one login alert per device per hour
- only one failed-login alert per 10 minutes
5. Handle retries carefully
If an SMS fails:
- retry transient provider errors
- don’t retry permanently invalid numbers endlessly
- mark as failed and notify via fallback channel if needed
6. Make messages actionable
Good:
Your password was changed. If this wasn’t you, reset it immediately.
Better than vague messages like:
Account event occurred.
Compliance notes
Depending on where you operate, SMS may be subject to:
- user consent requirements
- quiet hours / marketing restrictions
- opt-out language
- carrier registration rules
- privacy laws like GDPR/CCPA
- telecom regulations such as TCPA in the US
For transactional/security SMS, requirements are often lighter than marketing SMS, but you still need user consent and a legitimate purpose.
If you’re building this for production
A solid production setup usually includes:
- event bus or job queue
- notification templates
- user preference center
- provider abstraction layer
- delivery status webhook handler
- observability/metrics
- fallback channel like email
Suggested implementation stack
If you want something simple:
- Backend: Node.js, Python, Ruby, Go, etc.
- Queue: Redis Queue / Sidekiq / Celery / SQS / RabbitMQ
- SMS: Twilio or AWS SNS
- DB: PostgreSQL
- Monitoring: logs + alerts + webhook tracking
If you want, I can also give you:
- a step-by-step architecture diagram,
- a sample implementation in Node.js or Python, or
- a Twilio-based production-ready example.