This tutorial walks through sending your first SMS with the Twilio Node.js SDK, from installing the package through authenticating with your credentials and dispatching a real message. It targets JavaScript developers who have a Twilio account and want to add SMS to a Node.js script or application. By the end, your terminal will print a message SID confirming that Twilio queued the message for delivery.
Prerequisites and Setup
You need Node.js 14 or higher, a Twilio account with a purchased phone number, and your Account SID and Auth Token from the Twilio Console. Run npm install twilio dotenv to install the SDK and a dotenv helper that keeps credentials out of your source files. Create a .env file at the project root with three variables: TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER. Add .env to your .gitignore before writing a single line of code.
npm install twilio dotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
The Complete Code
The Twilio Node.js SDK exports a factory function that accepts your Account SID and Auth Token and returns a fully configured client. Calling client.messages.create() with body, from, and to sends the SMS and returns a Promise that resolves to a MessageInstance. The sid property on that object uniquely identifies the message so you can look it up in the Twilio Console at any time. Using async/await keeps the code flat and readable without nested callbacks.
require('dotenv').config();
const twilio = require('twilio');
const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
async function sendSMS() {
try {
const message = await client.messages.create({
body: 'Hello from Twilio and Node.js!',
from: process.env.TWILIO_PHONE_NUMBER,
to: '+15551234567',
});
console.log('SID:', message.sid);
console.log('Status:', message.status);
} catch (err) {
console.error('Twilio error', err.code, err.message);
}
}
sendSMS();
Testing and Debugging
Run node send-sms.js and verify a SID starting with SM appears in the terminal alongside a status of queued. If you receive error code 21608, the destination number is not verified under Verified Caller IDs in the Twilio Console, which is required for trial accounts. Error code 21211 means the to number is not in valid E.164 format with a leading plus sign and country code. Navigate to Monitor in the Twilio Console to see the full delivery chain if the script exits cleanly but no SMS arrives on the handset.
Taking It Further
Swap the hardcoded to number for a command-line argument using process.argv[2] to make the script reusable without editing the source. Add a statusCallback URL to the create options so Twilio POSTs delivery receipts to your server without you polling the API. Replace the from number with a messagingServiceSid to route messages through a Twilio Messaging Service that handles sender pool selection and carrier registration automatically. Wrap the script in an Express route to expose SMS sending as an API endpoint that other services can call over HTTP.
Contact Telphi Consulting to discuss building production-grade SMS workflows tailored to your Node.js application architecture.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.