{
  "id": "im2OjuCOBEsjN0Bs",
  "name": "Sync Beex leads to HubSpot copy",
  "tags": [],
  "nodes": [
    {
      "id": "9797c1ac-6392-4f62-873b-f312d7021f6a",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 528,
        "height": 1088,
        "content": "## Sync Beex Leads to HubSpot\n\n> **Disclaimer:** This workflow requires the community node `n8n-nodes-beex`\n\n### Overview\n\nThis workflow listens for **lead creation and update events** from Beex via a webhook and synchronizes them with **HubSpot contacts**.\n\n### How It Works\n\n1. **Trigger**: The workflow is initiated by a lead creation or update event from the Beex node\n2. **Data Transformation**: The `data` payload from the Beex Trigger is converted into a flat JSON structure to simplify field handling\n3. **Email Validation**: The workflow verifies that the lead contains a  non-null email address\n4. **Field Mapping**: Fields are mapped (via drag and drop) from the Beex payload to HubSpot contact properties\n5. **Event Routing**: Based on the Beex event type, the workflow routes to the appropriate branch:\n   - `CREATE`\n   - `UPDATE`\n6. **HTTP Execution**: The corresponding HTTP request is executed:\n   - **POST** to create a new contact in HubSpot\n   - **PUT** to update an existing contact in HubSpot\n   - Both operations use the same JSON body structure, differing only in endpoint and HTTP method\n\n### Usage\n\n- Use the community trigger node from `n8n-nodes-beex`\n- Configure 'Event Types' in the Beex Trigger\n- When mapping fields for create/update operations, ensure that:\n  - The email field is valid (non-null)\n  - Each mapped field name matches an existing HubSpot contact property (default or custom)\n\n### Requirements\n\n- **HubSpot Account** with an App Token (typically from a custom app)\n  - The app must have **read** and **write** permissions for **Contact/Customer** objects\n- **Beex Account** with permission to generate leads\n- Valid Beex **Bearer YOUR_TOKEN_HERE** (`YOUR_TOKEN_HERE`) configured in the Beex Trigger node"
      },
      "typeVersion": 1
    },
    {
      "id": "4ba61631-2c1c-4ce8-b97f-beb5b0ee5667",
      "name": "Routing",
      "type": "n8n-nodes-base.switch",
      "notes": "Create/Update",
      "position": [
        1600,
        528
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "CREATE",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "bf8b6a85-4550-48a0-b97a-7d6db561f20a",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ \n  $('Beex Trigger').item.json.event \n}}",
                    "rightValue": "contact_create"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "UPDATE",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "6fefe4f7-364b-455d-b3a5-9df48585ec3a",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ \n  $('Beex Trigger').item.json.event \n}}",
                    "rightValue": "contact_update"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "notesInFlow": true,
      "typeVersion": 3.3
    },
    {
      "id": "3e4110d9-50a3-45b0-b927-5ac56be2ff26",
      "name": "Update Contact",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "HubSpot",
      "position": [
        1824,
        624
      ],
      "parameters": {
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts/{{ $json.email }}",
        "method": "PATCH",
        "options": {},
        "jsonBody": "={\"properties\": {{ $json.toJsonString() }}}",
        "sendBody": true,
        "sendQuery": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "idProperty",
              "value": "email"
            }
          ]
        },
        "nodeCredentialType": "hubspotAppToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "id": "c4fba358-7d67-4b12-8bdc-10bdbaaf5629",
      "name": "\u00bfEmail Null?",
      "type": "n8n-nodes-base.filter",
      "notes": "Filter",
      "position": [
        1168,
        528
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "fcd1cc2b-f7c4-4510-a9af-5014c6b78444",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.email }}",
              "rightValue": ""
            },
            {
              "id": "1fd401e8-7d43-46a0-a09b-d9ffa70cda55",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.email }}",
              "rightValue": ""
            }
          ]
        }
      },
      "notesInFlow": true,
      "typeVersion": 2.2
    },
    {
      "id": "b0486c6a-6dd6-47e6-b24c-a6e3b0f79ae7",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1504,
        304
      ],
      "parameters": {
        "color": 7,
        "width": 528,
        "height": 480,
        "content": "## HTTP Requests (Routing + Create/Update)"
      },
      "typeVersion": 1
    },
    {
      "id": "4c944d2c-88ad-4894-9043-76bab5a45da5",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        624,
        304
      ],
      "parameters": {
        "color": 7,
        "width": 496,
        "height": 480,
        "content": "## Trigger Node (Beex Trigger + Format)\n- Link the webhook **URL** on the Beex platform\n- We convert the input data into flat JSON.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "c9825a97-ddb9-41ac-b783-1f3db9fc58cd",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1120,
        304
      ],
      "parameters": {
        "color": 5,
        "width": 384,
        "height": 480,
        "content": "## Filter and Set Fields\n- Leads without an email address are filtered out.\n- The fields to be configured must have the same name as the properties in HubSpot."
      },
      "typeVersion": 1
    },
    {
      "id": "cbf52c75-156e-49a8-bb93-9f24b5898487",
      "name": "Create Contact",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "HubSpot",
      "position": [
        1824,
        432
      ],
      "parameters": {
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts",
        "method": "POST",
        "options": {},
        "jsonBody": "={\"properties\": {{ $json.toJsonString() }}}",
        "sendBody": true,
        "sendQuery": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {}
          ]
        },
        "nodeCredentialType": "hubspotAppToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "id": "a290b1cb-5767-4509-a21f-e5529318eb3b",
      "name": "Beex Trigger",
      "type": "n8n-nodes-beex.beexTrigger",
      "notes": "Create/Update",
      "position": [
        704,
        528
      ],
      "parameters": {
        "eventTypes": [
          "contact_create",
          "contact_update"
        ]
      },
      "credentials": {
        "beexApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 1
    },
    {
      "id": "4af24a23-2a25-46a9-a4a7-07489e91f1fe",
      "name": "Format",
      "type": "n8n-nodes-base.code",
      "notes": "JavaScript",
      "position": [
        928,
        528
      ],
      "parameters": {
        "jsCode": "const data = $json.data;\nconst generalDates = data.general_dates;\nconst customFields = data.custom_fields;\n\n\nconst flatDates = {};\nconst flatFields = {};\n\nfor (const [key, obj] of Object.entries(generalDates)) {\n  flatDates[key] = obj.value;\n}\n\nfor (const [key, obj] of Object.entries(customFields)) {\n  flatFields[obj.title] = obj.value;\n}\n\n\nreturn {\n  email: data.email,\n  portfolio_id: data.portfolio.id,\n  portfolio_name: data.portfolio.name,\n  ...flatDates,\n  ...flatFields\n};\n"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "3289fc09-5bb3-4ec1-bc24-600888d396ab",
      "name": "Set Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        1360,
        528
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "039a4910-05c7-439f-9407-4a8c61110072",
              "name": "firstname",
              "type": "string",
              "value": "={{ $json.first_name }}"
            },
            {
              "id": "15d28009-0862-4bcc-859f-d63db7acc819",
              "name": "lastname",
              "type": "string",
              "value": "={{ $json.paternal_surname }} {{ $json.maternal_surname }}"
            },
            {
              "id": "e2403a2a-6569-4b86-9627-423f0db09198",
              "name": "email",
              "type": "string",
              "value": "={{ $json.email }}"
            },
            {
              "id": "cd29acd0-0a4d-493c-873f-29dd488b6e1c",
              "name": "hubspot_custom_field",
              "type": "string",
              "value": "={{ $json.hubspot_custom_field }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "d81812c2-c4a9-4491-9661-9f29a73ad675",
  "connections": {
    "Format": {
      "main": [
        [
          {
            "node": "\u00bfEmail Null?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Routing": {
      "main": [
        [
          {
            "node": "Create Contact",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Fields": {
      "main": [
        [
          {
            "node": "Routing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Beex Trigger": {
      "main": [
        [
          {
            "node": "Format",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u00bfEmail Null?": {
      "main": [
        [
          {
            "node": "Set Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}