How to Send an SMS from a Shell Script using cURL

March 23, 2021
Written by
Twilion

header - How to Send an SMS from a Shell Script using cURL

Twilio is all about powering communication and doing it conveniently and fast in any language.  

In this tutorial you’ll learn how to deliver a quick message or notification via SMS directly from your shell script. You’ll use the popular cURL command-line utility to send an SMS in an insanely fast manner. Ready? Let's get started!

Tutorial requirements

Buy a Twilio phone number

If you haven't done so already, purchase a Twilio phone number to send SMS from.

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” page, select your country and check SMS 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.

Buy a phone number

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.

Configure Twilio credentials and phone numbers

To be able to send an SMS via Twilio, the script will need to have access to your Twilio account credentials to authenticate. Likewise, to send an SMS you need to have two phone numbers, the number of the sender, which is the number you bought in the previous section, and the number of the recipient, which can be your personal number.

The most secure way to define these configuration values is to set environment variables for them.

The Twilio credentials that you need are your “Account SID” and your “Auth Token”. You can find both on the dashboard of the Twilio Console:

Twilio account SID and auth token

In your terminal, define the following environment variables:

export TWILIO_ACCOUNT_SID=xxxxxxxxx
export TWILIO_AUTH_TOKEN=xxxxxxxxx
export TWILIO_NUMBER=xxxxxxxxx
export TO_NUMBER=xxxxxxxxx

If you are following this tutorial on a Windows computer, use set instead of export to define your environment variables in the command prompt. For the phone numbers, use the E.164 format, which includes a plus sign and the country code.

If you want to learn more about environment variables, check out our how to set environment variables tutorial.

Send an SMS with cURL

With the four environment variables set as shown in the previous section, you can now send an SMS with a single-line command. If you are following this tutorial on a Unix or MacOS computer, here is how to do it:

curl -X POST -d "Body=Hi there, this is a test message from cURL" -d "From=$TWILIO_NUMBER" -d "To=$TO_NUMBER" "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages" -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

For Windows, the syntax to insert variables in the command is slightly different, so use this version instead:

curl -X POST -d "Body=Hi there, this is a test message from cURL" -d "From=%TWILIO_NUMBER%" -d "To=%TO_NUMBER%" "https://api.twilio.com/2010-04-01/Accounts/%TWILIO_ACCOUNT_SID%/Messages" -u "%TWILIO_ACCOUNT_SID%:%TWILIO_AUTH_TOKEN%"

In just a moment, you will receive the SMS in your mobile phone!

Now you can insert this line in any shell scripts where you need to send notifications. You can also insert the phone number variables in your script, but be very careful with your Twilio credentials, as you don’t want them to end up in a public git repository. Check out the Store your Twilio Credentials Securely article in the documentation for more information on this.

If you are using a free Twilio account, keep in mind that the number that you use as a recipient must be verified before it can receive SMS. This requirement does not apply if you are using a paid account.

What's next for sending SMS with Twilio?

Congratulations on learning the one-liner that sends a quick SMS! Do you want to learn other original ways to send an SMS with Twilio? Here are some that you may like:

Miguel Grinberg is a Python Developer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool Python project you’d like to share on this blog!