Support Forums

[Support Guide] Using environment variables on Netlify correctly

A couple of things @Richard_Fink

  1. You need to await response.json()
  2. Rather than an anonymous async function inside a non-async try
async function getEnvUN() {

  const response = await fetch('/.netlify/functions/envUN');
  const result = await response.json();    // Need to await



However the idea is that you don’t return the process.env.PAYLIVESHOP_USERNAME value to the front-end script. That defeats the purpose of using an environment variable and function. Obviously for testing, that’s a different thing.

1 Like

Screen Shot 2021-11-07 at 12.28.48 AM

I hope that says it… dang, it worked!

Thank you is a bit small. First I can move on, that’s big. Secondly, I can see/figure out just why this worked and the other methods did not. I appreciate the time you have contributed here. I will give the same elsewhere to another.

For me its 12:31am here in San Francisco. If at some time you had a moment to explain each line, and why it did the job, I would put the serious time in to study it. If that’s too much, I understand.

Thank you, very much. And to @hrishikesh the same thanks to you for your returning time and willing help.
Best, Ric

1 Like

Great that you got it working. However, I must tell again that if this was just for demo purposes, then it’s okay, but please don’t return the environment variable to client side using functions.

1 Like

if I never use the function to return an environment to a JS variable but only within the use statement where the value is needed, how is that dangerous. The value is passed but never held/exposed in a variable. It is never in plain text.

Seems the same as one may do using process.env in Node. Its when it is assigned to a JS variable that it becomes exposed and dangerous. Would you agree?

Could you share an example?

From what I know, any value that’s passed in any part of JavaScript code is accessible in the JS debugger using breakpoints.

Hey @fishstix81 did you get to solve that issue?

I am facing the same problem, although I have tried to reference a different env var, instead of NODE_ENV, using MYBUILD_ENV, the new var is always undefined for Gatsby.


Hey there, @mustainer :wave:

It looks like you have two threads open about this question! Let’s continue your conversation in the other thread (linked here: Problems with environment variables - #7) This will help streamline your support conversations.

@hillary ,

sorry, I came up with this answer and the users seems to have a solution, I thought it was easier to ask him directly and then link the solution to my original thread.

But sorry for the inconvinience.