1am: Send text messages with Twilio CLI

Leave your phone on the table and stay in the Terminal. Did you know that you can send text messages directly from the command line? In this article, we'll explore how to send text messages using the command line interface, allowing you to stay connected and send messages without the need for a smartphone or messaging app.

What does this article replace?

This article replaces GUI-based messaging apps like WhatsApp, iMessage, or standard SMS apps on your smartphone.

How long will it take to set up?

It should take you about 15 minutes to set up Twilio CLI and send your first text message from the terminal.

4 out of 5 stars

Difficulty Rating:

Texting from the terminal? You're taking on a more challenging task! We give this a 4/5 on the difficulty level scale.

Prerequisites

Before we begin, there are a few prerequisites for sending text messages from the command line:

  1. A Twilio account: Twilio is a cloud communications platform that provides APIs for sending and receiving text messages. Create a Twilio account at twilio.com if you don't have one already. When on a trial version, Twilio provides a free virtual number that you can use for testing.

  2. Twilio CLI: Install the Twilio CLI, a command-line tool that helps interact with Twilio's APIs. Follow the installation instructions below.

  3. Twilio phone number: Obtain a Twilio phone number that you can use to send text messages. You can purchase a phone number from the Twilio Console. For trial accounts, you can use the free virtual number provided by Twilio.

Step 1: Install Twilio CLI

To install the Twilio CLI using Homebrew, follow these steps:

  1. Open your terminal.

  2. Run the following command to tap into the Twilio brew repository:

brew tap twilio/brew
  1. Next, install the Twilio CLI by running the following command:
brew install twilio

Step 2: Configure Twilio CLI

After installing the Twilio CLI, configure it by running the following command:

twilio login

Follow the prompts to enter your Twilio account credentials and complete the authentication process.

Step 3: Send a Text Message

To send a text message from the command line using Twilio CLI, run the following command:

twilio api core messages create --from=YOUR_TWILIO_PHONE_NUMBER --to=RECIPIENT_PHONE_NUMBER --body="Your message goes here"

Replace YOUR_TWILIO_PHONE_NUMBER with your Twilio phone number (the one you obtained from the Twilio Console), and RECIPIENT_PHONE_NUMBER with the recipient's phone number. Additionally, customize the message by modifying the body parameter.

After running the command, Twilio API will send the text message to the specified recipient's phone number.

Troubleshooting

If you encounter any issues with sending the text message, consider the following troubleshooting steps:

  1. Verify Twilio CLI version: Ensure that you have the latest version of Twilio CLI installed. You can update it by running the following command:

    twilio plugins:update
    
  2. Confirm Twilio account credentials: Double-check that you have logged in to Twilio CLI with the correct account credentials by running:

    twilio whoami
    
  3. Check API request response: To view the API response details, you can use the --properties flag with the twilio api core messages create command. This displays the raw response from the API. Modify the command as follows:

    twilio api core messages create --from=YOUR_TWILIO_PHONE_NUMBER --to=RECIPIENT_PHONE_NUMBER --body="Your message goes here" --properties
    

    Review the response details to see if any error messages or status information are returned, which can help identify the issue.

  4. Verify phone number format: Ensure that the phone numbers provided in the command are in the correct format. Include the country code without any additional characters (such as parentheses or hyphens). For example, use +447980123456 instead of 07980 123456.

  5. Validate SMS capabilities: Confirm that the Twilio phone number used as the FROM number has SMS capabilities. You can check this in the Twilio Console under the Phone Numbers section.

If you continue to experience issues with sending a text message, I recommend reaching out to Twilio support for further assistance. They can help troubleshoot the issue specific to your Twilio account and provide the necessary guidance to resolve it.


Stay tuned for our next guide where we take command-line communication to the next level.