# MQTT configuration from file or env. variable

**URL:** <https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617>\
**Category:** General\
**Created:** [27 May 2019 23:08 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617 "2019-05-27T23:08:04Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [27 May 2019 23:08 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/1 "2019-05-27T23:08:04Z")

</div>

Hello,

I’m trying to figure out a way to configure my MQTT node’s Client ID , UserName, and Password, outside of the flow. My web portal assigns all the above and they are 32 to 64 characters long. The Client Id is used also within some of my subscription topics, and within an array of values. I would like to either use a file, or environmental variables to fill these credentials in when Node-Red Flows start. Don't want got to 4 different spots in my flows fill these in, for I will have 50plus devices. Thanks in advance for your idea’s and input.

---

<div class="post-metadata">

**Author:** ![Tommy200907](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@Tommy200907](https://discourse.nodered.org/u/Tommy200907)\
**Post date:** [28 May 2019 07:13 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/2 "2019-05-28T07:13:35Z")

</div>

The MQTT uses the "configuration node" for setting Client ID, Server, and etc.  
There seems to be no way to set up "configuration node" dynamically in run-time.  
To set up for any number of devices, e.g. 50plus devices, the only workaround that I find is to modify directly the node-red source code(.json file) with a script.  
Would be much better if node-red has a built-in solution.

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 13:35 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/3 "2019-05-28T13:35:38Z")

</div>

Thanks for the response Tommy, I assume you mean put the value in the source code flow file? I'm not concerned about it being dynamically changed, I would like to put these property values somewhere to be read at startup. Like in a file. So I would like the property set for the configuration before runtime. So there is no way for these properties to read an environmental variable , they can only read the property value from the flow code?

---

<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:** [28 May 2019 13:43 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/4 "2019-05-28T13:43:01Z")

</div>

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 configuration values.

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 16:34 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/5 "2019-05-28T16:34:18Z")

</div>

Thanks for your response! I am doing something wrong for it is not working.  
I set my client ID in /etc/environment client\_id = "5cdc3abcb2d28800084f9f37". Cycled power. Confirmed id is and env. variable, with "echo $client\_id" at command line.

had inserted $(client\_id) into the Client ID configuration text window within the MQTT node. It will not connect with env. variable.

What did I miss? Also wanted to note I added ```  
env: process.env under functionGlobalContext in settings.js file after reading env variables usage in node red.

```auto

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 May 2019 18:19 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/6 "2019-05-28T18:19:27Z")

</div>

Are you actually running in the same environment ? Did Node-RED start at boot or from the user environment that you ran the echo from ?

You don’t actually need to do the setting.js part unless you want access from within a function node but it won’t hurt.

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 18:29 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/7 "2019-05-28T18:29:36Z")

</div>

Hello dceejay,  
Node-red starts at boot. So no I did not start it from the same user environment I ran the echo from. Can you help direct me to learn how to insure its env. variable is available within the running Node-Red environment?  
Thanks,

Yes, I just ran command env from within node red with the exec node. The variable is not present there. So is there a place to declare it so its available to all environments?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 May 2019 20:41 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/8 "2019-05-28T20:41:10Z")

</div>

How does node red start from boot? If it is a systemd script is it the standard Pi one for example?

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 20:46 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/9 "2019-05-28T20:46:22Z")

</div>

No its a Moxa IOT gateway. And yes it starts from boot. I have added export client\_id= in bash.bashrc and it still isn't getting loaded into node-red's env variables.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 May 2019 20:54 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/10 "2019-05-28T20:54:49Z")

</div>

It is not clear what questions the No and Yes apply to. The first question was how does it run from boot, to which neither No nor Yes are valid answers. Presumably the No applies to the second question.  
You will probably need to find out how it runs from boot and amend that to include the environment var. If it uses systemd then there are standard ways of appending settings to the script without modifying the original.  
In order to check whether the env var is getting through at all you could use a change node to set msg.payload to the value and see what comes out.

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 21:02 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/11 "2019-05-28T21:02:53Z")

</div>

Thanks, I don't know how it runs from boot, I will try and contact Moxa to get an answer. I have tried both the change node and the exec node (running the env command) and do not get my value back. I only see it from the command prompt via my ssh session.  
I now know I need to dig into how node red starts within this device.

Thanks,

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 May 2019 21:22 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/12 "2019-05-28T21:22:43Z")

</div>

A quick search suggests that the OS is Debian in which case (unless it is an old Debian) it will use systemd. Try  
`sudo systemctl status nodered`  
if that doesn't find a nodered service it may be called something else. You could try  
`systemctl list-unit-files|grep node`  
to find it.

---

<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:** [28 May 2019 21:25 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/13 "2019-05-28T21:25:35Z")

</div>

You can also set environment variables in your settings file:

```auto
process.env.FOO = "hello";

```

As the settings file is node.js code, where it sources those env vars from is up to you - it could read from an external file or similar.

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 21:35 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/14 "2019-05-28T21:35:43Z")

</div>

Yes you are correct it is using systemd. So now I need to figure out where to set the env. variable with it running correct? Thanks!

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 21:36 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/15 "2019-05-28T21:36:58Z")

</div>

Ok So if I set it up in the settings file.. like process.env.Foo ="hello" will I still use $(Foo) in the MQTT config node?

Thanks,

---

<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:** [28 May 2019 21:57 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/16 "2019-05-28T21:57:16Z")

</div>

> [@Chris24918](#):
>
> will I still use $(Foo) in the MQTT config node?

Yes. How you set the env var is independent of how you use it.

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [28 May 2019 22:05 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/17 "2019-05-28T22:05:14Z")

</div>

Thank you , that should work. I'll give it a try and update the results tomorrow..

---

<div class="post-metadata">

**Author:** ![Chris24918](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Chris24918](https://discourse.nodered.org/u/Chris24918)\
**Post date:** [29 May 2019 15:13 UTC](https://discourse.nodered.org/t/mqtt-configuration-from-file-or-env-variable/11617/18 "2019-05-29T15:13:35Z")

</div>

Thanks process.env.client\_id worked. I have a better understanding of environments now.
