Skip to contentSkip to navigationSkip to topbar
On this page

Send SMS and MMS Messages in Node.js


In this tutorial, we'll show you how to use Programmable Messaging to send SMS and MMS messages from your Node.js application.

(warning)

Warning

The code samples in this tutorial use Twilio's Node helper library(link takes you to an external page). Let's get started!


Sign up for (or log in to) your Twilio account

sign-up-for-or-log-in-to-your-twilio-account page anchor
(information)

Info

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.

(warning)

Warning

If you are sending SMS to the U.S. or Canada, be aware of updated restrictions(link takes you to an external page) 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(link takes you to an external page) and a Twilio-powered phone number(link takes you to an external page).

If you're brand new to Twilio, you can sign up for a free trial account(link takes you to an external page) 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(link takes you to an external page) and get your Account SID and Auth Token. You will need those values for the code samples below.


Get a phone number with SMS (and MMS) capabilities

get-a-phone-number-with-sms-and-mms-capabilities page anchor

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(link takes you to an external page), check the 'SMS' box and click 'Search':

Buy an SMS-capable Twilio Number.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(link takes you to an external page):

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.

Send an SMS using the Programmable Messaging APILink to code sample: Send an SMS using the Programmable Messaging API
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createMessage() {
11
const message = await client.messages.create({
12
body: "This is the ship that made the Kessel Run in fourteen parsecs?",
13
from: "+15017122661",
14
to: "+15558675310",
15
});
16
17
console.log(message.body);
18
}
19
20
createMessage();

Output

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(link takes you to an external page).

(error)

Danger

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(link takes you to an external page)" 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!

(warning)

Warning

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.

Twilio's response

twilios-response page anchor

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.

Outgoing SMS Diagram.

Need some help?

Terms of service

Copyright © 2025 Twilio Inc.