In this tutorial, we'll show you how to use Programmable Messaging to send SMS and MMS messages from your Node.js application.
While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada. Learn more in this support article.
The code samples in this tutorial use Twilio's Node helper library. Let's get started!
If you have a Twilio account and Twilio phone number with SMS capabilities, you're all set! Feel free to jump straight to the code.
If you are sending SMS to the U.S. or Canada, be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained through Free Trial.
Before you can send messages from the Twilio API, you'll need a Twilio account and a Twilio-powered phone number.
If you're brand new to Twilio, you can sign up for a free trial account to get started.
Once you've signed up and selected a project (the "Learn and Explore" template will work for this tutorial), head over to your Console and get your Account SID and Auth Token. You will need those values for the code samples below.
Sending messages requires a Twilio phone number with SMS capabilities. If you don't currently own a Twilio phone number with SMS capabilities, you'll need to buy one. After navigating to the Buy a Number page, check the 'SMS' box and click 'Search':
HTTP POST
request to Twilio's Message resource.Twilio's helper library for Node.js helps you create a new instance of the Message resource, specifying the To, From, and Body parameters of your message.
If you don't already have the Node helper library installed, you can do so using npm:
npm install twilio
This will install the twilio
module so that Node.js scripts in the current directory can use it.
Now, create a file named sms.js
and include the following code.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createMessage() {11const message = await client.messages.create({12body: "This is the ship that made the Kessel Run in fourteen parsecs?",13from: "+15017122661",14to: "+15558675310",15});1617console.log(message.body);18}1920createMessage();
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"api_version": "2010-04-01",4"body": "This is the ship that made the Kessel Run in fourteen parsecs?",5"date_created": "Thu, 30 Jul 2015 20:12:31 +0000",6"date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",7"date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",8"direction": "outbound-api",9"error_code": null,10"error_message": null,11"from": "+15017122661",12"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",13"num_media": "0",14"num_segments": "1",15"price": null,16"price_unit": null,17"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"status": "queued",19"subresource_uris": {20"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"21},22"tags": null,23"to": "+15558675310",24"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"25}
Replace the placeholder values for accountSid
and authToken
with your unique values. You can find these in your Twilio console.
Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out our blog post "Working with Environment Variables in Node.js" for guidance.
You'll tell Twilio which phone number to use to send this message by replacing the from
number with the Twilio phone number you purchased earlier.
Next, specify yourself as the message recipient by replacing the to
number with your mobile phone number. Both the from
and to
parameters must use E.164 formatting ("+
" and a country code, e.g., +16175551212
).
We also include the body
parameter, which contains the content of the SMS we're going to send.
Once you've updated the code sample, you can test it out by running it from the command line:
node sms.js
In just a few moments you should receive an SMS!
If you're using a trial account, you'll notice that any messages you send will always begin with "Sent from a Twilio trial account." Once you upgrade your account, you will no longer see this message. Learn more about sending SMS and MMS messages from a trial account.
Let's take a moment to understand what's going on behind the scenes when you send this request to Twilio.
When Twilio receives your request to send an SMS via the REST API, it will check that you've included a valid Twilio phone number in the From
field. Twilio will then either queue the SMS or return this HTTP error in its response to your request.