{
  "id": "FwN3BphNMsHcoKWs",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Create new Beehiiv subscribers from new Systeme.io optins (funnel level)",
  "tags": [],
  "nodes": [
    {
      "id": "6617a5d6-38a5-44e9-a42e-c4b89ebcf2a7",
      "name": "Clean Data",
      "type": "n8n-nodes-base.set",
      "position": [
        -800,
        0
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={\n  \"email\": \"{{ $('On New Systeme.io Optin').item.json.body.data.contact.email }}\",\n  \"first_name\": \"{{ $('On New Systeme.io Optin').item.json.body.data.contact.fields.first_name ?? \"\" }}\",\n  \"last_name\" : \"{{ $('On New Systeme.io Optin').item.json.body.data.contact.fields.surname ?? \"\" }}\",\n  \"referring_site\" : \"{{ $('On New Systeme.io Optin').item.json.body.data.source_url.replace(/\\?.*$/, '') }}\",\n  \"utm_source\" : \"{{ $('On New Systeme.io Optin').item.json.body.data.source_url.match(/[?&]utm_source=([^&]*)/)?.[1] ?? \"\" }}\",\n  \"utm_medium\" : \"{{ $('On New Systeme.io Optin').item.json.body.data.source_url.match(/[?&]utm_medium=([^&]*)/)?.[1] ?? \"\" }}\",\n  \"utm_campaign\" : \"{{ $('On New Systeme.io Optin').item.json.body.data.source_url.match(/[?&]utm_campaign=([^&]*)/)?.[1] ?? \"\" }}\"\n}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "dca87621-6a51-4a02-a597-2d53ad59d793",
      "name": "Create New Beehiiv Subscriber",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -460,
        0
      ],
      "parameters": {
        "url": "=https://api.beehiiv.com/v2/publications/{{ $('Configure Workflow').item.json.beehiiv_publication_id }}/subscriptions",
        "method": "POST",
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        },
        "jsonBody": "={\n  \"email\": \"{{ $json.email }}\",\n  \"utm_source\": \"{{ $json.utm_source }}\",\n  \"utm_medium\": \"{{ $json.utm_medium }}\",\n  \"utm_campaign\": \"{{ $json.utm_campaign }}\",\n  \"referring_site\": \"{{ $json.referring_site }}\",\n  \"custom_fields\": [\n    {\n      \"name\": \"{{ $('Configure Workflow').item.json.beehiiv_firstname_field_name }}\",\n      \"value\": \"{{ $json.first_name }}\"\n    },\n    {\n      \"name\": \"{{ $('Configure Workflow').item.json.beehiiv_lastname_field_name }}\",\n      \"value\": \"{{ $json.last_name }}\"\n    }\n  ]\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "bff73db0-e3f8-4760-b2ab-3be9ccc32558",
      "name": "Subscriber Created?",
      "type": "n8n-nodes-base.if",
      "position": [
        -140,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "10a13ffe-eea9-4c39-83fc-82ada98ac937",
              "operator": {
                "type": "number",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 200
            },
            {
              "id": "8fedd56c-32c2-4b15-994f-c94584d0d263",
              "operator": {
                "type": "number",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 201
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "0c157ba4-1df6-4da9-90e7-073c6d328446",
      "name": "Send Email Alert (Beehiiv API error)",
      "type": "n8n-nodes-base.gmail",
      "position": [
        200,
        -20
      ],
      "parameters": {
        "sendTo": "={{ $('Configure Workflow').item.json.email_alert_recipients }}",
        "message": "=An error occurred while calling the Beehiiv API and the workflow has stopped.\n\nSubscriber affected: {{ $('Clean Data').item.json.email }}\nError status: {{ $json.body.statusText }} ({{ $json.body.status }})\nError message: {{ $json.body.errors[0].message }}",
        "options": {},
        "subject": "Systeme.io > Beehiiv Synchronization Error",
        "emailType": "text"
      },
      "typeVersion": 2.1
    },
    {
      "id": "fabbd076-4a33-4675-bf32-3da4c746b4d5",
      "name": "Configure Workflow",
      "type": "n8n-nodes-base.set",
      "position": [
        -1140,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "8d8477f3-229b-4273-ac12-b849befcc52b",
              "name": "beehiiv_publication_id",
              "type": "string",
              "value": "pub_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
            },
            {
              "id": "f4f80520-c182-45c0-81b2-00cd628e6ccf",
              "name": "beehiiv_firstname_field_name",
              "type": "string",
              "value": "firstname"
            },
            {
              "id": "a01fb97c-9bb9-4430-8fee-eb196d7a58be",
              "name": "beehiiv_lastname_field_name",
              "type": "string",
              "value": "lastname"
            },
            {
              "id": "253d55e0-f557-46cf-a69c-0e7a685bdd8a",
              "name": "email_alert_recipients",
              "type": "string",
              "value": "user@example.com"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f92affac-9a1b-4f0e-bf14-877b5fc5719b",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2200,
        -520
      ],
      "parameters": {
        "width": 540,
        "height": 1360,
        "content": "## Create New Beehiiv Subscribers From New Systeme.io Optins (Funnel Level)\n\n### \u2139\ufe0f About\nThis workflow automatically creates a subscriber in a given Beehiiv publication when a new opt-in is registered in a given Systeme.io sales funnel.\n\n**Good to know:** the integration with Systeme.io is done at the sales funnel level, not at the account level. If you have several sales funnels, you can use the same workflow several times.\n\n### \u26a1\ufe0f Quick Setup\n\n1. Configure your sales funnel in Systeme.io to [create and trigger a webhook after an opt-in](https://help.systeme.io/article/144-how-to-create-and-trigger-a-webhook-after-an-opt-in-or-a-sale)\n    - 1. Open the **\u201cOn New Systeme.io Optin\u201d** node to find the webhook URL needed to configure your sales funnel on Systeme.io\n2. Configure the \u201c**Configure Workflow\u201d** node\n    - Add your [Beehiiv publication ID](https://www.beehiiv.com/support/article/13091918395799-how-to-access-your-publication-id-or-api-keys)\n    - If you know the subscriber's first and last name and want to send it to Beehiiv, [configure the custom field names for first and last name](https://www.beehiiv.com/support/article/7712894720023-using-custom-fields-with-your-subscribers)\n    - Add one or more email addresses to which to send alert notifications in the event of a problem (separated by commas).\n3. If you have not already done so :\n    - Connect your Beehiiv account in the **\u201cCreate New Beehiiv Subscriber\u201d** node\n    - Connect your Gmail account in the **\u201cSend Email Alert (Beehiiv API error)\u201d** node\n\n### \ud83d\udd27 How It Works\n\n1. As soon as a new opt-in is registered on your sales funnel, Systeme.io triggers the workflow (via a webhook)\n    - Only requests actually coming from Systeme.io are considered (whitelisting of their IP addresses for security reasons)\n2. A new subscriber is added to your Beehiiv publication (via an API call)\n    - If available in Systeme.io, UTM tags (utm_source, utm_medium and utm_campaign) are transferred to Beehiiv to correctly track where your subscribers are coming from\n3. If an error occurs during the Beehiiv API call, an alert notification is sent to you (via email)\n\n### \u26a0\ufe0f Requirements\n\n1. A [Systeme.io](https://systeme.io/) account\n2. A [Beehiiv](https://www.beehiiv.com/) account with an active publication\n3. A Gmail account\n\n### \ud83d\udcaa Benefits\n\n- Automate & scale your email marketing efforts seamlessly\n- No more manual tasks to keep your subscriber list always up-to-date\n- Focus on creating a newsletter that stands out, not on the technical side\n\n### \ud83d\ude0d Check Out My Other Templates\n\n\ud83d\udc49 https://n8n.io/creators/belmehel/"
      },
      "typeVersion": 1
    },
    {
      "id": "219788ed-5d46-494f-822a-20c7279a0f6a",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1560,
        -200
      ],
      "parameters": {
        "color": 7,
        "width": 260,
        "height": 380,
        "content": "Automatically triggered with each new opt-in on your sales funnel\n\nOpen the node to find the **webhook URL** needed to configure your sales funnel on Systeme.io"
      },
      "typeVersion": 1
    },
    {
      "id": "09e191c2-2dc6-4d60-aeb2-aacfa794655f",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1220,
        -200
      ],
      "parameters": {
        "color": 7,
        "width": 260,
        "height": 380,
        "content": "## To configure\n\nSet the values to these 4 variables:\n- beehiiv_publication_id\n- beehiiv_firstname_field_name\n- beehiiv_lastname_field_name\n- email_alert_recipients"
      },
      "typeVersion": 1
    },
    {
      "id": "6a600c9d-c6d4-417d-a33a-d9d49b714fb4",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -540,
        -200
      ],
      "parameters": {
        "color": 7,
        "width": 260,
        "height": 380,
        "content": "## To configure\n\nConnect your Beehiiv account ([how to find your API key](https://www.beehiiv.com/support/article/13091918395799-how-to-access-your-publication-id-or-api-keys))."
      },
      "typeVersion": 1
    },
    {
      "id": "0af8282c-9ff8-44fa-a010-b59f6e202dad",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        120,
        -200
      ],
      "parameters": {
        "color": 7,
        "width": 260,
        "height": 380,
        "content": "## To configure\n\nConnect your Gmail account."
      },
      "typeVersion": 1
    },
    {
      "id": "5f56e65b-c56a-45e6-a7ef-434bb371f221",
      "name": "On New Systeme.io Optin",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -1480,
        0
      ],
      "parameters": {
        "path": "funnel-level",
        "options": {
          "ipWhitelist": "0.0.0.0, 0.0.0.0, 0.0.0.0"
        },
        "httpMethod": "POST"
      },
      "typeVersion": 2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "9e252eb4-eaf3-403b-9f57-3a56bd00e8e1",
  "connections": {
    "Clean Data": {
      "main": [
        [
          {
            "node": "Create New Beehiiv Subscriber",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Configure Workflow": {
      "main": [
        [
          {
            "node": "Clean Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Subscriber Created?": {
      "main": [
        [
          {
            "node": "Send Email Alert (Beehiiv API error)",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "On New Systeme.io Optin": {
      "main": [
        [
          {
            "node": "Configure Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create New Beehiiv Subscriber": {
      "main": [
        [
          {
            "node": "Subscriber Created?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}