0

In my app, I am using two API's that require different credentials. I am storing each in a .env file and read them using process.env. Both .env and .config files are in different directories.

The first config.js:

const dotenv = require('dotenv');
const cfg = {};

dotenv.config({path: '.env'});

cfg.port = process.env.PORT;

cfg.apiKey = process.env.apiKey;
cfg.authDomain = process.env.authDomain;
cfg.databaseURL = process.env.databaseURL;
cfg.projectId = process.env.projectId;
cfg.storageBucket = process.env.storageBucket;
cfg.messagingSenderId = process.env.messagingSenderId;


module.exports = cfg;

The second config.js

const dotenv = require('dotenv');
const cfg = {};

dotenv.config({path: '.env'});

cfg.port = process.env.PORT;

cfg.accountSid = process.env.TWILIO_ACCOUNT_SID;
cfg.authToken = process.env.TWILIO_AUTH_TOKEN;
cfg.twimlAppSid = process.env.TWILIO_TWIML_APP_SID;
cfg.callerId = process.env.TWILIO_CALLER_ID;

module.exports = cfg;

I configured both .env files the same way. But apparently the second config.js is not able to read the credentials such as: TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, etc. Which led me to believe that for the second .env file I have to configure differently than the first one.

How do I load the two sets of credentials into one environment? Or do I have to load them into different environments?

Thanks for your time.

Chen
  • 860
  • 10
  • 32

1 Answers1

0

For your first question, yes, you can load both set of credentials in one environment as the keys are different for each one, you just need to import both config.js in your 'main' code. If what you need to do is to use two set of credentials in the same api, you could both credentials in the same JSON as follow:

{
  firstSet: {
      TWILIO_ACCOUNT_SID: 'value',
      TWILIO_AUTH_TOKEN: 'value',
      TWILIO_TWIML_APP_SID: 'value',
      TWILIO_CALLER_ID: 'value'
  },
  secondSet: {
      TWILIO_ACCOUNT_SID: 'value2',
      TWILIO_AUTH_TOKEN: 'value2',
      TWILIO_TWIML_APP_SID: 'value2',
      TWILIO_CALLER_ID: 'value2'
  }
}

Defined your two different credentials, you could define the logic to use one or another or both credentials in your config.js, depending on your needs, and export that to your application where you could pick the credential from the config to use in the api.

Josiel Faleiros
  • 1,437
  • 2
  • 15
  • 19