N8N Routing

HI,

I have requirement here…

I have to make 13 different API call’s based on the input vairable i get from my SET node.

for eg: -

If the SET node Variable is A ----> API call to AA
If the SET node Variable is B ----> API call to BB and so on… API urls are complete different…

I know that i can use Switch Node to achieve the same, but how ever swtich node supports only 4 outputs ( Correct me if my understanding is wrong)… Even with swtich node with IF node i can achive the same, but the workflow becomes bigger…

What i am trying to find out is, is there any better way where i can do routing based on the dynamic value with out complicating the workflow…

Side Note: - IF node doesnt suite my need…

Hey @Sathya_Narayanan,

Do you happen to have a couple of examples of the variables / values you are setting? My first thought would be to use a function node to parse the variable and set up any options you might need for the HTTP Request node assuming the rest of the processing is fairly standard.

1 Like

Hi @Sathya_Narayanan, if it’s just the URLs that are different, you could use a custom JS snippet to set a field for the correct URL (and then read that field in the HTTP Request through an expression). This example uses switch and you can add as many cases as needed.

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "id"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Dummy Value",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "switch (item.id) {\n  case 0:\n    item.url = \"https://hookb.in/eKz7bLl8j6hlwQmmwb0w\"\n    break;\n  case 1:\n    item.url = \"https://postb.in/b/1638441484038-1521006589755\"\n    break;\n  default:\n    item.url = \"https://www.mockbin.org/bin/6bc6262d-b880-4662-86dd-df95a6f49ccf/view\"\n}\n\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "={{$json[\"url\"]}}",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "id",
              "value": "={{$json[\"id\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Dummy Value",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Dummy Value": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FunctionItem": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Edit: Sorry, saw @jon’s post to late :smiley:

My example pretty much does what he suggests (though using the Function Item node instead), setting the URL and an id variable. You can obviously extend with as many fields as needed.

1 Like

Yours is better @MutedJam, You have a nice example workflow :+1: