How to send a single API request with one HTTP node execution, but an array of parameters in it (like emails[all]?)?

Hi!

I’m still in the beginning. Now I need to make an API call via HTTP node, and send all static parameters, but with the array of emails parameter (named it wrongly just to show what I need {{ $json.email[all] }}). Would really appreciate your help, as long as kind really understand this from docs. Thanks!

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@Dan_Burykin , I can suggest 2 approaches:

  1. Fixed expression you composed (option 1)
  2. Redesign of your approach (option 2)

In both cases, it is assumed that by $json.email[all] you mean an array of email addresses as in ["[email protected]", "[email protected]"].

Option 1

The amended expression looks like this

{{ 
{
   "emails":[
      {
         "email": $json.email,
         "variables":{
            "Prediction": $('Sagittarius').item.json.text,
            "Prediction date": $evaluateExpression($('Schedule Trigger').item.json.Month + ' ' + $('Schedule Trigger').item.json['Day of month'] + ', ' + $('Schedule Trigger').item.json.Year)
         }
      }
   ]
}
}}

Note the whole JSON is an expression, not the separate values. It does impose some complexity. When you need to combine a few values into a single string, you have to use the n8n convenience function $evaluateExpression() (not sure if there is a better way).

Option 2

This solution makes the JSON cleaner and easier to understand which is why it is my recommendation. It relies on having all the values getting prepared in advance, which is done in “Preparation” set node. It also does not require usage of $evaluateExpression().

The expression for this option becomes much simpler

{{ 
{
   "emails":[
      {
         "email": $json.email,
         "variables":{
            "Prediction": $json.prediction,
            "Prediction date": $json.prediction_date
         }
      }
   ]
}
}}

Here’s the actual workflow.

Let us know if my understanding of your initial data and the general workflow structure is different from what you have which would make the proposed solutions not suitable for you.

Thanks so much for your reply, @ihortom ! I’m so sorry that I’m really even in explaining of what i need. So that the previous node sends something like this:

[

{

“email”: “[email protected]”,

“status”: 1,

“status_explain”: “Active”

},

{

“email”: “[email protected]”,

“status”: 1,

“status_explain”: “Active”

}

]

and so if I then make an API call via HTTP node this way:

