Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

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


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(link takes you to an external page)). 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

set-your-twilio-credentials-in-your-c-shell page anchor

Click to the Twilio console(link takes you to an external page) 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

find-a-twilio-number-with-curl-in-the-c-shell page anchor

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(link takes you to an external page) 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(link takes you to an external page) . 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(link takes you to an external page) 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(link takes you to an external page).)

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

purchase-a-twilio-phone-number-in-the-c-shell page anchor

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

send-an-sms-with-twilio-in-c-shell page anchor

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(link takes you to an external page) or sending SMSes to other numbers.


Twilio Rings Phone Bells in the C Shell

twilio-rings-phone-bells-in-the-c-shell page anchor

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:


Rate this page: