# Using environment variable as password for "MQTT in" node

**URL:** <https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318>\
**Category:** General\
**Created:** [8 May 2020 16:20 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318 "2020-05-08T16:20:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myzhar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myzhar/32/21557_2.png) [@Myzhar](https://discourse.nodered.org/u/Myzhar)\
**Post date:** [8 May 2020 16:20 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318/1 "2020-05-08T16:20:50Z")

</div>

Hi all,  
I'm trying to use an environment variable for connect to a MQTT broker (node "MQTT in") without entering the credential each time I start my node-red server.

I tried both `$(ENV_VAR)` and `${ENV_VAR}` syntaxes, but it seems like if the environment variable is not resolved and the node applies the exact chars entered in the form field as password.

Do you know a solution to this problem?

Thank you  
Walter

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2020 16:28 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318/2 "2020-05-08T16:28:45Z")

</div>

A search of the forum shows this...

> [@MQTT configuration from file or env. variable](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/4):
>
> Hi @Chris24918 you can do this using environment variables. Any node property can be set to something like $(MY\_CLIENT\_ID) and the runtime will substitute the value of the env var MY\_CLIENT\_ID before it gets passed to the node. This cannot be used to insert env vars into part of the value - it has to be the entire value - so $(MY\_CLIENT\_ID)\_FOO will not work. That is exactly what you want if you are deploying the same set of flows to 50+ devices and you need each to insert its own configurati…

> [@Myzhar](#):
>
> but it seems like if the environment variable is not resolved and the node applies the exact chars entered in the form field as password

Are you saying it works OK if the env var IS created (but uses the `$(xxx)` if its not defined)?

In which case - is that a problem?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 May 2020 16:44 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318/3 "2020-05-08T16:44:10Z")

</div>

I would suggest creating a flow with an inject node configured to send the env var you are trying to use. pass that to a debug node so you can verify the env var is actually set within the Node-RED environment.

---

<div class="post-metadata">

**Author:** ![Myzhar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myzhar/32/21557_2.png) [@Myzhar](https://discourse.nodered.org/u/Myzhar)\
**Post date:** [9 May 2020 15:36 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318/4 "2020-05-09T15:36:20Z")

</div>

"MQTT in" node is initialized before the flow starts, so I cannot inject a variable acquired while the flow is running.

I verified that the variable exists because so set it manually in "MQTT in" I use a sub-flow to read it from debug

---

<div class="post-metadata">

**Author:** ![Myzhar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myzhar/32/21557_2.png) [@Myzhar](https://discourse.nodered.org/u/Myzhar)\
**Post date:** [9 May 2020 15:40 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318/5 "2020-05-09T15:40:47Z")

</div>

It works for every form field (for example I can correctly get the IP address and the port of the MQTT broker from env variables), but not for the password field.

It's like if the char '$', '(' and '{' are used as password charts instead of been replaced

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 July 2020 15:40 UTC](https://discourse.nodered.org/t/using-environment-variable-as-password-for-mqtt-in-node/26318/6 "2020-07-08T15:40:54Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
