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.
Treat credentials like passwords
Never upload your credentials in plain text to a Git repository. Never write your credentials into your application code.
To store your credentials on UNIX-like operating systems like macOS and Linux, set environment variables.
-
Create one environment variable for your account SID and one for your authentication token. Store both in a file titled
.env.1echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > .env2echo "export TWILIO_AUTH_TOKEN='your_auth_token'" >> .env -
Execute the
.envas a command in your existing process.source ./.env -
Add the
.envfile to your.gitignorefile.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.
To set these environment variables as permanent settings, use the setx command through the Windows command prompt.
1setx TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2setx TWILIO_AUTH_TOKEN=your_auth_token
Most cloud providers provide the means for securing environment variables for your application.
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:
1// Download the Node helper library from twilio.com/docs/node/install2// These are your accountSid and authToken from https://www.twilio.com/console3// To set up environmental variables, see http://twil.io/secure4const accountSid = process.env.TWILIO_ACCOUNT_SID;5const authToken = process.env.TWILIO_AUTH_TOKEN;67const client = require('twilio')(accountSid, authToken);89// Make API calls here...