Twilio has become a favorite among devs looking to integrate messaging, calls, and streaming into their web and mobile apps. Their APIs are some of the most comprehensive and simple on the market, making it great for bootstrapping projects.
Before you start writing your NodeJS application, you need to set some things up
- Install NodeJS on your computer.
- Before building your NodeJS application, you need to create a Twilio account to get data that will help you run your application.
To set up your Twilio account:
- Go to https://www.twilio.com/try-twilio.
- Enter the information required and accept the Terms of Service.
- Next, you will be asked a few questions by Twilio to aid you in using the platform. Answer them to finally reach your dashboard.
- Once you have signed up, the first thing you need to do is verify our email address.
- Add a phone number and verify that as well using OTP.
- Your dashboard will now display your “ACCOUNT SID” and “AUTH TOKEN”. Twilio hides “AUTH TOKEN” by default to keep your account safe from misuse.
- The dashboard will also display your account’s “TRIAL BALANCE”. This balance keeps decreasing as you keep using Twilio’s features.
- Twilio trial accounts only let you send texts to verified phone numbers. So you need to add any numbers and verify them using OTP before you can text them using the Twilio API.
- Generate a Twilio trial number. This number will act as the sender’s number inside your application.
Follow the steps below to create your NodeJS application for sending texts using the Twilio API.
- Using the terminal, run
npm initinside the folder you just created. Accept all the defaults that pop on the terminal.
- Using the terminal again run the following command to install the Twilio dependency :
npm install twilio
- To make use of environment variables you need to install a package called dotenv by running the following command in the terminal:
npm install dotenv
- Now create a file named .env inside your project folder. Add your Twilio “ACCOUNT SID” and “AUTH TOKEN” to this file as shown below
TWILIO_ACCOUNT_SID=<your account sid here>
TWILIO_AUTH_TOKEN=<your auth token here>
Note: Never share your account sid or auth token with anyone.
7. We’ll then invoke the Twilio API like so
Well, you did it. You just created a basic application using NodeJS with Twilio API to send texts. When you are ready to show off your web app to the world, deploy it on Codesphere, the first bottle-neck free cloud platform.
Let us know in the comments what you plan to build using the Twilio API!