mycroft16
mycroft16

Reputation: 825

twilio cli not reading .env file in node

I have created a .env file (should it have a name or just the extension?) In it I have placed my credentials:

TWILIO_ACCOUNT_SID=AC---------------------------
TWILIO_API_KEY=SK------------------------------
TWILIO_API_SECRET=-------------

I have installed the dotenv package from npm and this code works perfectly:

const dotenv = require('dotenv');
const result = dotenv.config()

if (result.error) {
  throw result.error
}

console.log(result.parsed)

However, when I try and run the cli command: twilio phone-numbers:update "+xxxxxxxxxx" --sms-url="http://localhost:1337/sms"

It tells me:

Alternatively, twilio-cli can use credentials stored in environment variables:

# OPTION 1 (recommended)
TWILIO_ACCOUNT_SID = your Account SID from twil.io/console
TWILIO_API_KEY = an API Key created at twil.io/get-api-key
TWILIO_API_SECRET = the secret for the API Key

Trying to use the cli as recommended in the tutorials to update the webhook for my number and run ngrok automatically. Any ideas why the cli wouldn't be seeing the values in the .env when they are clearly accessible in a script via dotenv?

Upvotes: 0

Views: 764

Answers (1)

Yogesh Khatri
Yogesh Khatri

Reputation: 1210

.env file is not standard to create environment variables. In your script you used dotenv package which reads this particular file and sets environment variables for you.

for your twilio cli, you have to set those environment variables explicitly. There are multiple ways to do that e.g.

  1. inline export and then run your command

    export TWILIO_ACCOUNT_SID=-- TWILIO_API_KEY=-- TWILIO_API_SECRET=-- twilio phone-numbers:update "+xxxxxxxxxx" --sms-url="http://localhost:1337/sms"
    
  2. set it in your shell file like .bashrc so that they are always available

  3. use your current .env file like this

    export $(cat .env | xargs) && twilio phone-numbers:update "+xxxxxxxxxx" --sms-url="http://localhost:1337/sms"
    

Upvotes: 2

Related Questions