Share some config among Function nodes

I’d like to share common config (json) among some Function nodes triggered by separated webhooks within the same workflow.
Is there a way to achieve this, apart from using env vars or external files?

i.e. I’ve tried adding a Set node named config, filled it with some parameters and configured the Function to access them as $node["config"].parameter[0] but it works only if the Set node gets executed.

Hey @davidecavestro! Welcome to the community!

I love the little challenge that you gave us here. The first thing that I thought of was to put it into an environment variable (Oops! Strike 1!). Next, I figures that it could be stored in a file on the host (Dang! Strike 2!). Apparently, I was going to have to get pretty creative to make this work!

So I came up with the following workflow:

Essentially, each of the webhooks fires when a specific event occurs. Next, the corresponding set node creates a webhook key with the webhook number on it. The config set node holds all of your configuration settings. The Switch node then checks the value of the webhook key and sends the output (along with all of the configuration data) out the respective output.

Here is the entire workflow:

{
  "nodes": [
    {
      "parameters": {
        "value1": "={{$node[\"Config\"].json[\"webhook\"]}}",
        "rules": {
          "rules": [
            {
              "operation": "notEqual",
              "output": "={{$node[\"Config\"].json[\"webhook\"]}}"
            }
          ]
        }
      },
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        1170,
        370
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "propertyName1",
              "value": 1
            }
          ],
          "string": [
            {
              "name": "propertyName2",
              "value": "Two"
            }
          ]
        },
        "options": {}
      },
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        990,
        370
      ]
    },
    {
      "parameters": {
        "path": "155c361d-eb0a-47b2-8a45-f1b1513f76dc",
        "options": {}
      },
      "name": "Webhook 0",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        590,
        160
      ],
      "webhookId": "155c361d-eb0a-47b2-8a45-f1b1513f76dc"
    },
    {
      "parameters": {
        "path": "155c361d-eb0a-47b2-8a45-f1b1513f76dc",
        "options": {}
      },
      "name": "Webhook 1",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        590,
        310
      ],
      "webhookId": "155c361d-eb0a-47b2-8a45-f1b1513f76dc"
    },
    {
      "parameters": {
        "path": "155c361d-eb0a-47b2-8a45-f1b1513f76dc",
        "options": {}
      },
      "name": "Webhook 2",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        590,
        460
      ],
      "webhookId": "155c361d-eb0a-47b2-8a45-f1b1513f76dc"
    },
    {
      "parameters": {
        "path": "155c361d-eb0a-47b2-8a45-f1b1513f76dc",
        "options": {}
      },
      "name": "Webhook 3",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        590,
        610
      ],
      "webhookId": "155c361d-eb0a-47b2-8a45-f1b1513f76dc"
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "webhook"
            }
          ]
        },
        "options": {}
      },
      "name": "Set 0",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        160
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "webhook",
              "value": 1
            }
          ]
        },
        "options": {}
      },
      "name": "Set 1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        310
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "webhook",
              "value": 2
            }
          ]
        },
        "options": {}
      },
      "name": "Set 2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        460
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "webhook",
              "value": 3
            }
          ]
        },
        "options": {}
      },
      "name": "Set 3",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        610
      ]
    }
  ],
  "connections": {
    "Config": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook 0": {
      "main": [
        [
          {
            "node": "Set 0",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook 1": {
      "main": [
        [
          {
            "node": "Set 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook 2": {
      "main": [
        [
          {
            "node": "Set 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook 3": {
      "main": [
        [
          {
            "node": "Set 3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 0": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 1": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 2": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 3": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hopefully that helps.

2 Likes

Thank you @Tephlon, your solution just works.

But I must admit I was looking for a way to avoid these kind of switches, just for readability purposes.

IMHO n8n is all about simplicity, and it would be great if someday we had a way to share some config, possibly managed in a similar fashion to credentials (i.e. see NiFi parameters).

Hey @davidecavestro.

So, if I am understanding you correctly, you are looking for something that would be akin to global variables or a global Set node where you could set different parameters which could be referenced outside of the workflow.

I know you referenced this in your initial post, but using a Set node such as this:
image
with the following workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "globalValue1",
              "value": "Global1"
            },
            {
              "name": "globalValue2",
              "value": "Global2"
            }
          ]
        },
        "options": {}
      },
      "name": "Global Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "value": "={{$node[\"Global Config\"].parameter[\"values\"][\"string\"][0][\"value\"]}}"
      },
      "name": "Crypto",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        440,
        480
      ]
    },
    {
      "parameters": {
        "value": "={{$node[\"Global Config\"].parameter[\"values\"][\"string\"][1][\"value\"]}}"
      },
      "name": "Crypto1",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        590,
        480
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Global Config",
            "type": "main",
            "index": 0
          },
          {
            "node": "Crypto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crypto": {
      "main": [
        [
          {
            "node": "Crypto1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

by directly referencing the values set in the Global Config node.

I know that this is not the answer you are looking for but it does provide the required results with the system as it stands today.

The only other way to do this would be for n8n to be rewritten at a base level with another section, as you put it, like the credentials section. If that is the case, my recommendation to you would be to then submit an enhancement request so that the developers could put it into the work and prioritization queue.

1 Like

Thankyou @Tephlon, I’ll try to contribute.
OTOH your last solution remind me an aspect of n8n still obscure to me: is there any guarantee that the Crypt node gets executed after Global Config one? If so, is that documented somewhere?

Hey @davidecavestro,

Unfortunately, not that I am aware of. In fact, without knowing a lot more detail about exactly the order that nodes are executed when the workflow is split like this, my understanding is the they are actually executed approximately at the same time since they will receive the signal to execute at the same time. So, thinking that through, it would probably be worth putting in a “pause” function node for a second or two between the Start and the Crypt node.

By engaging others in the community, you already have and it is a great start!