Prompt
How do I send email, SMS, and push notifications from one API?
Latest observation
To send email, SMS, and push notifications from one API, you typically build a unified notification service that accepts one request format and routes it to the right provider(s) behind the scenes.
1) Define one common API
Design a single endpoint like:
POST /notifications/send
Example request:
{
"channel": "email",
"to": "user@example.com",
"template_id": "welcome_001",
"data": {
"first_name": "Ava"
}
}
Or for SMS:
{
"channel": "sms",
"to": "+15551234567",
"template_id": "otp_001",
"data": {
"code": "483921"
}
}
Or push:
{
"channel": "push",
"to": "user_123",
"template_id": "promo_001",
"data": {
"discount": "20%"
}
}
2) Normalize the message model
Keep a shared internal schema with fields like:
channel:email | sms | pushto: recipient address / phone / device token / user idsubject: for email onlytemplate_iddata: template variablesattachments: optionalpriority: optionalmetadata: optional
3) Route to channel-specific providers
Your backend takes the request and dispatches it to the appropriate service:
- Email → SendGrid, SES, Mailgun, Postmark
- SMS → Twilio, Vonage, MessageBird
- Push → FCM, APNs, Expo, OneSignal
Example flow:
- API receives request
- Validate payload
- Render template
- Choose provider by channel
- Send message
- Return status / message ID
4) Use templates for consistency
Instead of sending raw text every time, store templates per channel:
welcome_emailotp_smspassword_reset_push
This lets you reuse the same API while adapting output to each channel.
5) Consider async delivery
For reliability, don’t send directly in the request thread. Use a queue:
- API → database / queue
- worker → provider API
- callback/webhook → delivery status
This helps with retries, rate limits, and failures.
6) Return a unified response
Example response:
{
"notification_id": "notif_789",
"status": "queued",
"channel": "sms"
}
7) Optional: support multiple channels in one request
You can also let clients send to several channels at once:
{
"channels": ["email", "sms", "push"],
"to": {
"email": "user@example.com",
"sms": "+15551234567",
"push": "device_token_abc"
},
"template_id": "security_alert",
"data": {
"device": "Chrome on Windows"
}
}
8) Practical implementation pattern
A common architecture:
- API Gateway / Notification API
- Template service
- Channel adapters:
- Email adapter
- SMS adapter
- Push adapter
- Queue/worker system
- Delivery tracking + webhook handlers
9) Example pseudocode
def send_notification(payload):
channel = payload["channel"]
if channel == "email":
return send_email(payload)
elif channel == "sms":
return send_sms(payload)
elif channel == "push":
return send_push(payload)
else:
raise ValueError("Unsupported channel")
10) Best practices
- Validate phone numbers, emails, and push tokens
- Store provider credentials securely
- Use retries with backoff
- Track delivery receipts and failures
- Respect opt-in/opt-out preferences
- Support localization and time zones
- Log message status for auditing
If you want, I can also show you:
- a REST API design,
- a Node.js / Python implementation, or
- a recommended database schema for this notification system.