CLI Examples
Create and send an SMS message
twilio api:core:messages:create \
--to +12127363100 \
--from +14155551212 \
--body "Ahoy" \
-o json
Look up a phone number’s carrier and caller name
twilio api:lookups:v1:phone-numbers:fetch \
--phone-number +12127363100 \
--type carrier \
--type caller-name \
-o json
List phone numbers available for purchase by area code
twilio api:core:available-phone-numbers:local:list \
--area-code="209" --country-code US
Purchase an available phone number
twilio api:core:incoming-phone-numbers:create \
--phone-number="+12095551212"
Review your phone numbers with a filter
twilio api:core:incoming-phone-numbers:list \
--phone-number="+1209"
Have your phone number respond to an incoming SMS message
twilio api:core:incoming-phone-numbers:update \
--sid PNfbe7ed1d730e425d0db746c24f7d443c \
--sms-url "https://www.twilio.com/docs/twiml-snippet/twilio-cli"
List the messages your number has received
twilio api:core:messages:list --to="+12095551212"
List the messages your number has received, including the body
twilio api:core:messages:list \
--to="+12095551212" \
--properties="sid,from,body"
Send a Twilio SendGrid email with TSV-format message logs attached
Set the SENDGRID_API_KEY
environment variable with a SendGrid API key.
export SENDGRID_API_KEY=SG.xyz.abc
twilio api:core:messages:list \
-o tsv \
| twilio email:send \
--to 'test@example.com'\
--from 'sender@example.com'\
--subject='Message log'\
--text 'Message log'
Transfer a phone number between accounts
In this example we’ll transfer a phone number from the main account to subaccount 1, then to subaccount 2, then back to the main account. First, set a few environment variables to better identify the various accounts:
export MAIN_ACCOUNT_SID="AC00000000000000000000000000000000"
export SUBACCOUNT_1_SID="AC11111111111111111111111111111111"
export SUBACCOUNT_2_SID="AC22222222222222222222222222222222"
Assuming you are using a main account profile in twilio-cli
and the phone number is owned by the main account, transfer the phone number to subaccount 1:
twilio api:core:incoming-phone-numbers:update \
--sid PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
--target-account-sid $SUBACCOUNT_1_SID
The --account-sid
flag is not required — this is because the account SID defaults to the SID of the active profile.
Now transfer the phone number from subaccount 1 to subaccount 2:
twilio api:core:incoming-phone-numbers:update \
--sid PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
--account-sid $SUBACCOUNT_1_SID \
--target-account-sid $SUBACCOUNT_2_SID
Finally, transfer the phone number from subaccount 2 to the main account:
twilio api:core:incoming-phone-numbers:update \
--sid PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
--account-sid $SUBACCOUNT_2_SID \
--target-account-sid $MAIN_ACCOUNT_SID
In the last two commands the --account-sid
flag is required as the phone number is not owned by the main account.
List most recent debugger logs
twilio debugger:logs:list
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.