n8n Advanced Course (2/8) - Advanced technical nodes
16:46

n8n Advanced Course (2/8) - Advanced technical nodes

n8n 20.06.2024 44 243 просмотров 616 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
⌨ Sign up to n8n Cloud for a free trial: https://app.n8n.cloud/register Or: npm install -g n8n n8n start *Resources:* ❔ Need help or want to discuss the course? Join the conversation on our forum: https://community.n8n.io/t/advanced-course-2-8-advanced-technical-nodes/48597 🛠 Workflow templates: https://n8n.io/workflows/ 📚 Documentation: https://docs.n8n.io/

Оглавление (6 сегментов)

Introduction

hey and welcome to video number two of the advanced course for NN in this video we'll be covering some of the more advanced nend nodes and how to use them in your

Expressions

workflows in the beginner course we covered Expressions um expressions are how we Access Data from items in a node expressions are just little bits of JavaScript that we write between Cur brackets you can easily get the expression Associated to a certain item by using the drag and drop feature as you can see here we're just dragging the invitations sent into the field in which we want the expression this will automatically create the expression to get that item you can of course also write your own Expressions between curly brackets the simplest use case for this is basic arithmetics for example subtracting the value of one item from another addition multiplication etc but within those cie brackets you can write any JavaScript within Expressions you can access previous node data but also use data transformation functions that are native to naden there are functions to deal with numbers dates strings and much more in this example we're using the extract domain function to get the domain from an email address so Maxim at acme. com the Domain is acme. com here are some useful examples of inbuilt functions we have the is empty that returns a bullet here are some useful of here are some examples of useful buil-in functions we have is empty that checks if an object has key value pairs we have has field which checks if the object has a given field we can also remove duplicates check the difference between two arrays or as we saw previously extract a domain from a string you can find the whole list of data transformation functions at the URL Below in the NN documentation when dealing with dates and times you can use the loxon library within your Expressions it's a JavaScript library that makes it a lot easier to work with dates and times for example you can use the dollar now expression to access the current date of time and in your workflows the dollar now will return the current date and time even when building automations you can create date objects with functions like from format and then turn them back into Strings with two string or two local string and it then nodes pass date objects as strings between them so you need to be careful to be converting these strings back into date object if you want to be making um Transformations on them with the lockson library so here you have a few examples daughter now returns a object with the current date and time daughter now to string makes it a little bit more readable with a string format today gives the date of today with a timestamp at midnight etc

Code

now that you have a better handle on Expressions we can take a deeper look into the code node lets you extend the flexibility of nen much further than its native nodes allowing you to execute a script or piece of code on your input items the code node allows you to write JavaScript or python that we will be executed on the input items and return them as output items or you can generate a whole new set of output items with the code node you must ensure that you are returning a new set of items or the node will return an error this means you must return a list or array of Json objects if you want a code node to return nothing you can simply return a list with one empty Json object as you see below we just return square brackets and then inside curly brackets the code node can be useful for replacing switch nodes or performing calculations across items sums averages distributions etc when using the code node we would want to access items from the previous nodes this works in pretty much the same way as Expressions we have builtin variables dollar input. all will return all the input items from the current node this will be very useful when iterating over all of the input items dollar Json works the same way as in Expressions allowing you to access the current item the code node is reading now as selecting the language python or JavaScript the code node has two different modes that it can use you can either decide to run once for all items in which case your code will have to Output one or multiple items in a list format so a list or array of Json objects or you can decide to run once for each item in which case the code node will be executed once for each item individually and you will have to return no longer a list of Json objects but a Json object a singular Json object which one to choose it really depends on what you're trying to accomplish for example do you want to find all of the duplicates run once for all items is probably easier

HTTP

