Loop the url with a different parameter

Hello guys, I’m starting now with n8n, and I’m not able to find a solution, the api where I’m getting the data passes an ID in the url as I show in the images below, but when I put it as a parameter it doesn’t identify, so I had to put it straight in the url, the problem is that the loop returns success only 1 time. I would like your help to improve the flow, returns success when looping the url.
I left the images here and I will also leave a copy of my stream. thanks.

essa é a url de exembro meusite.com/api/contacts/:id

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {}
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        280,
        100
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "url_principal",
              "value": "https://meusite.com.br/"
            }
          ]
        },
        "options": {}
      },
      "name": "url_base_api",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        510,
        110
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "={{$json[\"url_principal\"]}}{{$json[\"url_buscar_todos_clientes\"]}}",
        "options": {},
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/x-www-form-urlencoded"
            }
          ]
        }
      },
      "name": "Buscar_todos_clientes",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        930,
        110
      ],
      "notesInFlow": true,
      "credentials": {
        "httpHeaderAuth": {
          "id": "8",
          "name": "token_curso_perfex"
        }
      },
      "continueOnFail": true,
      "notes": "buscar todos clientes perfex, curso de perfex crm"
    },
    {
      "parameters": {
        "functionCode": "// Based on:\n// https://docs.n8n.io/reference/javascript-code-snippets.html#_1-create-multiple-items-from-a-single-item\nreturn items[0].json.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Dividir_clientes",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1230,
        100
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "url_buscar_todos_clientes",
              "value": "api/customers"
            },
            {
              "name": "Adicionar_Cliente",
              "value": "api/customers"
            },
            {
              "name": "Excluir_Cliente",
              "value": "api/delete/customers/"
            },
            {
              "name": "Ver_todos_clientes",
              "value": "api/customers"
            },
            {
              "name": "Atualizar_cliente",
              "value": "api/customers/"
            },
            {
              "name": "Buscar_informacao_cliente",
              "value": "api/customers/search/"
            }
          ]
        },
        "options": {}
      },
      "name": "url_clientes",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        700,
        110
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "={{$node[\"url_base_api\"].parameter[\"values\"][\"string\"][0][\"value\"]}}{{$node[\"url_clientes\"].json[\"url_buscar_todos_clientes\"]}}/{{$json[\"id_clinete\"]}}",
        "options": {
          "useQueryString": false
        }
      },
      "name": "Buscar_cliente_id",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1720,
        110
      ],
      "notesInFlow": true,
      "executeOnce": false,
      "credentials": {
        "httpHeaderAuth": {
          "id": "8",
          "name": "token_curso_perfex"
        }
      },
      "continueOnFail": true,
      "notes": "Buscar o cliente pelo ID"
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "id_clinete",
              "value": "={{$json[\"userid\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "separar_id_cliente",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1490,
        100
      ]
    }
  ],
  "connections": {
    "url_base_api": {
      "main": [
        [
          {
            "node": "url_clientes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar_todos_clientes": {
      "main": [
        [
          {
            "node": "Dividir_clientes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dividir_clientes": {
      "main": [
        [
          {
            "node": "separar_id_cliente",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "url_clientes": {
      "main": [
        [
          {
            "node": "Buscar_todos_clientes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "separar_id_cliente": {
      "main": [
        [
          {
            "node": "Buscar_cliente_id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @egnine_team, looping through all input items would be the behavior.

However, in your Buscar_cliente_id node you are referencing other items using the {{$node["url_clientes"].json["url_buscar_todos_clientes"] expression which would only work for the first item of your url_clientes node. If you replace this expression with something like $items("url_clientes")[0].json["url_buscar_todos_clientes"] this would probably work as expected.

Here’s a demo I’ve put together based on your workflow using a public demo API:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "url_principal",
              "value": "https://reqres.in/"
            }
          ]
        },
        "options": {}
      },
      "name": "url_base_api",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"url_principal\"]}}{{$json[\"url_buscar_todos_clientes\"]}}",
        "options": {},
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/x-www-form-urlencoded"
            }
          ]
        }
      },
      "name": "Buscar_todos_clientes",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "notesInFlow": true,
      "continueOnFail": true,
      "notes": "buscar todos clientes perfex, curso de perfex crm"
    },
    {
      "parameters": {
        "functionCode": "// Based on:\n// https://docs.n8n.io/reference/javascript-code-snippets.html#_1-create-multiple-items-from-a-single-item\nreturn items[0].json.data.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Dividir_clientes",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "url_buscar_todos_clientes",
              "value": "api/users"
            },
            {
              "name": "Adicionar_Cliente",
              "value": "api/customers"
            },
            {
              "name": "Excluir_Cliente",
              "value": "api/delete/customers/"
            },
            {
              "name": "Ver_todos_clientes",
              "value": "api/customers"
            },
            {
              "name": "Atualizar_cliente",
              "value": "api/customers/"
            },
            {
              "name": "Buscar_informacao_cliente",
              "value": "api/customers/search/"
            }
          ]
        },
        "options": {}
      },
      "name": "url_clientes",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$node[\"url_base_api\"].parameter[\"values\"][\"string\"][0][\"value\"]}}{{$items(\"url_clientes\")[0].json[\"url_buscar_todos_clientes\"]}}/{{$json[\"id_clinete\"]}}",
        "options": {
          "useQueryString": false
        }
      },
      "name": "Buscar_cliente_id",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1450,
        300
      ],
      "notesInFlow": true,
      "executeOnce": false,
      "continueOnFail": true,
      "notes": "Buscar o cliente pelo ID"
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "id_clinete",
              "value": "={{$json[\"id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "separar_id_cliente",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "url_base_api",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "url_base_api": {
      "main": [
        [
          {
            "node": "url_clientes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar_todos_clientes": {
      "main": [
        [
          {
            "node": "Dividir_clientes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dividir_clientes": {
      "main": [
        [
          {
            "node": "separar_id_cliente",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "url_clientes": {
      "main": [
        [
          {
            "node": "Buscar_todos_clientes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "separar_id_cliente": {
      "main": [
        [
          {
            "node": "Buscar_cliente_id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this helps :slight_smile:

1 Like

@MutedJam it worked yes, it works well, thank you very much. I’m really enjoying the community. I read a little bit about the global functions inside the flow. could you give me an example how I add to this flow that sent me the global role of ID_USER

I am glad to hear it works! Not sure I fully understand the follow up question though. Would ID_USER or its role be a response coming from the API you are connecting to? And where would you like to send it to?