{
“emails”:[
{
“email”:“{{ $json.email }}”,
“variables”:{…

It tries to run node two times, which obviously cause API error. If I limit node running to a single time, it simply will only send one first email. While I need to send this in a single run:

{
“emails”:[
{
email":"[email protected]”,
“variables”:{
“name”:“Elizabeth”,
“Phone”:“380632727700”
}
},
{
email":"[email protected]
}
]
}

as well as another questions with regards to the same suffering workflow when I pull this kind of array:

[
{
“email”: “[email protected]”,
“phone”: null,
“add_date”: “2024-08-01 13:10:29”,
“status”: 1,
“status_explain”: “Active”,
“variables”: {
“Timer 0…23 ET”: “0”,
“Sign”: “Pisces”
}
},
{
“email”: “[email protected]”,
“phone”: null,
“add_date”: “2024-07-30 00:02:52”,
“status”: 8,
“status_explain”: “In blacklist”,
“variables”: {
“Name”: “[email protected]”,
“Timer 0…23 ET”: 11,
“Sign”: “Capricorn”,
“Prediction”: “…”,
“Prediction date”: “…”
}
},

{
“email”: “[email protected]”,
“phone”: null,
“add_date”: “2024-07-22 15:30:51”,
“status”: 8,
“status_explain”: “In blacklist”,
“variables”: {
“Name”: “[email protected]”,
“Gclid”: “55555”,
“Sign”: “Cancer”,
“Keyword”: “astrology”,
“Timer 0…23 ET”: “0”,
“Prediction”: “…”
}
}
]

I then need to convert only emails to sting of Email addresses separated by commas and encoded in Base64. Currently I’m converting them to files and then files to base64 string, but not sure if it’s optimal a speccially like for 500+ emails/files…

Thanks so much again!

I mean non of the emails as well as variables are static. They’re also pulled for HTTP API nodes.

what of aggregate all data?

Sorry, @Imperol I’m not sure I understand the question. The purpose is to send daily horoscopes I get from my AI API on the specific sign and hour of a subscriber. I use API calls to bulk mailing tool for both storing clients data (emails and variables) in the service mailing list, to run mailing campaigns with it as well as every time add and delete selected emails to/from black list (to bypass segments limitation): https://sendpulse.com/integrations/api/bulk-email

Thus, through this single workflow there are different API calls with HTTP node, that send/get slightly different data stractures. My main complications are:

  1. Send to API array of emails I get from the same previous API call and their variables taken from same and another API:
  1. Get emails form API call and add them to black list as a base64 encoded strings:

@Dan_Burykin , thanks for adding more clarity. If you do not mind, let me extend it even further with more details as it is important for understanding of what you are trying to achieve and how that could be completed.

For that, let’s first concentrate on item 1 as it is what you started with and added item 2 later on. The item 1 does not require item 2 - they are separate topics although related.

Item 1: Send array of emails with variables

Send to API array of emails I get from the same previous API call and their variables taken from same and another API

When checking your workflow I have concluded the following:

  1. “Aries” node: this is in fact the call to the chatbot which provides horoscope for the people born on a specific date.
  2. “HTTP Request1” node makes an API call to Get all of the contacts in a mailing list by variable.
  3. “HTTP Request2” node updates the address book as per Add emails to a mailing list (single-opt-in)

According to the API description related the last node, the address book could be updated with email addresses in two forms: with and without variables. Adding email addresses without variables was touched by me to some degree. What is needed is the array of emaul addresses only as in

{
   "emails":["[email protected]", "[email protected]"]
}

However, you seem to require the emails with variables in which case the POSTed data is in the form

{
   "emails":[
      {
         "email":"[email protected]",
         "variables":{
            "name":"Elizabeth",
            "Phone":"380632727700"
         }
      },
      {
         "email":"[email protected]"         
      }
   ]
}

I already mentioned that “Aries” node produces a single text - horoscope advice. The “HTTP Request1” essentially node gives you email addresses:

[
    {
        "email": "[email protected]",
        "status": 0,
        "status_explain": "New"
    },
    {
        "email": "[email protected]",
        "status": 1,
        "status_explain": "Active"
    }
]

You did mention you make another chatbot request which presumably provides another set of variables as in

[
  {
    "email": "[email protected]",
    "phone": null,
    "add_date": "2024-08-01 13:10:29",
    "status": 1,
    "status_explain": "Active",
    "variables": {
      "Timer 0…23 ET": "0",
      "Sign": "Pisces"
    }
  },
  {
    "email": "[email protected]",
    "phone": null,
    "add_date": "2024-07-30 00:02:52",
    "status": 8,
    "status_explain": "In blacklist",
    "variables": {
      "Name": "[email protected]",
      "Timer 0…23 ET": 11,
      "Sign": "Capricorn",
      "Prediction": "…",
      "Prediction date": "…"
    }
  }
]

This request is not depicted in your workflow. Thus, I would have to make some further assumptions here as well. I assume that your workflow(s) use either the horoscope text as a variable (presumably prediction with the prediction date) or the data from the “missing” chatbot node as a variable but not both at the same time for the same email address.

I am going to narrow down the requirement further to make sure we are not mixing up different issues. Here’s my revised solution to the item 1. As a reminder, the endpoint (“HTTP Request2” node) accepts the data in the format

{
   "emails":[
      {
         "email":"[email protected]",
         "variables":{
            "variable 1": "value 1",
            "variable 2": "value 2"
         }
      },
      {
         "email":"[email protected]"         
      }
   ]
}

As we are dealing with the 1st chatbot, the variables would be “Prediction” (aka horoscope text) and “Prediction date” (date given to the chatbot).

Here’s the revised solution

I left the node names unchanged so that you could correlate them with your own workflow you shared. The JSON POSTed in the last node now has the expression

{{
  { "emails": $json.emails }
}}

which translates into JSON POSTed to the API in the form

{
  "emails": [
    {
      "email": "[email protected]",
      "variables": {
        "Prediction": "Some text",
        "Prediction date": "August 2, 2024"
      }
    },
    {
      "email": "[email protected]",
      "variables": {
        "Prediction": "Some text",
        "Prediction date": "August 2, 2024"
      }
    }
  ]
}

Hopefully I got it right this time.

amazing. this is exactly what I needed. Thank you very much, @ihortom Just wonder how to make this 12 times for each zodiac sign w/o just replicating them))

Here’s how

I assume that all the horoscope signs’ predictions go to the very same mailing list.

To reuse the same workflow for all 12 signs, you just need to add 2 more nodes, namely “Horoscope Signs” that holds the list of signs and the Loop node to go over each sign individually (needed to obtain the email addresses of the owners born under the specific sign). Then the aggregation is executed on the “done” exit of the loop forming a single HTTP request to compose the mailing list.

I also renamed the original nodes (since we are already on the same page with me understanding the requirements) to reflect what it is they actually do.

2 Likes

Amazing. Thank you so much once again.

@ihortom . this is priceless to I have an example how to work with aggregate, edit and loop nodes! I promise, it’s the last question in this thread. I need to encode base64 string of not the following kind of json string:

[
{
“email”: [
[email protected]”,
[email protected]”,
[email protected]
]
}
]

but like this:

[email protected],[email protected],[email protected]

can’t really find how to transform it.

Thx!

Hi @Dan_Burykin

Would be best to open a new topic. :slight_smile:
But to answer your question:

{{ $json.email.join(‘,’) }}

@BramKn , computer says invalid syntax…)

@BramKn . I partly fixed it, because I saw in the node example, I should put it like this: {{$json.email.join()}}

But now it give this kind of error:

The value in “[email protected],[email protected],[email protected],[email protected]” is not set

Hi @Dan_Burykin

You are inserting it in a field that expects a fieldname.
Also it is not set to be an expression so that breaks it as well.

So you need to put it in a set node before this node and then set the fieldname you use in this node.

I recommend you checking the courses. As it seems you are missing some understanding of how n8n works. :slight_smile:

@BramKn has already gave you the answer. However, I would like to make a step back and give you more expanded explanation that would help you built workflows on your own.

The data passed between the n8n nodes are typically has the following form

[
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  }
]

I showed you how Aggregate node could be used to transform the above into

[
  {
    "email": [
      "[email protected]",
      "[email protected]",
      "[email protected]"
    ]
  }
]

which could be further transformed into the string of email addresses.

There are other approaches you can take depending on your starting position. Below is the workflow demonstrating them. Pick the one you like or suits your needs best.

Note to convert the string to base64 encoded value, you can use the n8n function base64Encode() as in {{ 'string_of_emails'.base64Encode() }}.

2 Likes

Thank you many times, @ihortom . Whenever you have time, kindly take a look at my another question (another project) please: Proper CSS selector to parse HTML content

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.