{
  "name": "Google Sheets \u2192 SIRENE Lookup API \u2192 Google Sheets",
  "nodes": [
    {
      "parameters": {},
      "id": "b2d3e4f5-2001-4c00-9000-000000000001",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "notes": "Run by hand after the Google Sheet and the SIRENE license credential are set. You can replace this with a Schedule trigger."
    },
    {
      "parameters": {
        "resource": "sheet",
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Input",
          "mode": "name"
        },
        "options": {}
      },
      "id": "b2d3e4f5-2002-4c00-9000-000000000002",
      "name": "Read SIREN list",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        240,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      },
      "notes": "Tab 'Input' must have a header siren_or_siret and one 9-digit SIREN or 14-digit SIRET per row. Paste your spreadsheet ID in place of YOUR_GOOGLE_SHEET_ID and connect Google Sheets OAuth."
    },
    {
      "parameters": {
        "jsCode": "// Keep only 9-digit SIREN or 14-digit SIRET values (digits only).\nreturn $input.all()\n  .map(item => {\n    const raw = String(\n      item.json.siren_or_siret ?? item.json.SIREN ?? item.json.SIRET ?? item.json.siren ?? item.json.siret ?? ''\n    ).trim();\n    const digits = raw.replace(/\\D/g, '');\n    let kind = null;\n    if (digits.length === 9) kind = 'siren';\n    else if (digits.length === 14) kind = 'siret';\n    return { json: { input: raw, digits, kind } };\n  })\n  .filter(item => item.json.kind);\n"
      },
      "id": "b2d3e4f5-2003-4c00-9000-000000000003",
      "name": "Normalize numbers",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://sirene-lookup-api.corinaflorea8.workers.dev/v1/{{ $json.kind }}/{{ $json.digits }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "batching": {
            "batch": {
              "batchSize": 1,
              "batchInterval": 1100
            }
          },
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": false
            }
          },
          "timeout": 30000
        }
      },
      "id": "b2d3e4f5-2004-4c00-9000-000000000004",
      "name": "SIRENE Lookup API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000,
      "credentials": {
        "httpHeaderAuth": {
          "id": "YOUR_SIRENE_HEADER_AUTH_CREDENTIAL_ID",
          "name": "SIRENE License Key"
        }
      },
      "notes": "Header Auth credential: Name = X-License-Key, Value = your Gumroad license key ({{SIRENE_LICENSE_KEY}}). Never hard-code the key in the workflow. Buy a key at https://corinaflorea.gumroad.com/l/xzhech. One call per second stays under the 60/min limit."
    },
    {
      "parameters": {
        "jsCode": "// Shape the API response for the Results sheet.\n// For a SIRET lookup, address comes from the establishment; otherwise from the head office.\n// commercial_opt_out = true must not be used for prospecting. masked = true means name/address were withheld.\nreturn $input.all().map((item, i) => {\n  const src = $('Normalize numbers').all()[i]?.json || {};\n  const api = (item.json && (item.json.data !== undefined || item.json.error !== undefined))\n    ? item.json\n    : (item.json?.api || item.json || {});\n  const err = api.error;\n  const data = api.data;\n  const company = data && (data.company || (data.siren ? data : null));\n  const est = (data && data.establishment) || (company && company.head_office) || {};\n  if (!company) {\n    return { json: {\n      input: src.input || src.digits || '',\n      siren: (src.digits && src.digits.length === 14) ? src.digits.slice(0, 9) : (src.digits || ''),\n      name: '',\n      address: '',\n      naf_code: '',\n      status: '',\n      creation_date: '',\n      commercial_opt_out: '',\n      masked: '',\n      error: (err && (err.code || err.message)) || 'not_found',\n    }};\n  }\n  return { json: {\n    input: src.input || src.digits || '',\n    siren: company.siren || '',\n    name: company.name || '',\n    address: est.address || company.head_office?.address || '',\n    naf_code: company.naf_code || '',\n    status: company.status || '',\n    creation_date: company.creation_date || '',\n    commercial_opt_out: company.commercial_opt_out === true,\n    masked: !!(company.diffusion && company.diffusion.masked),\n    error: '',\n  }};\n});\n"
      },
      "id": "b2d3e4f5-2005-4c00-9000-000000000005",
      "name": "Flatten company",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        0
      ]
    },
    {
      "parameters": {
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Results",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "input": "={{ $json.input }}",
            "siren": "={{ $json.siren }}",
            "name": "={{ $json.name }}",
            "address": "={{ $json.address }}",
            "naf_code": "={{ $json.naf_code }}",
            "status": "={{ $json.status }}",
            "creation_date": "={{ $json.creation_date }}",
            "commercial_opt_out": "={{ $json.commercial_opt_out }}",
            "masked": "={{ $json.masked }}",
            "error": "={{ $json.error }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "input",
              "displayName": "input",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "siren",
              "displayName": "siren",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "name",
              "displayName": "name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "address",
              "displayName": "address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "naf_code",
              "displayName": "naf_code",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "creation_date",
              "displayName": "creation_date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "commercial_opt_out",
              "displayName": "commercial_opt_out",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "masked",
              "displayName": "masked",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "error",
              "displayName": "error",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "USER_ENTERED",
          "useAppend": true
        }
      },
      "id": "b2d3e4f5-2006-4c00-9000-000000000006",
      "name": "Write results to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1200,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      },
      "notes": "Tab 'Results' must have the header row: input, siren, name, address, naf_code, status, creation_date, commercial_opt_out, masked, error. This is the real Google Sheets node \u2014 the workflow writes the rows itself."
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Read SIREN list",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read SIREN list": {
      "main": [
        [
          {
            "node": "Normalize numbers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize numbers": {
      "main": [
        [
          {
            "node": "SIRENE Lookup API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SIRENE Lookup API": {
      "main": [
        [
          {
            "node": "Flatten company",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten company": {
      "main": [
        [
          {
            "node": "Write results to Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Europe/Bucharest"
  },
  "pinData": {},
  "meta": {
    "instanceId": "REPLACE_ME"
  }
}
