The Twilio CLI is a nifty and convenient tool to have under your belt especially if you're a developer who loves using the command line. For those who are fans of Twilio, it's also a great way to build fast and test out the power of Twilio.
- A free or paid Twilio account. If you are new to Twilio get your free account now! (If you sign up through this link, Twilio will give you $10 credit when you upgrade.)
- The Twilio CLI.
- A smartphone with active service, to test the project.
Set up the Twilio CLI environment
If this is your first time installing the Twilio CLI, check out the Twilio CLI quickstart page for instructions on how to install it on your machine.
For developers using a Mac, it's very quick and convenient to install the CLI using Homebrew, so make sure you check out how to install Homebrew on your machine if you don’t have it already. Then, you can run the command
brew tap twilio/brew && brew install twilio to install the Twilio CLI.
For Windows developers, make sure you install Node.js and npm first, as you will need it to install the CLI with the command
npm install twilio-cli -g.
Once you have the Twilio CLI installed, type
twilio login to log in to your account. You’ll need your Account SID and Auth Token from the Twilio Console to login.
Buy a Twilio phone number
If you haven't done so already, purchase a Twilio number to send the SMS.
Log in to the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.
In the Buy a Number screen you can select your country and check Voice in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the Number field.
Click the Search button to see what numbers are available, and then click “Buy” for the number that you like from the results. After you confirm your purchase, click the Close button.
Send an SMS with Twilio CLI
Run the following command in your command line and replace the
from parameter with your new Twilio number, the
to parameter with your phone number, and the
body parameter with a message such as "Hello from your command line!". The phone numbers should be in E.164 format.
twilio api:core:messages:create \ --from "<YOUR_TWILIO_NUMBER>" \ --to "<YOUR_PHONE_NUMBER>" \ --body "Hello from your command line!"
Wait a minute… then check your phone!
What's next for sending SMS with Twilio CLI?
Congratulations on using Twilio to send an SMS! Just imagine how much time you could be saving by sending a quick text message to a friend or family member during your work day without having to pick up your phone. That is, until you have to respond to them!
There's so much you can accomplish with the Twilio CLI to help you build fast and test out the power of Twilio. Check out the examples on the docs to learn more about quick commands you can use on the Twilio CLI.
Now that you know the basics of sending SMS with Twilio CLI, you can try to expand on the project by sending a message to a list of contacts. Here are some other cool articles you can explore:
- Check out five Twilio CLI features you should know about
- Send a romantic poem over the phone with Twilio CLI
- Create a simple notification system using Twilio CLI, Programmable Voice, and Programmable Messaging
Let me know about the projects you're building with the Twilio CLI by reaching out to me over email!
Diane Phan is a Developer Network editor on the Developer Voices team. She loves to help programmers tackle difficult challenges that might prevent them from bringing their projects to life. She can be reached at dphan [at] twilio.com or LinkedIn.