How to Use the C Shell to Send an SMS from a New Twilio Number

November 22, 2016
Written by
Paul Kamp
Twilion
Reviewed by
Kat King
Twilion

cshell-sms

Today we'll make Bill jump for Joy by purchasing a new Twilio number and sending an SMS directly from csh (or, more likely, tcsh).  Here you'll learn how to find an available number, purchase a number, and send an SMS with Twilio without ever leaving your C Shell.

Set Your Twilio Credentials in your C Shell

Click to the Twilio console to get started.  Log-in to your existing account or sign up for a free account (be sure to verify your number, you'll need it later).  When that is done, look for the Account Summary as seen below:

Account Credentials

 

Using that information, substitute the account_sid and auth_token variables below with the 'ACCOUNT SID' and 'AUTH TOKEN' variables from the Twilio Console.  (Click on the eyeball icon to expose your AUTH TOKEN).

Run these in your shell:

set account_sid="ACXXXXXXXXXXXXXXXXXXXXX"
set auth_token="your_auth_token"

Find a Twilio Number with cURL in the C Shell

Now that our Account SID and auth token are configured, we can query Twilio's REST API to find available phone numbers.  We will need to find one (and purchase it) to send an SMS from our script.

set available_number=`curl -X GET \
"https://api.twilio.com/2010-04-01/Accounts/${account_sid}/AvailablePhoneNumbers/US/Local" \
-u "${account_sid}:${auth_token}" | \
sed -n "/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}"`
echo $available_number

If you paste that above command and hit return, available_number will be set to the first number Twilio gives you.

Let's explore what's happening in this busy line:

  1. set available_number= is the syntax for setting a local variable.  Everything contained in the backtick ( ` ) will be executed and set to available_number.
  2. curl -X GET "https://api.twilio.com/[omitted] -u "${account_sid}:${auth_token}" We use the software package cURL and the HTTP verb GET to ask Twilio's available numbers endpoint for a free number.  '-u' is the flag to provide account credentials by HTTP Basic Auth. A ${variable_name} inside of double quotes substitutes the variable names we set earlier into this command.
  3. "|" A pipe character in the shell means the results of the previous command are passed to the next command (in this case, we pass Twilio's response to sed).
  4. sed -n "/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}" Uses the stream manipulating package sed to extract just the first occurrence of the <PhoneNumber> tags from Twilio's XML response
  5. && allows us to chain our commands together in the shell
  6. echo $available_number prints the variable available_number to the shell.

(For advanced users: note that the 'US' after AvailablePhoneNumbers in the URI is the ISO Country Code.)

Note: If you're not in a trial account, it will cost money to purchase this number.  Check out our pricing page for more details.

Purchase a Twilio Phone Number in the C Shell

Twilio just returned an available number to us - let's put a ring on it, okay?  

Run the following to purchase the number and add it to your account:

curl -X POST -F "PhoneNumber=${available_number}" \
"https://api.twilio.com/2010-04-01/Accounts/${account_sid}/IncomingPhoneNumbers" \
-u "${account_sid}:${auth_token}"

Hit return and you should be the happy owner of a new Twilio phone number (if you aren't, it's possible the number was already purchased - re-run the 'available number' step and try again).  

If you're eagle-eyed, you'll probably notice:

  • We're now using the HTTP method POST
  • We're POSTing the PhoneNumber parameter to the IncomingPhoneNumbers endpoint, and substituting the phone number we found in the last step

If everything went okay you now own available_number and are ready for the big payoff.

Send an SMS with Twilio in C Shell

Change the your_number variable below to a verified Twilio number in your account and run the following line:

set your_number="+15555555555"
curl -X POST -F "Body=Hi there, your new phone number is working." \
-F "From=${available_number}" -F "To={$your_number}" \
"https://api.twilio.com/2010-04-01/Accounts/${account_sid}/Messages" \
-u "${account_sid}:${auth_token}"

This command sends a POST request to the Messages resource with the three required parameters: Body, From, and To. You should explore all of the legal parameters for the Messages endpoint and see how to send images or set callback URLs for updates on message delivery status, among many options.

If everything went well, though, your phone should be playing a ringtone or vibrating off your desk.  Try changing the body to add emojis or sending SMSes to other numbers.

Twilio Rings Phone Bells in the C Shell

Hitting useful Twilio endpoints without leaving csh should speed up your prototyping time immensely.  Command line hacking is a great way to explore the Twilio API and add communication capabilities to your shell scripts.  With your shell and this article, you can now search for numbers, purchase numbers, and send SMS messages without leaving csh - just use what *NIX gives you.

Further study: