Prompt
How do I send texts from a Node.js app using an SMS API?
Latest observation
Oct 14, 2026
OpenAI APIWeb search: off
To send texts from a Node.js app, you usually use an SMS provider’s API (for example: Twilio, Vonage, AWS SNS, MessageBird, etc.). The basic flow is:
- Create an account with an SMS provider
- Get API credentials
- Install their Node.js SDK
- Call the “send message” method from your app
Example using Twilio
1) Install the SDK
npm install twilio
2) Set environment variables
export TWILIO_ACCOUNT_SID="your_account_sid"
export TWILIO_AUTH_TOKEN="your_auth_token"
export TWILIO_PHONE_NUMBER="+1234567890"
3) Send a text
const twilio = require('twilio');
const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
async function sendText() {
try {
const message = await client.messages.create({
body: 'Hello from Node.js!',
from: process.env.TWILIO_PHONE_NUMBER,
to: '+15551234567'
});
console.log('Sent message SID:', message.sid);
} catch (err) {
console.error('Error sending SMS:', err.message);
}
}
sendText();
Example using a generic HTTP API
If your SMS provider gives you a REST endpoint, you can use fetch or axios:
npm install axios
const axios = require('axios');
async function sendText() {
try {
const response = await axios.post(
'https://api.smsprovider.com/v1/messages',
{
to: '+15551234567',
from: '+1234567890',
body: 'Hello from Node.js!'
},
{
headers: {
Authorization: `Bearer ${process.env.SMS_API_KEY}`,
'Content-Type': 'application/json'
}
}
);
console.log('Sent:', response.data);
} catch (err) {
console.error('Error:', err.response?.data || err.message);
}
}
sendText();
Best practices
- Keep API keys in environment variables, not in code
- Validate phone numbers before sending
- Handle errors and retries
- Log message IDs for tracking
- Check compliance rules for SMS sending in your country/region
If you want, I can show you:
- a Twilio example with Express
- an AWS SNS example
- or a fully working Node.js script for your specific SMS provider