Menu

Expand
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?

Twilio CLI Examples

Create and send an SMS message

twilio api:core:messages:create \
  --to +12127363100 \
  --from +14155551212 \
  --body "Ahoy" \
  -o json

Lookup 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 Twilio 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 master account to subaccount1, then to subaccount2, then back to the master account. First, set a few environment variables to better identify the various accounts:

export MASTER_ACCOUNT_SID="AC00000000000000000000000000000000"
export SUBACCOUNT_1_SID="AC11111111111111111111111111111111"
export SUBACCOUNT_2_SID="AC22222222222222222222222222222222"

Assuming you are using a master account profile in the CLI and the phone number is owned by the master account, transfer the phone number to subaccount1:

twilio api:core:incoming-phone-numbers:update \
  --sid PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
  --target-account-sid $SUBACCOUNT_1_SID

Note in the above command that the --account-sid flag is not required. This is because the account SID is defaulted to the CLI profile account SID.

Now transfer the phone number from subaccount1 to subaccount2:

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 subaccount2 to the master account:

twilio api:core:incoming-phone-numbers:update \
  --sid PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
  --account-sid $SUBACCOUNT_2_SID
  --target-account-sid $MASTER_ACCOUNT_SID

Note that in the above two commands the --account-sid flag is required since the phone number is not owned by the master account.

Rate this page:

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 browsing the Twilio tag on Stack Overflow.