{
  "id": "eObdW8oqxSRIvxi2",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": [],
  "nodes": [
    {
      "id": "4a712b97-03ff-4f47-b17a-d2db1730cd4b",
      "name": "Execute Workflow Manually",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -368,
        176
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "a6a2f770-8850-49f5-a475-8069d852f35d",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "position": [
        -112,
        176
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "dc5f25ff-443b-42b9-be1f-2299632c747c",
              "name": "telegram_id",
              "type": "string",
              "value": "YOUR_TELEGRAM_ID"
            },
            {
              "id": "2c3806e7-5ef5-450a-a9cb-bc688b3e38cf",
              "name": "api_base_url",
              "type": "string",
              "value": "https://minhareceita.org/"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "5ce78f98-1dcf-4048-bb3e-8c038470e991",
      "name": "Loop CNPJs",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        496,
        176
      ],
      "parameters": {
        "options": {},
        "batchSize": 100
      },
      "typeVersion": 3
    },
    {
      "id": "4e6e8675-949d-4a39-b779-efc3738a5445",
      "name": "Send API Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        800,
        304
      ],
      "parameters": {
        "url": "={{ $('Settings').item.json.api_base_url }}{{ $json.cnpj }}",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "bcfd66a0-8e12-41b0-a87e-067bbe79dc30",
      "name": "Update CNPJ Info in Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1136,
        304
      ],
      "parameters": {
        "columns": {
          "value": {
            "uf": "={{ $json.uf }}",
            "cep": "={{ $json.cep }}",
            "pais": "={{ $json.pais }}",
            "email": "={{ $json.email }}",
            "porte": "={{ $json.porte }}",
            "bairro": "={{ $json.bairro }}",
            "numero": "={{ $json.numero }}",
            "ddd_fax": "={{ $json.ddd_fax }}",
            "municipio": "={{ $json.municipio }}",
            "logradouro": "={{ $json.logradouro }}",
            "row_number": "={{ $('Loop CNPJs').item.json.row_number }}",
            "codigo_pais": "={{ $json.codigo_pais }}",
            "complemento": "={{ $json.complemento }}",
            "codigo_porte": "={{ $json.codigo_porte }}",
            "razao_social": "={{ $json.razao_social }}",
            "nome_fantasia": "={{ $json.nome_fantasia }}",
            "capital_social": "={{ $json.capital_social }}",
            "ddd_telefone_1": "={{ $json.ddd_telefone_1 }}",
            "ddd_telefone_2": "={{ $json.ddd_telefone_2 }}",
            "codigo_municipio": "={{ $json.codigo_municipio }}",
            "natureza_juridica": "={{ $json.natureza_juridica }}",
            "codigo_municipio_ibge": "={{ $json.codigo_municipio_ibge }}",
            "nome_cidade_no_exterior": "={{ $json.nome_cidade_no_exterior }}",
            "descricao_tipo_de_logradouro": "={{ $json.descricao_tipo_de_logradouro }}"
          },
          "schema": [
            {
              "id": "cnpj",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "cnpj",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "razao_social",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "razao_social",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "nome_fantasia",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "nome_fantasia",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "capital_social",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "capital_social",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "porte",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "porte",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "codigo_porte",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "codigo_porte",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "uf",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "uf",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "municipio",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "municipio",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "codigo_municipio",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "codigo_municipio",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "codigo_municipio_ibge",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "codigo_municipio_ibge",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "bairro",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "bairro",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "logradouro",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "logradouro",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "descricao_tipo_de_logradouro",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "descricao_tipo_de_logradouro",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "numero",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "numero",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "complemento",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "complemento",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "cep",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "cep",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "pais",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "pais",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "codigo_pais",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "codigo_pais",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "nome_cidade_no_exterior",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "nome_cidade_no_exterior",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ddd_telefone_1",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ddd_telefone_1",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ddd_telefone_2",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ddd_telefone_2",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ddd_fax",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ddd_fax",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "natureza_juridica",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "natureza_juridica",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "codigo_natureza_juridica",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "codigo_natureza_juridica",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "cnae_fiscal",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "cnae_fiscal",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "cnae_fiscal_descricao",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "cnae_fiscal_descricao",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "cnaes_secundarios",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "cnaes_secundarios",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "regime_tributario",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "regime_tributario",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "qualificacao_do_responsavel",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "qualificacao_do_responsavel",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "situacao_cadastral",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "situacao_cadastral",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "descricao_situacao_cadastral",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "descricao_situacao_cadastral",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "motivo_situacao_cadastral",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "motivo_situacao_cadastral",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "descricao_motivo_situacao_cadastral",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "descricao_motivo_situacao_cadastral",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "situacao_especial",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "situacao_especial",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "identificador_matriz_filial",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "identificador_matriz_filial",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "descricao_identificador_matriz_filial",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "descricao_identificador_matriz_filial",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ente_federativo_responsavel",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ente_federativo_responsavel",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_inicio_atividade",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_inicio_atividade",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_situacao_cadastral",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_situacao_cadastral",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_situacao_especial",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_situacao_especial",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "opcao_pelo_mei",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "opcao_pelo_mei",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_opcao_pelo_mei",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_opcao_pelo_mei",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_exclusao_do_mei",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_exclusao_do_mei",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "opcao_pelo_simples",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "opcao_pelo_simples",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_opcao_pelo_simples",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_opcao_pelo_simples",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data_exclusao_do_simples",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data_exclusao_do_simples",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "qsa",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "qsa",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": false,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "row_number"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1Y5GlbWxbWD8zCnB8-vJIlTVtrWEqztyTXeaE6OTuY8s/edit#gid=0",
          "cachedResultName": "P\u00e1gina1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1Y5GlbWxbWD8zCnB8-vJIlTVtrWEqztyTXeaE6OTuY8s",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1Y5GlbWxbWD8zCnB8-vJIlTVtrWEqztyTXeaE6OTuY8s/edit?usp=drivesdk",
          "cachedResultName": "Planilha sem t\u00edtulo"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "9aa7367f-4a85-4007-a482-4828fd276654",
      "name": "Notify When Finish",
      "type": "n8n-nodes-base.telegram",
      "position": [
        976,
        -112
      ],
      "parameters": {
        "text": "=CNPJ info extraction complete!",
        "chatId": "={{ $('Settings').item.json.telegram_id }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "bdb3fd23-cedc-43d9-872f-32bf099c4665",
      "name": "Limit Notifier Interactions",
      "type": "n8n-nodes-base.limit",
      "position": [
        704,
        -112
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "6a1ba7c8-9358-46db-be11-21d98ab87506",
      "name": "Get All CNPJs in Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        224,
        176
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "razao_social"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1Y5GlbWxbWD8zCnB8-vJIlTVtrWEqztyTXeaE6OTuY8s/edit#gid=0",
          "cachedResultName": "P\u00e1gina1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1Y5GlbWxbWD8zCnB8-vJIlTVtrWEqztyTXeaE6OTuY8s",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1Y5GlbWxbWD8zCnB8-vJIlTVtrWEqztyTXeaE6OTuY8s/edit?usp=drivesdk",
          "cachedResultName": "Planilha sem t\u00edtulo"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "573946b9-f954-4b25-b039-0631abd131a1",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -208,
        -16
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 336,
        "content": "## \u2699\ufe0f INITIAL SETUP\n- Configure your Telegram ID for notifications\n- API URL is already configured (MinhaReceita.org)\n- Make sure Google Sheets credentials are active"
      },
      "typeVersion": 1
    },
    {
      "id": "b0c352e7-4f9e-4290-9b34-e5603423143f",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        112,
        -16
      ],
      "parameters": {
        "color": 7,
        "width": 320,
        "height": 336,
        "content": "## \ud83d\udcca DATA SOURCE\n- Reads CNPJs from Google Sheets spreadsheet\n- Filters by empty 'razao_social' column\n- Processes only CNPJs without data"
      },
      "typeVersion": 1
    },
    {
      "id": "1a464427-aed0-4d9e-9176-2728d947bad7",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        688,
        112
      ],
      "parameters": {
        "color": 7,
        "width": 320,
        "height": 352,
        "content": "## \ud83c\udf10 API DETAILS\n- API: minhareceita.org/{cnpj}\n- Returns +40 fields from Federal Revenue\n- Updated and official data\n- Includes IBGE codes and classifications\n- Cadastral status history\n- Free, no authentication required"
      },
      "typeVersion": 1
    },
    {
      "id": "8f07398e-256f-4e33-9972-9786519a37e1",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        864,
        -240
      ],
      "parameters": {
        "color": 7,
        "width": 320,
        "height": 288,
        "content": "## \ud83d\udcf1 NOTIFICATIONS\n- Telegram notifies when finished\n- Limiter prevents notification spam\n- One notification per complete execution"
      },
      "typeVersion": 1
    },
    {
      "id": "82a3945d-fac6-4ff5-820a-f3d6aacbc31e",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1024,
        112
      ],
      "parameters": {
        "color": 7,
        "width": 320,
        "height": 352,
        "content": "## \ud83d\udcc4 INSERT NEW DATA\n- Retrieves data from API\n- Updates corresponding row in spreadsheet with new information"
      },
      "typeVersion": 1
    },
    {
      "id": "3de014ac-1ab2-483e-90bb-fda01fdbf431",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -208,
        -528
      ],
      "parameters": {
        "width": 272,
        "height": 112,
        "content": "# \ud83d\udccc Follow me:\n## [LinkedIn](https://www.linkedin.com/in/vikthyr)"
      },
      "typeVersion": 1
    },
    {
      "id": "36960786-d9cd-4527-957d-4762b2131aa7",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -208,
        -400
      ],
      "parameters": {
        "width": 640,
        "height": 368,
        "content": "# \ud83d\udccb HOW THE WORKFLOW WORKS\n\n**1\ufe0f\u20e3 START:** Manual execution triggers the process\n\n**2\ufe0f\u20e3 SETUP:** Defines variables (Telegram ID and API URL)\n\n**3\ufe0f\u20e3 READING:** Searches for all CNPJs in the spreadsheet that don't have data (empty 'razao_social' column)\n\n**4\ufe0f\u20e3 PROCESSING:** For each CNPJ found:\n   \u2022 Makes HTTP request to MinhaReceita API\n   \u2022 Receives complete company data\n   \u2022 Updates corresponding row in spreadsheet\n\n**5\ufe0f\u20e3 NOTIFICATION:** Upon completion of entire process, sends Telegram message confirming conclusion\n\n**\ud83d\udd04 The loop continues until all pending CNPJs are processed**"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "71a2befa-8a16-419d-b2cf-750ef55a4ab2",
  "connections": {
    "Settings": {
      "main": [
        [
          {
            "node": "Get All CNPJs in Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop CNPJs": {
      "main": [
        [
          {
            "node": "Limit Notifier Interactions",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send API Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send API Request": {
      "main": [
        [
          {
            "node": "Update CNPJ Info in Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get All CNPJs in Sheet": {
      "main": [
        [
          {
            "node": "Loop CNPJs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow Manually": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update CNPJ Info in Sheet": {
      "main": [
        [
          {
            "node": "Loop CNPJs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Limit Notifier Interactions": {
      "main": [
        [
          {
            "node": "Notify When Finish",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}