{
  "name": "Jsonpage - Complete a new row (name, VAT, address)",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1"
        },
        "event": "rowAdded",
        "options": {}
      },
      "id": "d4ea27e9-e9b7-5b3e-9730-0d68a0635ec1",
      "name": "Google Sheets Trigger",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ],
      "credentials": {
        "googleSheetsTriggerOAuth2Api": {
          "id": "",
          "name": "Google Sheets Trigger account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "828b69e5-fbf4-5bc0-8758-cf685ab2b35a",
              "name": "apiKey",
              "value": "PASTE_YOUR_JSONPAGE_API_KEY",
              "type": "string"
            },
            {
              "id": "64ce9908-c5f7-58f1-8936-09ef0522c598",
              "name": "baseUrl",
              "value": "https://api.jsonpage.com/v2",
              "type": "string"
            },
            {
              "id": "7a246d0b-c427-5267-88e6-1fa5b51254cd",
              "name": "column",
              "value": "Entreprise",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {
          "includeBinary": true
        }
      },
      "id": "ebcc5125-baff-579a-926a-4056654b4235",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ],
      "notes": "Edit the values of this node only: nothing else needs to change.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "jsCode": "// Turns the cell into a search term the autocomplete route understands:\n// a name, a SIREN or SIRET, a French VAT number, or an identifier such as FR-552032534.\nconst column = $('Config').first().json.column || 'Entreprise';\nreturn $input.all().map((item) => {\n  const raw = String(item.json[column] ?? '').trim();\n  let q = raw;\n  let country = '';\n  const global = /^([A-Za-z]{2})-([0-9A-Za-z]+)$/.exec(raw);\n  if (global) {\n    country = global[1].toUpperCase();\n    q = global[2];\n  } else {\n    const compact = raw.toUpperCase().replace(/[\\s.\\-]/g, '');\n    const vat = /^FR[0-9A-Z]{2}([0-9]{9})$/.exec(compact);\n    if (vat) q = vat[1];\n    else if (/^[0-9]{9,14}$/.test(compact)) q = compact;\n  }\n  return { json: { ...item.json, q, country } };\n});"
      },
      "id": "fe6f4168-7092-5897-948e-7675a92c9c47",
      "name": "Prepare",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Config').first().json.baseUrl }}/autocomplete",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $json.q }}"
            },
            {
              "name": "country",
              "value": "={{ $json.country }}"
            },
            {
              "name": "limit",
              "value": "1"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $('Config').first().json.apiKey }}"
            }
          ]
        },
        "options": {}
      },
      "id": "0299ae39-7e92-5873-807d-8587961ad7de",
      "name": "Autocomplete",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        300
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "111d9397-1f5e-5d65-9d20-9f586e02b5ce",
              "leftValue": "={{ $json.data && $json.data.length > 0 ? $json.data[0].id : '' }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "111d9397-1f5e-5d65-9d20-9f586e02b5ce",
      "name": "Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        880,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Config').first().json.baseUrl }}/companies/{{ $('Autocomplete').item.json.data[0].id }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "include",
              "value": "address,vat"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $('Config').first().json.apiKey }}"
            }
          ]
        },
        "options": {}
      },
      "id": "1bcdfc18-63e8-5a21-8a2e-83ec5edf17f2",
      "name": "Company",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        200
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "57f93727-c1c6-5b4f-a162-46a8c0f4b303",
              "name": "row_number",
              "value": "={{ $('Prepare').item.json.row_number }}",
              "type": "number"
            },
            {
              "id": "ac6f18eb-bac8-524c-a1ec-ba03894d5e6e",
              "name": "Official name",
              "value": "={{ $json.data.name }}",
              "type": "string"
            },
            {
              "id": "dae8533e-e924-531a-94e2-51e14669801c",
              "name": "SIREN",
              "value": "={{ $json.data.registry_id }}",
              "type": "string"
            },
            {
              "id": "62b4f4a7-e984-57db-89ad-9276bd55f815",
              "name": "Status",
              "value": "={{ $json.data.status }}",
              "type": "string"
            },
            {
              "id": "92857ec7-fe5c-54cd-92ec-55a228b438a3",
              "name": "VAT number",
              "value": "={{ $json.data.vat ? $json.data.vat.number : '' }}",
              "type": "string"
            },
            {
              "id": "3e990d7f-8d99-59f4-a38d-2231a5f35142",
              "name": "VAT status",
              "value": "={{ $json.data.vat ? $json.data.vat.status : '' }}",
              "type": "string"
            },
            {
              "id": "78462244-0b59-5193-8f43-21047096ad60",
              "name": "Address",
              "value": "={{ $json.data.address ? $json.data.address.formatted : '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "2bc6ef69-7070-5f7b-ae10-a3e6be42e340",
      "name": "Completed row",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1320,
        200
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "301d013b-04ff-5497-a23b-53b12d84e7e2",
              "name": "row_number",
              "value": "={{ $('Prepare').item.json.row_number }}",
              "type": "number"
            },
            {
              "id": "eb40d053-55d6-59e1-9b62-b33e030ec982",
              "name": "Official name",
              "value": "",
              "type": "string"
            },
            {
              "id": "1a25b69d-cff9-5b90-a842-c60fd47d4e07",
              "name": "SIREN",
              "value": "",
              "type": "string"
            },
            {
              "id": "a94c5ae6-fcc6-5dc9-823f-6b2da7c3e905",
              "name": "Status",
              "value": "NOT_FOUND",
              "type": "string"
            },
            {
              "id": "f0c8d283-a5a4-5a7a-9b1e-6a5a6bdbaf1f",
              "name": "VAT number",
              "value": "",
              "type": "string"
            },
            {
              "id": "133d48e2-c078-5804-9d4b-a0c07e52d4e3",
              "name": "VAT status",
              "value": "",
              "type": "string"
            },
            {
              "id": "7ed96a2b-3a66-52fe-a0de-d372f7b5df2c",
              "name": "Address",
              "value": "",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "31e76aa7-5723-5b29-b04a-b278794dfc4d",
      "name": "Not found",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1100,
        420
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "row_number": "={{ $json.row_number }}",
            "Official name": "={{ $json['Official name'] }}",
            "SIREN": "={{ $json['SIREN'] }}",
            "Status": "={{ $json['Status'] }}",
            "VAT number": "={{ $json['VAT number'] }}",
            "VAT status": "={{ $json['VAT status'] }}",
            "Address": "={{ $json['Address'] }}"
          },
          "matchingColumns": [
            "row_number"
          ],
          "schema": [
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": false
            },
            {
              "id": "Official name",
              "displayName": "Official name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "SIREN",
              "displayName": "SIREN",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "VAT number",
              "displayName": "VAT number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "VAT status",
              "displayName": "VAT status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Address",
              "displayName": "Address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            }
          ]
        },
        "options": {}
      },
      "id": "fad89adf-00f2-5524-a4ab-9070df9bc30a",
      "name": "Write back to the sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1540,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Google Sheets Trigger": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Prepare",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare": {
      "main": [
        [
          {
            "node": "Autocomplete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Autocomplete": {
      "main": [
        [
          {
            "node": "Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Found?": {
      "main": [
        [
          {
            "node": "Company",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Company": {
      "main": [
        [
          {
            "node": "Completed row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Completed row": {
      "main": [
        [
          {
            "node": "Write back to the sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Not found": {
      "main": [
        [
          {
            "node": "Write back to the sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
