How to Register a Twilio Phone Number and Send an SMS Using the Twilio CLI
Time to read: 3 minutes
There are over 4.5 billion text-enabled devices. This creates a huge opportunity to notify people about upcoming appointments, special events, breaking news, or commercial promotions via SMS. Sending SMS messages programmatically enables reaching many people almost simultaneously. With Twilio Programmable SMS, you can register your own virtual phone number and send SMS messages across the world.
This tutorial will show you how to set up Twilio CLI, register a phone number and send an SMS using .the new number
Important compliance note: There are rules for using SMS messaging and they vary between countries. Familiarize yourself with the countries’ rules in which you’ll be sending messages with the Twilio Regulatory Guidelines for SMS.
Prerequisites
To complete the project described in this tutorial you will need the following tools and resources:
- Twilio account – Sign up for free using this link and receive an additional $10 account credit when you upgrade to a regular account.
- Twilio CLI – The Twilio command-line interface requires Node.js and npm, which is installed with Node.js.
Get a Twilio Phone Number
Twilio SMS messages are sent using Twilio phone numbers, which provide instant access to local, national, mobile, and toll-free phone numbers in more than 100 countries with a developer-friendly API. You can get a Twilio phone number for free as part of your trial account.
Once you’ve created a Twilio account, you can use the Twilio CLI to get a phone number.
Note: If you’ve previously installed the CLI, be sure you have the latest version by executing the following command:
If you’ve stored your Twilio credentials as environment variables, the Twilio CLI will use them automatically. Otherwise you’ll have to login by using the following command:
This command will prompt you for your Account SID and Auth Token, both of which are available on the Twilio Console.
To list the phone numbers available for registration, use the following command, substituting the appropriate ISO 3166 alpha-2 country code for “US”, if necessary:
You should see a list similar to the below output:
Copy one of the numbers from the list and register it to your Twilio account by using:
If your registration attempt is successful, you should see:
Once registered, the phone number is available for your use (until you release it using the CLI or Twilio Console). Note that the SID associated with the phone number is a user secret and should be handled securely.
To verify the number has been successfully added to your account, you can use the Twilio CLI to send a test message. Run the following command, taking care to replace the placeholder values with your Twilio phone number and your personal SMS-enabled number, in E.164 format:
Note: With a trial account you can only send messages to phone numbers you’ve previously registered to your account. The SMS-enabled phone number you used to sign up for your Twilio account is the first number you’ve registered.
The API will return a response similar to the below output to indicate the SMS message has been successfully received and is queued to be sent:
Within a short time you should receive an SMS message on your phone:
You can check the status of the message creation request using the following CLI command:
You will see a response similar to the following:
If your message isn’t delivered you can use the status logs in the Twilio console to help you identify the problem. If you’re using a Twilio trial account, be sure you’re sending to a registered phone number.
Summary
In this post you’ve learned how to install and set up Twilio CLI and how to obtain a Twilio Phone number. This number is necessary to work with some of the Twilio products like Programmable SMS. You’ve also learned how to send SMS to any SMS-enabled number around the world with Twilio CLI.
Additional resources
Maciej Treder is a Senior Software Development Engineer at Akamai Technologies. He is also an international conference speaker and the author of @ng-toolkit. You can learn more about him at https://www.maciejtreder.com. You can also contact him at: contact@maciejtreder.com or @maciejtreder on GitHub, Twitter, StackOverflow, and LinkedIn.
Gabriela Rogowska contributed to this post.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.