Make API Calls with Node-RED | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be showing you how to make API calls using node-red using the HTTP request node I'm going to go through two examples I'm going to show you how to retrieve Formula One driver standings and then in the second example I'm going to show you how to connect to the switchbot API [Applause] in the first example we're going to be calling a free to use Formula One API we're going to be getting driver standings but there's also other information available so let's dive in all right so now that we're in node rad let's filter by HTTP and you can see here this bottom one is the node that we're interested in so the first two httpn and HTTP response is if you're actually going to create a web server where things externally can send requests to node-red and the node-red can respond whereas for this video we're actually going to send web requests out to the internet and get a response back so that's this node here so we can drag this onto the palette so if you double click into the load then we can see all the options available so you can see that you can do https requests which most requests these days should be that especially if it's an API call you can do authentication so you can do some basic mechanisms like basic course digest and Bearer but if you need to do anything more complicated which I'll show you in this video then you need to do some separate custom functions and pass that in separately I'm not entirely sure what enable connection keep live does to be honest use proxy means that you can go through a proxy server so that's usually if you've got a more complex internet setup so like for businesses they would have a proxy in the middle being at home you probably don't have a proxy in place the next one is interesting so basically responses that are 200 or 201s Etc usually means that it's a successful request whereas if you get a response back that's not a 200 then it's probably a bad request for example a 400 so if you get a bad request back then it will go to a catch node and then you can do something with that error likewise I'm not sure what disable strict HTTP passing is if we look over to the right here you can see that it does have a lot of information about what you can do in the node so these are all the inputs you can have so the inputs are basically nodes that come before this one you can pass in certain variables as an input scrolling down here we can see the outputs so this is basically what is actually returned back from the server that you made the request to so the response headers and the response body the payload which is the most important thing generally the response URL response copies and a redirect list so generally you're just going to be interested in the payload that is returned and probably the status code so the first thing we're going to do is I'm actually going to install a new palette and this allows you to store credentials securely within node-red so if we go up to the hamburger menu manage palette and then we can do install and then if we search for this node-red country credentials you can see here it says it allows you to store credentials so let's press install and there we go it's installed Now searching by credentials you can see there's this here and if we double click in here you can see that you can add credential values so this is the private value and then this here is where it goes in the message we'll have a look at this node in a bit more detail later on okay so back to the HTTP request node so for our first example what I'm going to do is I'm going to retrieve the driver standings for the Formula One championship this is an unauthenticated API that's available for free online so let's take a look so here's the website and you can see it's fairly basic but if I search for Json then it tells us here that any of the URLs it gives if you append dot Json to the end then it should return some Json so let's have a look so we're going to go to standings and you can see here drive standings so if we press this then you can see it returns it in a HTML page but if we add to the end of this dot Json then it should return it in Json and there we go so this is what we want to call in node-red so let's copy this URL and then go back to node red so if we double click into this node and you can see this URL here we can simply paste the URL into there and then we can see it's a get request which is what we want because we're not sending any data simply press done and now we're going to take an inject node so that we can trigger this let's put this here and Link it up and then we're going to take a debug node so that we can see what the output is so debug link that to that I'm going to double click into this debug node and click here and do complete message so we can see everything that's returned if you leave it as it is like this message.payload then it will show you the response of the message that we saw over here so this but it won't show you things like HTTP response codes whereas if we do this complete message now let's give it a go so we're going to deploy this go to the debug tab here on the right hand side and now we press this button here there we go that was super quick that's expanded out so you can see payload that's got all the data status code 200 here so this is what we want to check to make sure that the request is actually being successful and then this has got some Header information as well which you're probably not interested in but you might need under certain circumstances so the first thing I noticed here is that this is a Json message but this actually isn't expandable it's just as text so what we want to do is we want to have this in Json so we can actually manipulate this data so I'm going to add a new node so that we can convert that so I've just searched for a Json node here and we're going to add it between the debug node and the request so if we just drag it and put it over there it automatically puts it in between double click and then you can see convert between Json string and object so we'll figure it out for you basically on this setting and then message.payload that is actually what we want to convert so that's okay as well so let's click done deploy let's minimize that message press it again and now let's see what's happened payload and here you go you can see now you've got a Json structure so you've got this Mr Data and then within that you've got more information so we can see all the information here I've actually called 2008 season which is not ideal so let's change that to this year so what we want to do is we want to take this URL instead so now we've ended up with this url.json let's see what that does I'm going to remove this deploy let's try again okay so this looks better so season 2023 and we should see Max verstappen at the top of the driver standing so let's see if that is the case and there we go it shows his permanent number it shows that he's the first object in the array so he's first and it gives quite a bit of information about him actually so now we can do whatever we want with this data so let's just say that we wanted the number of points that the person's got so we can then press this button here which gets the path of that Json so loading this up in notepad we can see that it shows payload.mrdata dot standings table dot blah blah blah blah so that is the path within the Json to get that specific piece of data so I think the next logical thing to do now is to catch an error scenario so if the API call fails for some reason what do we do so let's scroll down on here and you see there's a catch node here let's go back into this HTTP request and we can tick only send non-200 responses to the catch node and then drag a debug mode again I'm going to rename this and say there was an error double click into here and then instead of all nodes we can do selected nodes so you see there's HTTP request node so let's press that so if there's an error then it should come to this debug node here so let's run that let's deploy and you can see it's just returned a message from here nowhere from here because it was a status code 200 so to make an error let's go into here and let's just add another m to the end of this URL hopefully that will come up with an error deploy here we go we can see there was an error so you can see that this here is not found the URL and so it's returned an error through to this catch node and it's called that error at the moment here it's just returning the timestamp but at least you know that you need to resend that request because it's failed for some reason or another so if we were doing this properly we would probably want to combine this catch node with something like the counter node so if we had a counter then we could get it to retry so many times before stopping and we might want to use something like the delay node where we can do rate limiting so if you go into this node then you can do rate limit and you can say how many message you want to send per period and then you can also drop messages in between so you could say only retry within a 10 second period or something like that I'm not going to show you exactly how to set this up here because I want to go through some more API examples instead so in this example we're going to be calling the switchbar API if you don't know what switchbot is they're a company that makes a lot of home automation products so they do things like smart locks smart blind controllers and much more so they've got an API available the API isn't local it's a cloud-based API unfortunately but you can use it to retrieve information about your devices and also control them so in this example we're going to just get the list of devices but you can also control them like unlocking or locking your front door so let's take a look okay so now we're back into node red let's do plus and add a new flow double click it and we'll call it switchbot so the first thing we're going to do is add an inject node so that we can actually trigger this the next node we want is a function node which is going to be doing a lot of the hard work and what we also want to do here is we want to create a unique identifier for every API request so to do that we're going to need to add another new palette so go to the hamburger menu again manage palette install and this time we're going to install random generator node red con trib install there we go it shows you which new palettes are available so uuid is the node that we want so let's drag that on and if you double click into this you can see there's nothing available as an option it will simply generate a new guide for you every time it runs through this node so the next node we're going to want is the debug node again to see what the response is so we can then call these up so we need to of course add in the HTTP request node as well so let's drag that and add it here so basically from left to right we are going to call it by pressing this button obviously normally you'd get it to do it on a schedule or something like that uuid it will generate and it will store it in message.payload the function node we're going to use to generate the header which is going to be used for authentication called the switchbar API HTTP request is going to do that actual request to the switchbar API and then the debug node is going to give us the results so we can see if it was successful or not so opening up the function node there's a couple of things we need to do first so we need to go to setup first and then we need to go to add here and we need to add a module called crypto which is a cryptographic function and what this means is we actually call it with the variable crypto so let's go to on message and this is where we need to put all the code so I'm going to paste the code in and I'll have that in the description so walking through this we can see the first thing it does is it generates the current date into a variable T we then have the nonce which is the uuid from here so we've got message.payload saving it as that and then data is the token which is basically part of our API credentials we've got T that and then we've got that so that's the format it needs to be in for the switchbar API this varies per API you've got to look at their documentation and see what you need to do so now we've got here we've got creating this hmac so we've got a hmac function which is in this crypto that we set up and then we need to pass in the secret so the token and the secret are the things that we need to pass in which are our credentials so we haven't done that yet and then it changes it to utf-8 and then does a digest and then it gets base64 encoded and then gets finally added to the header so the missing part of the jigsaw here is the token and the secret so to add that we're going to use the palette that we added earlier so credentials let's drag that on so what we want to do here is is we want to add this to the flow so clicking back into this function node we've called it token and secret so that's what we want to call it in here so double click this and then let's do add and message dot token is one of them add again message dot secret so then we need to add the credentials in here and then hopefully we should be good to go to use them in here so now that I've added the credentials into here we need to go back into the function node and make sure we actually use them right so I've added these two lines now which will pull out the token and secret from the message so now we need to go into this HTTP request node and actually add the URL so this is a get request it's a secure connection so we need to make sure that we've got that so we need to add a secure connection so we don't really need to do anything here we just leave it all as it is and press add press done let's press deploy now let's run it and see what happens and there we go we can see that we've actually got a successful result back from the switchbot API it's listing all of my devices here I've obviously had to blur a lot of it out unfortunately but trust me it's showing the right information so just like in the first example we need to actually convert this string into a Json message so we use this Json node again and we can drag that in and add that to here so doing that again you can see now that it's in a proper structured message you've got status code of 100 and you've got a success as the message and then the body is here now it's got the list of devices the three devices and then you can expand these out in my case you can see here that I've got a hub mini I've got a front door keypad and a front door Smart Lock so hopefully these couple of examples have helped you get started on calling apis to get the information you need every API is different in some shape or form so you're going to need to probably experiment a bit yourself there's different types of authentication different messages some use Json some use XML but hopefully this has given you a good foundation to get started and leave comments below if you've got any questions well that's it for today so thanks until next time [Music] [Applause] [Music]
Info
Channel: Let's Automate
Views: 2,838
Rating: undefined out of 5
Keywords: nodered, node red, api calls, switchbot, switchbot api, ergast api, ergast
Id: KqtxtPkmoik
Channel Id: undefined
Length: 16min 20sec (980 seconds)
Published: Thu Jul 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.