REST API Calls in Power Automate - Beginners Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we create a new flow to do our rest API calls in so I click create choose an instant Cloud flow it will work in any flow type but it's more easy to test in this manually triggered flows so then I click create we need an API to connect to you can see a rest API called as a Google search you do a request for example on cat pictures and then you get some results back in business standards it's usually payroll system crns CRM system that we want to connect to and grab some customer data or even update them for today you can go to the course page the link is in the description here the API address I'll click that this is the data that we are automating today we will treat them as customer data although they are Star Wars data here I want to look up customer IDs and then get these information back for example name height mass and birth here you can see this format it looks a little bit weird but it's not it's very efficient to send data to this is the response that we get back this is a Jason a Json you can see that with the curly brackets in the start and at the end and then you'll have a lot of key value Bears the key here is the name then the value is Skywalker the key here is height and then the value is 172. similarly we have some arrays down here against the films um this person will have five five films attached to him let's go down here and grab this URL I want to show you something cool so I open up a new tab and then I paste in the address this is also known as an endpoint because a rest API call uses a URL to perform the request and this is the response that we get back down here and let me show you the very cool thing that is if I change this one up here to 11 I click enter then we will get another customer pack so we can use this URL make it Dynamic to do multiple rest API calls and update a customer database in our case it will be in Excel sheet let's go do that so I go to Power automate again I'll click new step and here I'll search for an HTTP the reason here is that the rest API call is often known as an HTTP request and here I'll choose the HTTP like this the method here you can choose between get pot post patch delete usually you want to do a get sometimes you want to do a post that is if you want to update data in the actual database or delete if you want to delete a record I'll just get the URL we actually had that that was this one now click save and we go test the flow so I click test I'll do this run the flow click done and it ran successfully open up this scroll a little bit down first of all we have a status code of 200 that is usually just a success so well done and then this body this is the result that comes back so I just search for number one this was the address and then I get the customer data back I can use that in my flow let us just Mark everything Ctrl C Ctrl a Ctrl C to copy this we will use it in a little while but first let me just show you two things again I can do another search if I just search for person 11. I'll do this and here you can see that we now have Anakin Skywalker if you didn't copied out the Json the body before please do it now we will need to use it it doesn't matter so just Ctrl a Ctrl C let me also show you another status code we saw only the successes that is two this number 17 doesn't exist for some reason that could be if you looked up a wrong customer number on your API that is in your system then you will see my flow failed let me open it you can see I have now have a status code of 404. and the body says detail not found we also want to do some error handling here in our flow so let me click edit the first thing we will do is that we want to have a new step and here I can have a compose and I want to interpret the data that I got up from here so one way to do it you can see I can get the entire body but I actually want to go a little bit deeper let's say I want the name out for example the height and the mass then I will do an expression and here I'll say I want a body and then I want to go into the eight ttdp action then I want to move a little bit down because I want to grab the name that was a key in the actual Json that will look like this so hard brackets single quotation marks name I'll click OK and the expression is up here let us just fix this because this 17 doesn't exist so let's just go with Luke Skywalker again I'll click save I will test it manually I will run the flow I'll click done and down here in the compose you can see that we now were able to grab the look Skywalker very easy but we had to write this expression let's do it in a much nicer way so I'll just click plus between the HTTP and the compose I'll click add an action here I will have a parse Json the format that we were used that was actually a Json so here I'll take pass Json the content that is the body that comes from up here that those ones are the results we now need to generate a schemer that is how our Json looks so I click generate from sample and here I'll press Ctrl V because I copied one of the results from before I'll click done now it is it will be very easy I just go down to compose I delete this one here and now you can see that I can just I can just hard code in I can say name then I can grab the name from the dynamic content I'll do a new line and here I can say hide I will not do each one of these but this is so easy to use and here I can say Mass do this let's try to save it and see how easy that was and it actually that we can succeed so in the compose you can see here now I have the name look Skywalker height 172 Mass 77. similarly I could replace it up here let me just edit it could replace this with a 10. and perform the actual HTTP request or rest API call so here and let me just save this run it click done and again here you will have Obi-Wan Kenobi height 182 and mass 77. now we want to do a dynamic solution we don't want to set an update one item so let's look at some data here I have my course site click the download the Excel book that will take you to my OneDrive click the three dots and click download we will place this on our desktop just to inspect it so I'll move it out here double click to open it so this one is a sheet with a lot of customer IDs I want to update the name the height the mass the birth and I'll write a log message I'll also want to do error handling remember the id17 didn't exist so we want to combat for that and let's go create it so what I will do here close this I will need to go to my OneDrive because I need this Excel sheet where we do these requests from I'll need to place that in either one drive SharePoint Dropbox or box it needs to be accessible in the cloud for power automate I'll go to my root of my OneDrive folder I'll go into live and here I'll have my customer data so if you you should do the same it will look like this let me just delete this and you just move your data over like this so now we have it here I didn't do anything with it the important part here is also that these data they need to be formatted as tables otherwise we can access it access it either so now we have it we go to Power automate and we click edit the first one we will get rid of is the compose we will not have it printed out here we want to print it back to excel so I click delete what we will do is first we need to grab out the Excel data so in the beginning before the eight TTP I'll choose add an action here I'll say list rows present in the table what this does it get us all the rows in the table of the Excel book so I'll click this drop down the location that is OneDrive for business the document library that is OneDrive the file well I placed that in my live folder where you placed it is up to you just go find it now choose customer xlx it's formatted as a table so I can find it here then we need to iterate through it and that is because we take each row one by one and then we perform a an HTTP request so go up here then you will have an add an action I'll find and apply to each and what this does is iterating to a collection one by one so I want the output from this so I go in here and then I'll take the value now I need to move these two in here and because these two are dependent on each other what I will do is that I'll just open here and make it not dependent for now of the HTTP then I can move the HTTP in and I'll move the path Json and I'll need to have the body back here so now I for each one of these rows I'll do an HTTP request and I'll also do a Json path but if I did it here and I'll open this one here you can see I will do a request for person number 10. what I will do here is that I'll delete this and then do a request on the ID so that is the dynamic content that is why we got the Excel data now I will do Dynamic rest API calls I will pass them just like here that will work fine but after we are done we want to update the Excel sheet so let me just do this so for each one of these calls I'll have an add an action I'll find update a row and have it in here again I'll need to find this Excel book it would be nice if it just remembered or if you have we had the option to sadly it doesn't but that's okay we will practice our skills I'll go find all the data and again the table and now we need to specify a key column and a key value the key column and that comes from here that is the idea then I want to say and because these are uniquely named I can find it and let me just open it over here you can see that this is a unique column that is key values and this is clever this could be customer ideas or social security numbers so and what I want to do let me go back here I want to match them to this list row that is the value that I'm iterating to so I know that each data will get into the correct row so what you will do here is to scroll a little bit down and choose the idea now I can fill things in I don't want to fill in the ID what I want to do is that I want to fill in a name so that will be this one here I have the height I'll fill that in the mass as well I'll fill that in and the birth that will be a little bit down below we could have taken each one of these customer data we also have a lock but we will wait a bit by updating that now let's go test it and see that we can actually update it and we also need to do a little bit of error handling remember we had the id17 that one gave us an arrow what will happen here when it has ran in a little white is that number 17 it will it can perform that and it will jump to the next item so we'll need to make some good locking and we'll talk about best practice in logging the results of these rest API calls but I think this will take probably like a minute or something but imagine that you will have to do these um in a system where you had to look up in the actual uis this is much faster and here you can see we have an arrow let's open it so this one went fine you can see fine fine so I go to next fail that is number 17. and because uh this number 17 failed it up here in the HTTP it has not performed the past Json the updated row if I go in here we will get the status code 404 not found so and if I go back here to my actual results you can see that we now have all the data we have the names we have the height some of them are unknowns but that is just because in our system they are our announced remember the wilhofs then we'll have a mass and a birth here you can see room this 17 hasn't got updated so let's do some nice error handling and talk about what we need to do here to do this I will just delete all the data so we can see that it updates afterwards so now I click edit and what I need to do here is that um I will look at the status code and that is because I get a status code from this a to GDP I can get a 200 that was a success but I could also get a 404 that was an error so what I will do here is that right after here I'll click Plus I'll click add an action and here I'll choose a switch I could choose a condition but the switch is nice because then I can combat all the error codes that I can get from my HTTP we will only do it for these two ones here but you can fine do it for more status code choose a switch the switch works as I can give an expression and then we can say if that um this if that value is equal to something then we'll do this otherwise we can add another case another case and we can have a default case but let's take it step by step up here I will choose the status code from the http so that was empty now I can say if this one equals 2 200 that was a success and let me just rename this case then to case to 200 success then I want to do something so what do I want to do here I want to pass a Json um because now I know I have some data to pass out I also want to update the row so I move it in here and here let me just also update um this row here so I'll just rename this update of row and I will say 200. success here as well let us open it now we can update the log message and here I can just say success so now we have done everything if this actually equal to 200 so I can add a new case plus this case will be my 404 not found so I'll say 404 not found these namings those are just best practices that is if we come back or your fellow developer come back we can easily see what's going on you don't have you don't have to add them to make it work but it looks nicer and it is nice so now I say if this status code equal 404 what do I didn't want to do well I'll add an action because I will update the row and this one will be with a error message so I then need to find it over here again I think if you can get your head around what we're actually doing here with this these API calls and handling the responses you come a long way in your automation Journey so just rewind the video If things went a little too fast again I'll look at the ID from up here in the list rows present in a table like this now I will just do a lock and here I will say business exception ID not found and here you it's not a it's not an exception a link to alfo we did everything right it's just that this third-party system this ID doesn't exist over there so what I will also do is that it's always best practice to have a default case and that is because we can see a lot of other error codes and we want to handle them if those occur they will not do it now we know that but it's best practice so always do the cases and then have a default case that catches everything else so choose add an action and here I will have an update a row like this and let me just rename these two so we actually have um I'll do yeah I can say 404 not found over here and in this one here I will say unrecognized or something so I will say update a row on red kick nice like this I'll find the exact same thing again and let me just it will load um otherwise if I get a little unpatient um you can see that it sometimes it takes a few seconds and often the solution here is just to if this happens if you see this load and load unload it sometimes it's easier just to delete the actual action so I'll do this and then just add it again it's not something that you can do it's not something that you have done wrong it's just the way it is now you can see it showed up instantly so don't worry and let me rename it again I think these live pills where I teach um a topic it's great because you'll see all the actual pitfalls or the struggles that we can face as automation Developers so I'll do this and I'll fill it in just as before and then you'll have this now I can say exception except ion and then I could for example have a status code I can say unrecognized status code and then I can actually have the actual status code because we have it right here in our case we will not go into the default Brands and that is because we don't we only have status codes of 200 and 404 one thing that we will need to do you can see this switch here it comes right after the HTTP and when an arrow occurs from up here we need to say that we want to run the switch and let this through which handle the arrows and not power automate so let me just do this so what you'll do is to click these three dots here configure run after and then we will say whatever happens here we want to run the switch otherwise it will take the next iteration so I click done here I'll click save and let's try to actually run this and this is uh yeah as I said it's such a cool technique if you can do do this and understand this it will come to your benefit so I will just fast forward the video so we can inspect the results there you go we now succeeded now we can see that we had no fails that is because we let our switch handle it let's inspect the actual sheet and see that we can handle everything we got a lot of successes and here there you go we now have uh row number 17 with the id17 business exception ID not found you need to learn more Microsoft power ultimate and right here is the next Advanced lesson for you
Info
Channel: Anders Jensen
Views: 15,462
Rating: undefined out of 5
Keywords: anders jensen, power automate, power automate tutorial, power automate examples, power automate api, power automate api call, power automate rest api, power automate rest api call, power automate http, power automate http request, power automate web request, power automate json parsing, power automate json, power automate json parse, power platform, microsoft, microsoft power automate, how to do api calls in power automate, how to do rest api in power automate
Id: rkq7cjLdJGA
Channel Id: undefined
Length: 22min 25sec (1345 seconds)
Published: Fri Nov 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.