Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?


Let's level up our shell hacking skills by utilizing the Twilio API to send a text message directly in bash or zsh. We'll walk through finding an available number, purchasing it, and sending SMSes all without leaving the comfort of the command line.



Start by clicking over to the Twilio console. Either log-in or sign up for a free account (be sure to verify your number), and look for the Account Summary as seen below:


You'll want to replace the 'account_sid' and 'auth_token' variables below with the 'ACCOUNT SID' and 'AUTH TOKEN' variables from the Twilio Console. Click the eyeball icon to expose your auth token then copy/paste it into the shell.



構成済みのアカウントSIDと認証トークンで、Twilio REST APIにプログラム可能な電話番号を照会できます。この番号は、スクリプトからSMSを送信するのに必要です。

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 


この行ではいくつかのことを行っています - ここで見てみましょう。

  1. available_number= in bash or zsh is the syntax for setting a local variable. The backtick ( ` ) characters mean that what the ticks contain will be executed and set to available_number.
  2. curl -X GET "https://api.twilio.com/[omitted] -u "${account_sid}:${auth_token}" Uses the software package cURL and the HTTP verb GET to ask Twilio's available numbers endpoint for an available number. '-u' signifies that we will be providing account credentials by HTTP Basic Auth. The ${variable_name} syntax inside of double quotes substitutes the variable names we set earlier into the command.
  3. "|" A pipe character in the shell means the results of the previous command are passed to the next.
  4. sed -n "/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}" ストリーム操作パッケージsedを使用して、TwilioのXMLレスポンスから最初に見つかった<PhoneNumber>タグを展開します。
  5. && &&記号は後続のコマンドが他のコマンドが終了した直後に実行されることを意味してます。
  6. echo $available_number is a sanity check that we received an output, printing the variable's contents in 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, you are about to spend money to buy a number. Check out our pricing page for more details.


これで購入可能なTwilio電話番号が見つかったので、これに決めましょう。 下記のコマンドで、見つけたばかりの電話番号を購入し、アカウントに追加できます。

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

After executing the above, you'll be the happy owner of a new Twilio-provided phone number (if you aren't, the number may have just been purchased - you can re-run the 'available number' step and try again). Note a few changes here when we use cURL with the IncomingNumber endpoint:

  • 今回はHTTPのPOSTメソッドを使用しています。
  • We're POSTing the PhoneNumber parameter to the IncomingPhoneNumbers endpoint, and substituting the phone number we found in the last step




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

This command will send a POST request to the Messages resource with three required parameters: Body, From, and To. You can explore all of the variables for the Messages endpoint to see how you can send images, or set callback URLs to get updated on message delivery status.

If everything went well, you should now have a buzzing, beeping, or blinking phone. Try sending SMSes to other numbers, or even changing the body to add emojis.


Poking at the Twilio API without leaving the comfort of your favorite shell is a great way to explore the Twilio API and add communication to your command line scripts. You can now search for numbers, purchase numbers, and send SMS messages using built-in *NIX tools and zsh or bash.


Paul Kamp Carlos Villavicencio Kevin Whinnery David Prothero Kat King David Meng
Rate this page:


誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。