How to access secrets from nodejs server?

Hi!

Thank you for building fly.io, really like it.

Little stuck while working on secrets though. I set my secret using flyctl secrets set NAME=VALUE.
flyctl secrets list shows NAME as well.
In my nodejs, I use process.env.NAME. That works great locally (when setting my key using export in my terminal), but doesn’t work when deployed. Logging process.env on the deployed server, shows that NAME is not available.

How should I access NAME from my nodejs typescript server, if process.env.NAME is not the way to go?

Thanks!

That seems correct.

Did you set the secret with an uppercase name? We’ve seen Node not pick secrets up with lowercase names before. flyctl secrets list should show the name in the case you used.

I’ve put together a minimal test here to test this:

const express=require("express");
const app=express();

value=process.env.NAME;

app.get("/", (req,res) => {
    res.status(200).send(value);
})

app.listen(8080,() => "Server is listening")

done

fly secrets set NAME="heya"

then
fly deploy

then
fly open

and I can see the env var value being displayed.

So we probably want to dig down on your code in case your env is being overwritten.

Followup question: Are you getting an error like:

Property 'NAME' does not exist on type 'ProcessEnv'

With your typescript code that is…

Thank you for the fast responses!

I still cannot seem to get the job done :thinking:
Looks like I can run the example that you provide. That works!

I used the env name ‘google’, which didn’t work. Unsetting it works, setting it will give me a ‘no change detected in secrets’.
Using the env name ‘NAME’, same value, worked.

Not sure what’s going wrong here!

Hmm, very strange behaviour (or I’m missing something trivial).
In the following code snippet, removing console.log will result in branching into the if-statement. Using console.log results in not branching into the if-statement (which makes sense).

const credentials = process.env.NAME;
console.log(`name: ${process.env.NAME}`);
if (!credentials) {

Does this make any sense to you?

Try dumping the entire env object to the console or response body and making sure it’s what you expect. Something like this:

var env = process.env;

Object.keys(env).forEach(function(key) {
  console.log(`${key}="${env[key]}"`);
});

I unset your google secret, you had both lowercase and uppercase GOOGLE set. Can you see if that made it work?

Thanks for deleting those two secrets, I was able to set a new google value now without an issue. Not sure what went wrong there.

I tried printing the process.env object earlier and it didn’t show either NAME and google (it did include a bunch of other variables though). Looping over the keys and printing those results in the same (not seeing google or NAME).
Printing it explicitly using console.log(process.env.NAME), does show a value. Looks like the ‘google’ secret is still not working correctly. The DIGEST is the same for all 3 (NAME, google, GOOGLE). I am not sure what would be the difference between NAME and the other two if the DIGEST is the same.
While I can definitely make my server work with the NAME variable, I will do some more tests tomorrow to see whether other variables do work (might be useful for you as well haha).

Thanks a lot for the help by the way! :slight_smile: