Setting Credentials Programmatically

n8n Jul 4, 2021

A user in the n8n Discord community (visit https://discord.gg/zrap422Hnp if you would like to join this server) asked about setting up credentials dynamically, specifically in the SSH node:

can I use the SSH node for a variable host? for example, I have a Node that returns an IP address to which I want to connect. however, the SSH node requires SSH credentials with the host in there. can I create new SSH credentials on command and use those as part of a workflow?

The key to doing this is to have a node prior to the node you want to set the credentials in advance and pull the information from the previous node.

A quick side note that this will only work with regular nodes. You cannot (at least at the time of this writing) do this with trigger nodes as there are no nodes that are executed prior to the trigger.

How To Do It

Let's say you have the following workflow:

Sample Workflow

Inside the SSH node, you have the following configuration:

SSH Parameters

The Test SSH credential looks like this:

SSH Credentials

You will notice that the value of the Host is an expression because it is in red:

Hostname Expression

The value of the expression is {{$node["Set Hostname"].json["hostname"]}} which is 192.168.1.1

Set Hostname Parameters

If I execute the Set Hostname node:

Executed Set Hostname Node

and now go back into the SSH credentials, you will notice that the value of the Host is now set correctly based on the value of the Set Hostname:

Host Value

If you want to play with it, here is the entire workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {},
      "name": "SSH",
      "type": "n8n-nodes-base.ssh",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "credentials": {
        "sshPassword": "Test SSH"
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "hostname",
              "value": "192.168.1.1"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Hostname",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Hostname",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Hostname": {
      "main": [
        [
          {
            "node": "SSH",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Sample Node Code

Because credentials are not copied in workflows, you will need to create it yourself but the key is setting the hostname to the expression mentioned above.

Hopefully you find that to be useful!

Tags

Tephlon

A passionate tech nerd for over 25 years. I consider myself to be a generalist so I know a little about a lot. My latest obsessions are Raspberry Pi computers and n8n automation.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.