last day (15 days later) » 

1:30 AM
-1
Q: How to store sendgrid api key into env variables?

NansI know this kind of question is already made in the StackOverflow but I read all of the answers none of them help me at all. Please answer me on how to solve this. I've installed dotenv and created .env also declared the dotenv.config() in the server.js file but whenever I put my key into the env...

 
Please update your question and include your code. It's hard to diagnose the issue you're having without seeing what you're trying.
 
@Cully I've added necessary details
 
What do you get from console.log(process.env.SENDGRID_API_KEY)?
Also, try dotenv.config({ path: './config/config.env', debug: true }). What does that show?
 
@Cully Still the same error API key does not start with "SG."
@Cully Doing console.log(process.env.SENDGRID_API_KEY) I'm getting undefined.
 
Can you include your directory structure in the question (i.e. the location of all these files, relative to each other)?
Does this change anything: dotenv.config({ path: path.resolve(process.cwd(), 'config/config.env') })?
 
1:30 AM
@Cully The location of every file is correctly assigned because I have few other environment variables in this file they are working in fine the problem is with only this SendGrid API key
 
Have you tried putting quotes around the env variable value? E.g. SENDGRID_API_KEY="SG.3jm....."
 
@Cully nothing is working I've tried the dotenv.config({ path: path.resolve(process.cwd(), 'config/config.env') }) still the same error
@Cully Before asking this question I've tried every possible way I know quotes, export keyword, export keyword with quotes, converting to string using toString() I got the same error
@Cully the only way I'm seeing now is creating a js file add it with export keyword and in .gitignore file add that file to ignore would that work
 
So you have other values in config.env that are showing up in process.env as expected?
 
@Cully Yes as expected if I put this API key directly to the sgMail.setApiKey() using quotes it working perfectly fine all I want to hide is this API key
 
That's not what I'm asking. You said "I have few other environment variables in this file they are working in fine". I wanted to confirm, you have other values in config.env that are being properly loaded into process.env?
 
1:30 AM
@Cully yes is loaded perfectly
 
Can you edit your question and include the full contents of config.env? Obviously, you don't need to include any secrets.
 
@Cully here I edited the env file check this
 
What do you get when you do console.log(process.env.JWT_RESET_PASSWORD, process.env.SENDGRID_API_KEY)?
 
@Cully Both shows undefined
 
Then the rest of your environment variables are not being loaded from config.env either. What made you think they were?
And where are you putting your console.log? Right after the dotenv.config call? Also, try wrapping your dotenv.config call in console.log, what is the result?
 
1:30 AM
@Cully Well the PORT and MONGO_URI variable is reading when I put the API key directly to send grid the ClIENT_URL, FROM_EMAIL, JWT_ACCOUNT_ACTIVATION all are reading perfectly I've assigned the same way process.env.VARIABLE
 
What do you get from console.log(process.env.JWT_ACCOUNT_ACTIVATION)?
 
@Cully Every env variable inside the console log showing me undefined the even the PORT and MONGO_URI
 
Then they aren't being loaded.
Where are you putting your console.log? Right after the dotenv.config call or somewhere else? Also, try wrapping your dotenv.config call in console.log, what is the result?
 
@Cully when I console log from the controller file its showing me undefined but In server.js file it returning me every variable value
 
Then auth.controller.js isn't being loaded in the same process as server.js. What framework are you using?
Or auth.controller.js is processed before your dotenv.config call.
 
1:30 AM
@Cully Thanks Cully because of you I finally figure out the problem. The Problem was inside the server.js file I'm assigning the routes file address before dotenv.config(). Thank you so much.
 
Next time include more code. It would have been easy to spot if you included more of your server.js file.
 
@Cully Yeah you're right if I added the full server.js file I will get the answer much faster.
 

last day (15 days later) »