{
  "id": "4tjCc9KAJEf8gkp5",
  "name": "Brazilian Company Lookup",
  "tags": [],
  "nodes": [
    {
      "id": "0a31b32a-db82-43d9-bebe-e27295bdbbb9",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 480,
        "height": 864,
        "content": "## Brazilian Company Lookup\n\n### How it works\n\nThis workflow collects a Brazilian CNPJ through a form, prepares the API URL, cleans the submitted CNPJ value, and validates it before making a lookup request. If the CNPJ passes validation, it queries an external company data API and displays the returned information through form response screens. If validation fails, it routes the user to an error or retry form path.\n\n### Setup steps\n\n- Configure the form trigger fields so users can submit a CNPJ value in the expected field name.\n- Review the Set URL node and set the correct API base URL for the Brazilian company lookup service.\n- If the lookup API requires authentication, add the required credentials or headers to the Send API Request node.\n- Check the If node conditions to ensure they match the desired CNPJ validation rules, such as cleaned length and numeric format.\n- Customize the form response nodes with the messages and fields you want users to see for success and validation failure paths.\n\n### Customization\n\nYou can adjust the CNPJ cleaning expression, validation condition, API endpoint, and the success/error form pages to fit different lookup providers or display formats."
      },
      "typeVersion": 1
    },
    {
      "id": "e014cd42-0454-4751-ae81-e14976b60f72",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 592,
        "height": 576,
        "content": "## Capture and prepare input\n\nThe left-side starting cluster receives the submitted form data, sets the API URL values, and normalizes the submitted CNPJ string for downstream validation."
      },
      "typeVersion": 1
    },
    {
      "id": "88b19a9c-9778-4bd7-adda-40d81118ecfe",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1152,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 704,
        "height": 576,
        "content": "## Validate and handle errors\n\nThis mid-canvas branch checks whether the cleaned CNPJ is valid and, on failure, sends the user through a nearby pair of form nodes for an error or retry response."
      },
      "typeVersion": 1
    },
    {
      "id": "d13a81e1-e673-4d35-81e4-bb691c479005",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1856,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 384,
        "height": 576,
        "content": "## Query company API\n\nThis isolated request node performs the company lookup by sending a GET request to the configured API endpoint when validation succeeds."
      },
      "typeVersion": 1
    },
    {
      "id": "6c99f941-514e-47ca-b2c2-a4d12d6d84af",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2240,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 496,
        "height": 576,
        "content": "## Present lookup results\n\nThe right-side form cluster displays the API lookup results through two closely placed form paths, likely covering returned company details or alternate response sections."
      },
      "typeVersion": 1
    },
    {
      "id": "b58606f8-3a6c-43ca-88f2-e20f7088c2af",
      "name": "When Form Submitted",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        608,
        240
      ],
      "parameters": {
        "options": {},
        "formTitle": "CNPJ",
        "formFields": {
          "values": [
            {
              "fieldLabel": "CNPJ",
              "placeholder": "00.000.000/0000-00",
              "requiredField": true
            }
          ]
        },
        "formDescription": "Enter a Brazilian CNPJ. Dots, slashes and hyphens are optional."
      },
      "typeVersion": 2.3
    },
    {
      "id": "03dd5285-fb9d-441a-a73b-1dfe47c9c848",
      "name": "Handle Form 1",
      "type": "n8n-nodes-base.form",
      "position": [
        2464,
        208
      ],
      "parameters": {
        "options": {},
        "operation": "completion",
        "redirectUrl": "={{ $('Set API URL Parameters').item.json.url }}",
        "respondWith": "redirect"
      },
      "typeVersion": 2.3
    },
    {
      "id": "98cbc178-fe20-4560-a9ef-f059d009ffa3",
      "name": "Handle Form 2",
      "type": "n8n-nodes-base.form",
      "position": [
        2288,
        208
      ],
      "parameters": {
        "options": {
          "formTitle": "=About the company CNPJ {{ $json.cnpj }}: {{ $json.razao_social }}",
          "buttonLabel": "Finish",
          "formDescription": "=**Company Name**\n{{ $json.razao_social }}\n\n**Trade Name**\n{{ $json.nome_fantasia }}\n\n**Status**\n{{ $json.descricao_situacao_cadastral }}\n\n**Business Size**\n{{ $json.porte }}\n\n**Primary Activity**\n{{ $json.cnae_fiscal }} - {{ $json.cnae_fiscal_descricao }}\n\n**Legal Nature**\n{{ $json.natureza_juridica }}\n\n**Business Start Date**\n{{ $json.data_inicio_atividade }}\n\n**Main Partners**\n{{ $json.qsa.map(s => s.nome_socio).join(', ') }}\n\n**Phone**\n{{ $json.ddd_telefone_1 || 'Not available' }}\n\n**Email**\n{{ $json.email || 'Not available' }}\n\n**Address**\n{{ $json.descricao_tipo_de_logradouro }} {{ $json.logradouro }}, {{ $json.numero }}{{ $json.complemento ? ', ' + $json.complemento : '' }}\n{{ $json.bairro }}\n{{ $json.municipio }} - {{ $json.uf }}\nZIP Code: {{ $json.cep }}\n\n**Other Activities**\n{{ $json.cnaes_secundarios.map(c => '\u2022 ' + c.descricao).join('\\n') }}"
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "9d3aa82c-c3cc-495e-8631-0bdb8df1b086",
      "name": "Handle Form",
      "type": "n8n-nodes-base.form",
      "position": [
        2464,
        384
      ],
      "parameters": {
        "options": {},
        "operation": "completion",
        "redirectUrl": "={{ $('Set API URL Parameters').item.json.url }}",
        "respondWith": "redirect"
      },
      "typeVersion": 2.3
    },
    {
      "id": "8742ceaa-c177-4710-bc78-7382ade73be1",
      "name": "Handle Form 3",
      "type": "n8n-nodes-base.form",
      "position": [
        2288,
        384
      ],
      "parameters": {
        "options": {
          "formTitle": "=About the company CNPJ {{ $('When Form Submitted').item.json.CNPJ }}",
          "buttonLabel": "Finish",
          "formDescription": "=Error:{{ $json.error.message.slice(22,-4) }}\n"
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "3e0f2985-4e2b-4cb0-88c7-bfde63bb1291",
      "name": "Set API URL Parameters",
      "type": "n8n-nodes-base.set",
      "position": [
        800,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "dc5f25ff-443b-42b9-be1f-2299632c747c",
              "name": "url",
              "type": "string",
              "value": "REPLACE_WITH_PRODUCTION_FORM_URL"
            },
            {
              "id": "2c3806e7-5ef5-450a-a9cb-bc688b3e38cf",
              "name": "api_base_url",
              "type": "string",
              "value": "https://minhareceita.org/"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "6e2bbd47-7f7e-4fb7-ba2b-414a3437023c",
      "name": "Fetch CNPJ Data",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueErrorOutput",
      "position": [
        1936,
        224
      ],
      "parameters": {
        "url": "={{ $('Set API URL Parameters').item.json.api_base_url }}{{ $json.CNPJ }}",
        "options": {}
      },
      "typeVersion": 4.2,
      "alwaysOutputData": false
    },
    {
      "id": "0d345c05-9ddb-4dca-a12f-354af2dd7f8b",
      "name": "Prepare CNPJ Format",
      "type": "n8n-nodes-base.set",
      "position": [
        1008,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "a6529fe3-50a3-4f52-857e-26b649bea2da",
              "name": "CNPJ",
              "type": "string",
              "value": "={{ $('When Form Submitted').item.json.CNPJ.replace(/\\D/g, '') }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "dea841ea-0486-4e89-9249-faed236575e6",
      "name": "Check CNPJ Completeness",
      "type": "n8n-nodes-base.if",
      "position": [
        1248,
        240
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "925db7fb-60a5-410b-8fed-34c11afa102e",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.CNPJ }}",
              "rightValue": ""
            },
            {
              "id": "99f1300f-8cbe-42ad-bceb-686e0c999325",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.CNPJ.length }}",
              "rightValue": 14
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "87dcdb73-9023-4b88-a86a-3d31014d52e2",
      "name": "Handle Form 4",
      "type": "n8n-nodes-base.form",
      "position": [
        1712,
        448
      ],
      "parameters": {
        "options": {},
        "operation": "completion",
        "redirectUrl": "={{ $('Set API URL Parameters').item.json.url }}",
        "respondWith": "redirect"
      },
      "typeVersion": 2.3
    },
    {
      "id": "cc825890-0724-4f01-8d36-c30bb7c53837",
      "name": "Handle Form 5",
      "type": "n8n-nodes-base.form",
      "position": [
        1536,
        448
      ],
      "parameters": {
        "options": {
          "formTitle": "=About the company CNPJ {{ $('When Form Submitted').item.json.CNPJ }}",
          "buttonLabel": "Finish",
          "formDescription": "=Error:Invalid format\n"
        }
      },
      "typeVersion": 2.3
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "39586ec3-f200-4251-8c37-6fc497f44136",
  "connections": {
    "Handle Form 2": {
      "main": [
        [
          {
            "node": "Handle Form 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Handle Form 3": {
      "main": [
        [
          {
            "node": "Handle Form",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Handle Form 5": {
      "main": [
        [
          {
            "node": "Handle Form 4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch CNPJ Data": {
      "main": [
        [
          {
            "node": "Handle Form 2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Form 3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare CNPJ Format": {
      "main": [
        [
          {
            "node": "Check CNPJ Completeness",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Form Submitted": {
      "main": [
        [
          {
            "node": "Set API URL Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set API URL Parameters": {
      "main": [
        [
          {
            "node": "Prepare CNPJ Format",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check CNPJ Completeness": {
      "main": [
        [
          {
            "node": "Fetch CNPJ Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Form 5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}