the next node we're going to look at is the HTTP node we often describe it as Postman in a node as its name indicates this node lets you make HTTP requests or in other words lets you use apis the HTTP node has settings for every different part of the HTTP request header body query parameters URL etc these will all be found in the main node settings the advanced settings of the HTTP node allow you to deal with pagination timeouts and other common use cases for example if you're calling an API that gives you the URL of the next page in the response you can configure it in the pagination settings to make the HTTP node deal with pagination without having to create a loop this will say you a ton of time when building um API requests the HTP node also lets you use credentials from other nodes so you don't have to figure out how to authenticate the services this is often one of the most tedious parts of using an API you can just select the credential from the list of predefined credentials and use the HTTP node as you would for any other API this makes extending the access to services that already have native nodes a lot easier another cool feature of this node is the import curl or curl a lot of API documentations will give you the curl for a request and you can simply paste this into the node and the node will configure itself you can also ask Jack F GPT or other generative AI tools to generate the curl for you and save a ton of time setting up the node we'll look at an example of this later let's look at how

Code node

the code node Works in practice uh here I'm just going to drag the code node into a workflow and we're going to see that we have the two different modes so we have one once for all items and run once for each items when you add the code node we're going to have by default some code here this code just creates uh a new key in the input data called my field and Set uh to a value of one um so here we can see that if we're running once for all items it Loops o over all of the input and for each item is going to add the field so if I had 10 Fields here it would add the my new field equal to one to every item if I switch it over to run once for each item um this time the code is going to look a little bit different we don't need to iterate over every input value and therefore we can just set input item json. my Newfield 21 and return the item this has the same output if now we you know drag in some test data here I just have a Google sheet with some um information on client orders we can run through an example of how to use the code node here I have in my Json uh a bunch of different orders uh order value order count total order value and what I'd like to figure out here is what is the total of the total order value so for every item I want to sum the total order value and get the result so what we're going to do here we're going to keep the loop first we're going to create a variable called total set it to zero we're still going to Loop over every item except this time we're going to say total plus equals item. json. total order value then uh here if I execute this it won't do anything useful because I need to return the total here because we are in the Run once for all items mode I need to return a Json object so here I can simply return total and return my variable total here this will return one item that shows the sum total of everything let's say here I was coding and I had made a little mistake and I had forgotten my plus uh here this obviously would have been the wrong value it's clear that this value is 55,000 the total cannot be 23,000 what we can do here is use the um console because everything that happens in nen is Javascript we can use the console to sort of figure out what's happening so here clearly I have a problem somewhere in my um Loop so I could console. log the value of total and see if it's evolving over time when I test the step I'm going to see the different values here it would be quite easy to recognize 55,000 83,000 these are simply the values of total order value looking back through my code I can see we're missing a little plus if I run it again this time we'll see the value that keep going up now going uh into another example for run once for each item I could um let's reset the code node I could say I could be interested in uh because we have the total order value and the order count I could get for example the um average order value for every person um in this case because all of the data is within the individual item it's just a lot easier to write um some code for run once for each item in this case I could get the input. item. Json this time I want the total order value and I want to divide it by the order count so here I can put there um average value equals delete this line and then set the new field and then just return the full item here if I execute I can see that uh was returned 6,000 9,000 2,200 uh these seem to be the um values for total order value divided by order count so that's for the code node um as mentioned in u the slides you can use Expressions variables any anything that you can use in expression the code node um as well as all of the built-in functions that are going to help you save a lot of time now I'm going to show you how you can use the import curl to easily set up your HTTP node when an API reference has a curl example so here I'm just going to import a new HTTP node and here I have an A documentation for a search on people data labs and I'm just going to copy this uh curl from here I can import it uh just paste the data um obviously here the API key uh is given as a header but I have a uh predefined header or directly here and uh because I'm going to be using uh the generic credential type I don't need to be sending headers but as you can see here um importing the curl is going to import all of these different fields already formatted correctly into your HTTP request um and now we can just uh execute I'm just going to set the size to one to use a few less credits and now I can just test the step um the HTTP uh request went uh as planned uh I returned one location Mexico help phone numbers and it returned a single person so everything went as EX expected thanks for listening to the

Outro

second video of the niden advanced course where we covered Expressions the code node and the HTTP node in the next video we'll be covering pinning data and editing outputs

Другие видео автора — n8n

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник