Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Secure your Twilio Credentials


To secure your Twilio Account SID and Authentication token, store them in environment variables. These variables remain local to your development machine and your app can access them. Using environment variables keeps credentials separate from your code and other locations that could result in unauthorized access to Twilio.

(warning)

Treat credentials like passwords

Never upload your credentials in plain text to a Git repository. Never write your credentials into your application code.


macOS and Linux

macos-and-linux page anchor

To store your credentials on UNIX-like operating systems like macOS and Linux, set environment variables.

  1. Create one environment variable for your account SID and one for your authentication token. Store both in a file titled .env.

    1
    echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > .env
    2
    echo "export TWILIO_AUTH_TOKEN='your_auth_token'" >> .env
  2. Execute the .env as a command in your existing process.

    source ./.env
  3. Add the .env file to your .gitignore file.

    echo ".env" >> .gitignore

To store your credentials in environment variables on Microsoft Windows, you have three options: use the command prompt (cmd.exe), PowerShell, or the Windows UI.

Command promptPowerShellWindows UI

To set these environment variables as permanent settings, use the setx command through the Windows command prompt.

1
setx TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2
setx TWILIO_AUTH_TOKEN=your_auth_token

Most cloud providers provide the means for securing environment variables for your application.


Load credentials from environment variables

load-credentials-from-environment-variables page anchor

After you store your credentials in environment variables, access from your apps using their variable name. To display the proper code for using environment variables, choose your programming language in the following example:

Load credentials from environment variablesLink to code sample: Load credentials from environment variables
1
// Download the Node helper library from twilio.com/docs/node/install
2
// These are your accountSid and authToken from https://www.twilio.com/console
3
// To set up environmental variables, see http://twil.io/secure
4
const accountSid = process.env.TWILIO_ACCOUNT_SID;
5
const authToken = process.env.TWILIO_AUTH_TOKEN;
6
7
const client = require('twilio')(accountSid, authToken);
8
9
// Make API calls here...