Five Twilio CLI features you should know about

August 13, 2019
Written by

Copy of Generic Blog Header 4.png

The new Twilio CLI beta is hot off the press. It was released at our yearly SIGNAL Conference only a few days ago. The CLI allows developers to run Twilio tasks right in their terminal without the need to visit the Twilio console in their browser.

The tool enables scripting and execution of everyday tasks such as buying and setting up phone numbers, creating managing sync services, sending emails, and much more!

Command: `twilio phone-numbers:buy:mobile --country-code DE` showing available phone numbers

Command in the screenshot: twilio phone-numbers:buy:mobile --country-code DE.

I asked the team that worked on this new part of the Twilio infrastructure what their favorite CLI features are; let me tell you what they said!

Command discovery with auto-completion and --help

The Twilio command-line interface ships with hundreds of commands to navigate and control Twilio’s services. You must find a way to browse and know about all these commands.

That's why it’s recommended to enable auto-completion.

Result of command: `twilio + [TAB]` showing 772 auto-complete options

Command in the screenshot: twilio + [TAB].

If you’re on bash or zsh run twilio autocomplete to get setup instructions for the auto-completion. My environment is running on zsh and is shown in the screenshots. The bash auto-completion looks a little bit different.

Instructions to set up auto-complete after ran `twilio autocomplete`

Command in the screenshot: twilio autocomplete.

Auto-completion is the best way to discover new commands, and you’ll find the commands you’re looking for by just “tabbing around”, I promise!

Completion for `twilio api:wireless:v1:` after pressing <TAB>

Command in the screenshot: twilio api:wireless:v1: + <TAB>.

After discovering the commands that are important for you, you still need the information on how to use them. The development team took much care to support the common --help flag for every command to find more information about arguments and supported flags.

Help instructions afte running `twilio email:set --help`

Command in the screenshot: twilio email:set --help.

Auto-completion, in combination with the --help flag, lets you find all the information you need to be productive in the Twilio CLI.

Configurable command output options

CLI output can be overwhelming at times. Printing a table full of information into a small terminal window makes things messy. Luckily, you can adjust the output of commands using the --properties flag.

Reduced command output after ran command `twilio phone-numbers:list --properties=friendlyName` including a properties flag

Commands in the screenshot: twilio phone-numbers:list and twilio phone-numbers:list --properties=friendlyName.

The properties flag is great to make the output more human-readable and adjust it to your needs.

The human-readable output is not the only thing you need, though. If you want to write complex scripts, you also need a way to parse the results of the command execution.

To change the output to a completely different format, you can use the -o flag to change it to machine-readable JSON or TSV.

JSON output with `twilio phone-numbers:list -o=json` and the -o flag

Command in the screenshot: twilio phone-numbers:list -o=json.

The JSON output, paired with tools like jq, enables you to script complete infrastructure setups.

(Currently, I’m working on another article that sets up an SMS group chat with one shell script – I’ll keep you posted 😉)

Pipe attachment data into the email:send command

Using the CLI you can start outbound communication right from your terminal. For example, twilio email:send enables you to send an email if an event like a failing build process occurred in your system. To use it, define SENDGRID_API_KEY in your environment variables and you’re good to go.

What is cool about this command is that you can define email attachments via the --attachment flag. But that’s not all! Additionally, you can pipe data right into the command. This trick can be convenient when you want to send emails that include log files or command outputs.

Example command to pipe data in `twilio email:send`

Commands in the screenshot: twilio email:send --from=no-reply@... --to=stefanjudis@... --subject=”test” --text=”test” --attachment=/Users/you/... and px aux | twilio email:send --from=no-reply@... --to=stefanjudis@... --subject=”test” --text=”test”.

Local webhook URLs are magically accessible

Local development and its setup was always the most tedious part when working with Twilio.

What you had to do:

  • open a tunnel to your local environment using ngrok (or other tools) to react to webhooks for incoming phone calls or messages
  • log in to the Twilio console
  • find the number and configure the new webhook URLs
  • repeat the above every time you restart the tunnel command and got a new URL

What if I would tell you that all these steps are now redundant with the new CLI?!

`twilio phone-numbers:update +491…. --sms-url=https://localhost:8000` automatically opens an available tunnel

Command in the screenshot: twilio phone-numbers:update +491…. --sms-url=https://localhost:8000.

Use the twilio phone-numbers:update command and point it to your local environment. This command will start a local tunnel and configure your number automatically. No more additional clicks needed. Does that feel like magic? It surely does to me! 🎉

A powerful plugin system to extend the functionality

The last thing that I’m very excited about is the ability to extend the Twilio CLI with plugins. Using twilio plugins:install you can enrich the general CLI functionality with more commands.

Completion of `twilio plugins + <TAB>` showing install, uninstall and update functionality

Command in the screenshot: twilio plugins + <TAB>.

And what’s great about this extensibility, is that Twilions have already started extending our CLI! Here are some of my favourite plugins.🎉

Watch all your notifications right in the CLI (@twilio-labs/plugin-watch)

Status updates are essential when developing with Twilio. Using the watch plugin, you can have constant updates of what’s going on and see when your API/webhook calls were answered or errored.

Result of running `twilio watch` showing all Twilio activity in the terminal

Command in the screenshot: twilio watch.

This plugin is only one twilio plugins:install @twilio-labs/plugin-watch command away. Enjoy!

Deploy your serverless runtime with a few commands (@twilio-labs/plugin-serverless)

The Signal conference was not only the public release of the CLI, but also the release of the new Twilio Serverless Runtime API. The Runtime provides you with an easy to use API to manage your serverless functions and assets.

The Serverless Runtime CLI plugin lets you create new functions, test these locally, and deploy these to the Twilio Runtime. You can install it with the command twilio plugins:install @twilio-labs/plugin-serverless.

Side note: when you’ve installed a new plugin make sure to run twilio autocomplete again to add the new commands to the list of the completion commands.

`twilio serverless` commands shown with completion: activate, deploy, init, list, new and start

Command in the screenshot: twilio serverless: + <TAB>.

Afterwards, you can set up Twilio runtime services, create functions, connect these with numbers, and deploy it all together with only a few commands right from your terminal!

If you’re looking for more CLI plugins or want to contribute, have a look at our new Open Source area on twilio.com or Twilio Labs on GitHub.

Provide feedback

Are there features that you’d like to see? Is there something missing? Do you have an idea for another plugin? We’d love to hear your feedback!

Result of `twilio feedback` - bit.ly/twilio-cli-feedback

Command in the screenshot: twilio feedback.

But how does the feedback process work? Glad you asked… maybe you want to try the twilio feedback command. 😉

 

Tell me how you use the new Twilio CLI! You can reach me under the following channels.