{
  "name": "bubbl3: leads from Paperform",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "replace-with-a-long-random-value",
        "options": {}
      },
      "name": "Paperform sends here",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "t",
              "name": "t",
              "value": "={{ String(Math.floor(Date.now() / 1000)) }}",
              "type": "string"
            },
            {
              "id": "event_id",
              "name": "event_id",
              "value": "={{ (() => { const id = [$json.body.submission_id]; if (id.some((v) => v === undefined || v === null || v === '')) throw new Error('No record id from Paperform'); return 'paperform-' + id.join('-'); })() }}",
              "type": "string"
            },
            {
              "id": "lead",
              "name": "lead",
              "value": "={{ JSON.stringify({ event_id: (() => { const id = [$json.body.submission_id]; if (id.some((v) => v === undefined || v === null || v === '')) throw new Error('No record id from Paperform'); return 'paperform-' + id.join('-'); })(), phone: ($json.body.data.find((f) => f.custom_key === 'phone') || {}).value, full_name: ($json.body.data.find((f) => f.custom_key === 'name') || {}).value, email: ($json.body.data.find((f) => f.custom_key === 'email') || {}).value }) }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "name": "Lead fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [300, 0]
    },
    {
      "parameters": {
        "action": "hmac",
        "type": "SHA256",
        "value": "={{ $json.t }}.{{ $json.lead }}",
        "dataPropertyName": "signature",
        "secret": "PASTE_THE_SOURCE_SECRET",
        "encoding": "hex"
      },
      "name": "Signature",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [600, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.bubbl3.com/v1/inbound/leads/PASTE_THE_SOURCE_ID",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "bubbl3-signature", "value": "=t={{ $json.t }},v1={{ $json.signature }}" },
            { "name": "Idempotency-Key", "value": "={{ $json.event_id }}" }
          ]
        },
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ $json.lead }}",
        "options": {}
      },
      "name": "Send the lead to bubbl3",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [900, 0]
    }
  ],
  "connections": {
    "Paperform sends here": { "main": [[{ "node": "Lead fields", "type": "main", "index": 0 }]] },
    "Lead fields": { "main": [[{ "node": "Signature", "type": "main", "index": 0 }]] },
    "Signature": { "main": [[{ "node": "Send the lead to bubbl3", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
