Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Twilio Best Practices

You're reading from   Twilio Best Practices Learn how to build powerful real-time voice and SMS applications with Twilio

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781782175896
Length 178 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Node.js


The twilio-node library is available on npm, which is Node's package manager that is included when you download Node itself.

To install the library, run npm install twilio --save in your project directory, or npm install –g twilio to install it globally across your machine.

In your code, you'll just need to require the library and instantiate a client (client.js):

var account_sid = process.env.TWILIO_ACCOUNT_SID;
var auth_token = process.env.TWILIO_AUTH_TOKEN;
var client = require('twilio')(account_sid, auth_token);

The syntax for sending an SMS from this point onwards is similar to Ruby, using a dictionary of arguments.

However, there is a complication in that Node uses asynchronous requests when contacting the API, meaning that we can't simply save what the method returns to a variable. Instead, we provide a callback that will be invoked with the Message object once it has been sent successfully (send_sms.js):

client.messages.create({
  body: "This is an SMS message.",
  to: "+441708300116...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime