Loading credentials fields from environment variables

Is it possible to reference environment variables when creating credentials? For example, if I have Postgres connection inputs with the prefix TP_RO_DB_POSTGRESDB_ which were exported in the startup script, it might look like this:

I’ve tried this but it doesn’t seem to be properly templating the variabes. I just get EADDR errors because “{{ $TP_RO_DB_POSTGRESDB_HOST }}” is just evaluated as a string instead of a variable.

I basically just need this because I’m trying to create a node that connects to a Heroku Postgres instance and that instance’s connection string can change, so hard-coding it in the credentials wouldn’t work.

Any help is appreciated!

Other context info:

  • I used the one-click Heroku deploy from this repo to host on Heroku
  • I’m attempting create credentials to connect to a separate database from another Heroku app
  • n8n is running and available, so no startup issues
  • I confirmed those environment variables were set properly in the startup script

Welcome to the community @mitch!

What you want to do is possible with:

{{ $env.VARIABLE_NAME }}

But only in more current versions (support got added with 0.104.0) and it looks like you probably use version 0.98.0:

So if you upgrade and use the correct syntax it should work.

Hey @jan , thank you again for all the help on this. I do have another issue I’m running into here.

I was able to successfully upgrade to unlock expressions in credentials, however I’m unable to reference any variables. I’m getting the following error:

Maybe something still isn’t configured correctly or a package is missing? I’ve scoured the internet but no luck. Thank you.

That is very strange. Do not understand that error message at all. It is to expect that you can not see the value in the editor-UI as it does not have access to the environment variables. So it should be red, but it should rather display you the message [not found]. If you then execute the node, it runs on the server and will then resolve to the actual value.

Boom! It works. Thanks for all the help @jan

Great to hear! Still confused about the error though.

Have fun!

@jan happy to provide more context. I wasn’t able to find any errors in the browser console or server logs but if there’s any other information you’d like to know lemme know! It was in Chrome 88.0.4324.192 (Official Build) (arm64). I’ll try in Safari later today.

1 Like

Hello,

Does this feature still available on the current version (0.230.3) ?

I’m tring to create a postgres credential using environment variables, but the binding does not work.
I’ve set up a credential with variable (like {{ $env.DB_POSTGRESDB_HOST }}), but the postgres node failed and show {{ $env.DB_POSTGRESDB_HOST }} in ouput.
No problem if I read the variable in a code node.

Thanks

Hi @yadv :wave:

I just pulled version 0.230.3 and while using Postgres, had no issues using an expression in credentials:

(The email address was redacted, but it correctly pulled the right one.)

You might need to add one more environment variable to the n8n section: N8N_BLOCK_ENV_ACCESS_IN_NODE which you can find more information about here: Environment variables reference | n8n Docs

As this thread is quite old, please make a new one if you’re still having trouble, and be sure to fully fill out the template provided when you create a new topic. That helps us help you quickly!