I tried automating reminder email registration using the free workflow automation tool 'n8n'



GIGAZINE regularly runs donation campaigns, and for those who 'want to donate but don't have time right now,' we have a system in place that sends reminder emails when they register their email address. We use

listmonk to send reminder emails, and because the API is public, email addresses can be registered externally. However, the listmonk API does not support what is known as UPSERT . Therefore, we used n8n, a free and open-source workflow automation tool, to handle the process of 'registering an email address if it's not registered, and updating it if it is registered ,' enabling UPSERT support without complex coding.

First, let's briefly explain the concepts you'll need to know to use listmonk.
Subscriber : The person to whom email is sent
List : This is like a label that indicates what kind of email to send to subscribers. In this case, it is 'Reminder Email.'

There are three APIs we will use this time. The first is an API that adds a specified email address as a subscriber.
[code]
POST /api/subscribers
[/code]


The second is an API that retrieves information about registered subscribers.
[code]
GET /api/subscribers
[/code]


The third is an API to add lists to registered subscribers.
[code]
PUT /api/subscribers/lists
[/code]


To elaborate a little more on the 'UPSERT is not supported' issue mentioned at the beginning, if you specify an existing email address as a parameter for 'POST /api/subscribers', an error ' HTTP 409 Conflict ' will be returned. On the other hand, from the perspective of the API user, if there is already a subscriber with the specified email address, you would like that subscriber to be added to the 'Reminder Email Recipient' list. Therefore, to achieve the desired processing, you must take one of the following steps.

1. First, get the subscriber's information, and if it already exists, add the list to the subscriber. If not, add the subscriber.
2. First, add a subscriber. If an error occurs, get the subscriber's information and add the list to the subscriber.

The former requires two API calls, whereas the latter requires only one API call if the subscriber is not registered, but three if they are registered. From the perspective that it is better to call the API as little as possible, both methods have their pros and cons, but in the case of reminder emails, we will adopt the latter method, assuming that it is probably rare for the subscriber to already be an existing subscriber.

The result was a workflow like this.



Below, we explain what each node in the workflow does.

◆Webhook node
When the workflow is called in Web API format, it is received at this node. It receives an 'email address' as a parameter.



◆Code (array value setting 1) node
This is a bit of a hack.

POST /api/subscribers requires that an array value of the list ID be passed for the subscriber list. However, if you try to pass an array value as a parameter in the HTTP Request node normally, for example, even if you try to pass an array value such as '[1, 2, 3]', it will be interpreted as the string '[1, 2, 3]'. Therefore, in order to pass the array value correctly as an array value, you need to set it in the JavaScript code. The JSON value is also set here in the same way.



◆ Variable setting node
This sets variables that can be used within n8n workflows, except for array and JSON values, for the reasons mentioned above.



◆HTTP Request (Subscriber Addition) Node
I am calling an API to add a subscriber.



◆IF node
The next node to transition to is selected depending on whether the condition is true or false. The set condition is 'whether the HTTP status returned by the API to add a subscriber is anything other than HTTP 409 Conflict.'



◆Respond to Webhook (Subscriber Addition) Node
This is the destination of the transition if the IF node's condition is true. The response from the API that adds the subscriber is returned to the webhook caller.



◆Merge node
This is the transition destination when the IF node's condition is false. The input route from the IF node is 'Input 1', and there is also a separate input route 'Input 2' from the variable setting node. The setting for this node is 'When input is received from both input routes, output the input from Input 2 as is'. In other words, the input from Input 1 is merely a trigger.



◆HTTP Request (Subscriber Search) Node
I am calling an API to get subscriber information.



◆Code (array value setting 2) node
This performs the same processing as the Code (Array Value Setting 1) node described above. However, since the parameters of the API being called are different, they are set again.



◆HTTP Request (Add subscriber list) node
I am calling an API to add a list to a subscriber.



◆Respond to Webhook (add subscriber list) node
The response from the API that adds the list to the subscriber is returned to the webhook caller.



This is how the reminder email registration process is implemented. GIGAZINE's server operation continues to be in a difficult situation, so if you can support us, please make a donation on the 'About GIGAZINE' page.

About GIGAZINE

https://gigazine.net/news/about/



If you would like to sign up for email reminders instead of donating now, scroll down a bit to the footer and click the 'No, I'll do it later if I have time' button.



A pop-up will appear asking you to enter your email address, so please enter it and click the 'Send' button.

in Software,   Review, Posted by log1c_sh