SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

How to Use API Key and Secret to Authenticate Twilio CLI Commands

 

Objective

Twilio customers who want to securely run CLI commands, particularly in automated environments or scripts, can authenticate without logging in or storing credentials locally. This guide explains how to use your Twilio API Key and Secret with environment variables to authorize Twilio CLI commands in a safe and efficient way.

Product

Twilio CLI

User Account Permission/Role(s) Required 

To follow this process, you’ll need permission to view your Twilio Account SID and create API Keys in the Twilio Console. These actions require appropriate access to account-level settings.

The following roles typically have this level of access:

  • Owner
  • Administrator
  • Developer

Procedure 

Follow these steps to authenticate the Twilio CLI using environment variables:

Step 1: Generate a Twilio API Key and Secret

  1. Sign in to the Twilio Console.
  2. Click Create new API Key.
  3. Choose Standard as key type.
  4. Give your key a recognizable friendly name and click Create API Key.
  5. Copy both the API Key SID and Secret. The secret will be shown only once.

Step 2: Set Environment Variables

Set the following environment variables in your terminal or automation environment.


For macOS/Linux (bash/zsh):

export TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
export TWILIO_API_KEY=SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export TWILIO_API_SECRET=your_api_secret

For Windows PowerShell:

$env:TWILIO_ACCOUNT_SID="ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$env:TWILIO_API_KEY="SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$env:TWILIO_API_SECRET="your_api_secret"

For Windows CMD:

set TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
set TWILIO_API_KEY=SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
set TWILIO_API_SECRET=your_api_secret

Step 3: Run a Twilio CLI Command

Once the environment variables are set, you can run commands without logging in or using a stored profile. For example:

twilio api:core:messages:list

The CLI will use the environment variables to authenticate your request.

If multiple credentials are available, Twilio CLI uses them in this order:

  • Profile specified with the -p flag
  • Environment variables
  • Active CLI profile

Setting TWILIO_REGION is optional unless working outside the default us1.

For best practices, use limited-scope API Keys, rotate them regularly, and avoid exposing credentials in scripts or version control.

Additional Information

Have more questions? Submit a request
Powered by Zendesk