How To Call An API in C# - Examples, Best Practices, Memory Management, and Pitfalls

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it seems like everything has an API these days but have you ever wondered how to make use of an API in your application it turns out that it isn't really that hard in this video I'm gonna show you how to easily load the data from two different api's into our application along the way I'll show you how to avoid some memory issues and some other best practices around talking to API s-- now if you're new to this channel my name is Tim Corr and it's my goal to make learning c-sharp easier this channel is full of videos explaining the various parts of c-sharp I also have a website where I provide full courses in c-sharp and sequel you can check it out at I am Tim quarry comm that is also where a blog post for this video is located it includes the source code for my sample application both the start of the video as well as the end also note that in the description below you can find links to join my mailing lists or to support me on patreon okay let's get started our example project for this application I set up a simple WPF user interface that has two forms this main form we where we load up the images from the xkcd comic the other is called son info and we're gonna display the sunrise and sunset times a day in this form so that's pretty simple that's all it is this right here we have a demo library but that's just empty right now because we're starting from scratch or almost that we've got I've got a button in here a couple of text blocks and a main window has three buttons and an image so let's talk through what we're going to be doing we're talking at two different api's like I said and the first one is for the xkcd comic now it's a kind of a nerdy comic so if you were go to xk CD you'll see the the current comic as well as as previous ones and random ones well they have an api it's a really simple API either you call this URL for the current comic recall this URL with this number 614 being the number of the comic you're looking for so I make use of both of these to load up these comics in in flip through them in our application the other thing we're going to do is this sunrise and sunset time so if you know your latitude and longitude and you can find that out fairly easily on on Google is looking for your address or other information you can find a close latitude and longitude just I think have the exact and you can use this to find when the Sun will rise and when the Sun will set for your location it's going to give you all this information so we're gonna load both of these just kind of download the different pieces of loading API so I want to show two different ones that are just you know totally different so that you can kind of get a feel for what's what's similar and what's different in these and they also provide a couple of other unique tidbits Lee fun to do so let's start with the comic first you know drag it off a screen so what we have here is my plan is when the form loads we're going to load the comic in this space down here the the image space which is essentially the rest of the form and then we're going to allow the user to go previous to previous comics or next if it's not the current one so obviously if you're on the current comic you can't go any farther forward in the future but you can go to previous comics and once we get to comic number one if we ever got that low then this would grayed out and we can only go next so that's the plan for that son info button what that's going to do is load up the other form son info and then allow us to make a call from there so the first thing I do is actually go my demo library and create a new class then I call this class the let's call it API helper I get public and what that's going to do is it's going to allow us to create some of the the basic needs of an API call now for we add anything those class I'm off first good my references for my library right click on references and say majnu get packages I'm gonna bring one in we search for AP at Web API if I can spell today dot client now it's called Microsoft asp.net if net dot Web API that client but I found a searching Web API dot client find that just fine we're gonna go ahead and install that accept it and then right away once it's installed we have to go have to but I like to go to update the Newton soft JSON so right now it's at version 6.0 point four with that package of the Web API client we can upgrade it to version 11 point 2 as other recording those video you go ahead and do that again I like to update all of my nougat packages the layest version if i can at the very start of the application it just makes my work later smaller so get all the updates away as soon as possible so if you run into any bugs you find them early okay so now we're going to do is create a property and we'll call this it's an HTTP client which with a kick control dot and say using system naitch' TTP it will call it my tab isn't working more because I need to the control dot will call it the web we want to call it web climbs that's gonna step on some toes so let's call it the API client that works and will immediately initialize it as well and that's something right there that you wouldn't think is gonna do but I'm going to I'm gonna make it static now usually you don't have properties that are static usually we want to instantiate a property so that we don't step on toes so that everybody can have their own version of that property instead of having the whole application share one but in this case we're going to make it static because the fact that HTTP client we want to open it once per application and yes that's very unusual it's not something we do every day but in this specific case we want to open it once per application what it does is opens up a tcp/ip port and some that set up time takes our set work takes some extra time and so we want to do it once and it is designed to be thread safe it's designed to have multiple concurrent calls it's set up to do all these things with one HTTP client for the entire application so things like opening up one browser on your computer you know so we can have multiple tabs and all the rest but we're only using one browser we don't need to have five versions of that browser open we can just have different tabs so in this case we're make it static and then we'll have one static method you avoid type and it will be initialize client well this will do is we'll set up the API client yes we've initialized it with a new HTTP client in fact you know what let's move that yeah let's move it down into the into the actual method call that way we can check this for null since it's a class we can check for null first and if it's no we'll call the initialized client let's take the semicolon off there to API client equals new HTTP clients the rest say client died oops API client dot default header requests that except dot clear and we're add one new header request and this will be a new media type with quality header the hell you yeah it's kind of long application slash JSON okay so there we go so what this is and actually almost hit can must hit a dot there there we go and it shrinks it down will a be using up here so this is gonna add a header that says give me JSON essentially and that way we can parse it then into our our models so we're just saying give us for the request we're saying just give us JSON we're not going to look for a web page with the the tag and all the rest were looking for data with type JSON all right and that will be all we need to do for this initialize client now I can make this more robust if you want but this is really a sample application that I go full in-depth on this the one thing that I want to point out is typically what we do here as well is say API client dot base address equals new URI and then the the address and so what this is if we come back over here to the xkcd comic notice that both of these start off with HTTP so get a highlight here they start off well it's let's cut it this way the high everything but the highlighted part is what they start off with both of them so we could use that as a base address niche append info dot zero dot JSON or 614 slash info dot zero dot JSON so that's what the base address will allow us to do so we would come in here and we'd say HDPE xkcd dot-com like so and then all commands after that would just be appending on the end now we're not going to do that today we're actually going to leave the base address blank and the reason why is because going to use the same HTTP client for more than one base address because the sunrise and sunset information comes from a totally different website so we'll leave that blank so now we have an API client set up that's static and actually make the whole thing's the whole class static we have our API client that we can initialize that client that's the first thing we'll do in fact we'll do that in the main window that's that's where our application really starts so we will say API helper really just a min dot initialize client so now our HTTP client is initialized at the start of our application we could technically put it in the the app dot sam'l in here but I'm gonna leave it right in me in the root of our main window because it's more visible there to me so all this did all the API helper did for us and this API client is it allows us to make calls on the internet now this is what will use the API client will use this to make calls like you would use a browser to pull up webpages well in this case it's a browser for our application so now let's create a class for loading our comic so let's right click on demo library we're gonna say add class and this is just for loading the comic strip or the comic itself so we're gonna call this the comic processor it's got a process comics like it public it will not be static it will be instantiated and we're going to need to have a few things in here but let's start off by building our actual call to the API so we have a public async we won't make it we wanna make web calls asynchronously and the reason why is because we don't know how long's gonna take to get information back from the website we don't want to depend on the other website being you know ultra performant instead we have to that it might take a little bit to get data back and so look at asynchronous our application doesn't lock up and we're going to return a type of type task and we'll we'll change this to be a task of something but for right now we'll just return tasks because they don't have that something to return yet so we'll call tasks and we'll say we're going to load comic and we're gonna pass in the comic number because these are all numbered will default that to zero and so what does what zero will be is it will be the the current comic so if ever you if you don't pass anything in or if you pass in a zero it's going to be the current comic otherwise it's whatever number comic you're looking for specifically okay so let's start off with our URL and we don't know what the URL is gonna be yet because we don't know if the comic is zero or not so let's handle that first if comic number is greater than zero then we're going to do is say URL equals dollar sign couple double quotes on our semicolon come back in here and say HTTP xkcd calm slash and our comic number and then slash info dot 0 dot JSON so that's what's gonna be if we're going to use a comic number however if we're not going to use a number let's say URL equals and it's the same base right here at the end it's this info dot 0 dot JSON every API will have different terminology different ways to express their API so you're gonna have to get use of their system for doing things and may modify your code accordingly but in this case this is the the format and that's fine what we'll handle it from here next we're gonna do is actually open up a call to our our web browser in this case the the client so using HTTP response message let's fill out right HTTP response message which is in the using system net HTTP we'll call it response equals a wait client oops it's going to be the API helper dot API client dot gets async in our URL so what that'll do is it'll open up a new call or our new request off of our API client and so now wait for this response now what's gonna happen is we actually open up us this call and get the response back at the end of this using statement it's gonna close everything down related to this particular request that we don't remember to dispose of things and make sure that our our calls are all cleaned up that's one of the things we did careful of is we don't want to leave ports open we don't want to open up new ports all the time there's a lot of it things to be careful of as far as memory management and network management and port management we're talking about opening up api's so using one client for all of our calls and then making sure that our actual specific calls get disposed of properly they're both important in making sure that we're managing our resources well so now in here and this is already this point made the call that's always get async that URL wait for the response so if the response is successful status code so there's a couple different success status codes but essentially saying if it's successful then you can do something with it all right so we're going to do something with it in this case we're going to read the data that has come back but at this point you wonder well what is the day that's gonna come back and that's where the best thing I do is to actually make a call and look at the data so I click on this and this is the data which is not pretty at all but that's okay let's see if I can't make that a little bigger for you so this is JSON there was a clear basis so the first thing here the the month that's a property and the eight is its value numbers a property this is a value so you have to kind of understand how to read it and like I said everyone's different but in this case num twenty thirty that's the comic number so as of the recording of this video we're on comic twenty thirty and it's in two thousand eighteen month eight that's August of 2018 day eight which that's not today's date that's a last date the most current comic as of today so we have so we have our num which we're gonna grab we have title and safe title sorry safe title and title read grab title we have the image IMG which is this actual image here and in fact if we were to copy this and open a new tab there's our actual comic so those are things you want we don't have to grab everything so for example the alt text we're not going to deal with that in this application yes it's important yes we should probably attach this if we're gonna display it because if a person is visually I can't can't give the pictures when I have a screen reader read to them this alt text is important so just keep that in mind but for this application we're not going to do that we're just going to grab the number the title actually we want to carry the title I'm not gonna display it so the the number that is and the actual image that's it so numb and IMG so let's create a new model where I call this comic model I get public this is going to be the num property and this is going to be the IMG property so these correspond these names correspond to the the actual API so I would have preferred not to see it be called num but that's okay it's not the end of the world notice though I did change the casing so this is in standard C sharp casing of Pascal casing where every word the first letter gets a capital letter so that's one of those things that the JSON converter can do for us is say let's not worry about casing as long as the names match up so now we can do is we can say comic model comic equals a weight response dot content dot read as a sink and of type comic model so what's happening here is read as a singing this is part of what comes in the Web API is it uses dad's Newton soft JSON converter and takes the data in JSON and says let's try and convert it over to the type that you've given us it maps over and it finds that matches so this case it Matt it's gonna match the num and the IMG properties now what's not going to do it is not gonna worry about all that data that we didn't need so month link year news save tile we don't care and so it says if you're not gonna have that as one of your properties then I'm assuming you don't care and that's perfect I see a lot of people who wire up api's have these huge long models that have all the possible data but they don't care about all that data so unless we care about it don't put it in your model if you think you might care about down the road sure and if you want but don't spend a time on stuff you're not going to use in this case I'm not using anything but num and image so it's only I care about all right now in here what I'm gonna do is now that I have my comic back I'm going to return comic but we're not quite done yet because first of all you can't return a comic yet you have to make it a task of type Marisa bread Tomatoes later tasks a type comic model so now we're gonna return a comic model but not everything returns a value not all code pass so we would do now say else me that's that's what the problem here is this L this FC that so I'm saying only if it's successful well if it's not successful that's a problem right well sure it is throw new exception and we'll say response dot reason phrase and that's just the reason why it wasn't successful so essentially it's going to throw an exception if there's a problem getting our data which is what we'd expect either kid is back good data or it says there's a major problem blow up so now we have this load comic method and it will return a comic model before we go I want to add one property to this comic processor and it's gonna be called max comic number we're gonna do is if you call this with comic number of zero so if comic number equals zero then I say that the max comic number equals comic dot num so if you call with a zero that means the current comic now you find that comic I'm gonna grab the number of that comic and say well that's the max one you can go to and this will help us out in our UI we're trying to go to previous and next to know what the top of the limit is a top of the range is now as I'm sitting here looking at this the only thing I'm concerned about is the idea of cross threading because this technically it's gonna be called task and to me calling something that's not inside taslitz let's change this let's take that back out we'll move this logic over to our our form so the formal hang out since the form is what is doing the previous/next I feel it's okay so let's go over to our forum now and look at our code behind which has pretty much nothing and we have the previous and next and well load this when we load the the forum itself so let's create so let's start by creating a private async if I am spell async task called load image and it's gonna pull in the image number just like before and in this one we're going to say let's do var comic equals a weight and then we'll say it's comic processor dot oops it didn't stand sheet it actually let's make a static that way we don't have to instantiate it there's no reason to make it static trying to process our dot load comic it will pass in our image number so it's gonna grab back that comic and put here and we're going to do at this point the the code for EF image number equals zero then let's set up two private variables here max number and current number so max number equals the comic dot num in either way we're going to set the current number equal to the comic dot num so we're a loading image that's what's going to do they'll take care of calling it taking care of the numbers but then down here let's actually apply that to our image which is called comic image so that little bit rest save our URI source equals new URI and it's the comic dot IMG and the kind whoops it's the kind he is going to be an absolute path so what does it does it takes it applies it grabs its URL and says it's a full URL not just a partial U or L partial URL would be just the the end of the URL we already have the beginning you know if rent website or something like that but we're not want the absolute it's where the whole thing and it will set the comic image dot source equal to the actually its new bitmap and bitmap image image and it is URI source ok so that's all we're doing which is yeah it's a little trickier be let's tape EF and it's doing images but once you do it once it's pretty simple so we're grabbing that URL which is called a uniform resource in nakita I believe URI we grab that and you say it's an absolute URL and it puts that into the URI source and we use that to create bitmap so it's gonna go out to the URL it's going to load up the image and to create a bitmap image out of it and put that into our WPF image here so that's how it's gonna work we're gonna see in action in a minute but that's the process we're calling our API which that seemed pretty simple and then we're grabbing a cold piece of information about the current one that it is if it's the the max one or if it's either way what the current number is and then we're applying that image to our image and should display right away so to test this out let's go ahead and call this load image without an image number right away now you'd think you could put it right in the constructor but the problem is you can't make a constructor asynchronous and we don't want to convert this into a synchronous call and have it block or application if something were to happen to the website it's very performant but we don't want to depend somebody else's performance in order for our application to be performant so here's a little trick that I've picked up that is to an extent it's a little bit hacky but it's not so you know properties on your window itself those searches up here in the window you go have properties there is an event in here but you can also just type it if you type loaded and what this is it's the event for when the windows loaded so I create a new event right there called window underscore Luud and now right here I can make this async and we'll say a weight load image that's it so now if we start this application which before I do the one thing I always forget let's go back up here to my properties and again either come over here or type it out where I go to the window once it windows set location and said to center screen we're gonna start up location that's what it is sometimes I find easy go the properties and find it sometimes I find it easier to go and just type it it's up to we know whatever sticks in your brain do that so we're gonna start up location center screen that we have pops up in this screen so let's hit start on this and there we go it popped up the Connacht notice the comic actually expands shrinks and grows with the window which is kind of nice the one that I didn't do is add a border at the bottom let's go ahead and do that next time he closes out but that's our application we've called an API we have gotten information back we've used that information to load an image now that's a couple things first of all it's fix our image so that has a margin I'll just make it 20 all around that gives a little bit of padding and now let's wire up these previous and next buttons so let's start by actually giving them events so if you just start typing click in your button and then drop down to new event handler hit tab it creates an event for me and I'll do the same thing for the next button and now they come over here we have two new events previous and next so we're going to do in the previous is say if current number is greater than one because if the current number is one you can't go to position zero and so it's a one based counting system I'm assuming maybe I'm wrong with that comic it could be a zero based but I'm assuming it's a one based system so let's say that one is the bottom one so as long as you're greater than one you can go back one so degree than one then current number minus equal as one which subtracts one from the current number and then we'll say the next image button dot is enabled equals true because we want to do is I want enable and disable these buttons Japan where we're at now that kind of trees my mind wait this one's always enabled right now let's go ahead and disable it when we right here in the constructor so let's do the next button next image button that is enabled equals false right away so it immediately you can't go further in the future because we're loading the current one so the next image button will be enabled if you're going back one and then we'll do in a wait for load image and we'll pass in the current number now the reason it's yelling it because this is not an async method and we'll do the same thing down here for this method this event method so it's ready for it and then we're going to say if the current number equals one meaning we're at the bottom now then previous image button dot is enabled equals false there we go so that takes care the previous button let's do the next button if the current number is less than the max number so that max number is whatever today's comic is or this week's coming so therefore if the current number is less than that we can go up one so current number plus equals one and we'll make sure we enable the previous image button because if you are going forward you can go back and then we will await the load image for the current number and then if the current number equals the max number so if we're at the top then the next image button dot is enabled is false so essentially it this is a a mirror image of this but in Reverse okay let's run that and make sure that it works yeah missed any logic here so there's our current button or a current sorry comic and the next is grayed out but the previous loads the previous comic notice next is now available we can go back to that one now it does bring up something that he is kind of important to think through in your application I have not put a kind of caching in here so these comics don't change comic number one two three is always comic number one two three it's not gonna be different tomorrow so really what I could do here is I have some kind of cache here or minor database system behind scenes then I make it call to the API remembers that information for whatever period of time is relevant so if it's time-sensitive information well then maybe we want to keep it for a shorter period of time but in this case I could keep it as long as I wanted to because the day is not changing so therefore the only thing would change is the current one everything previous net if I have that number I have the information I don't have to go to the API to make that call caching information does two things for you one it you become a good citizen because you're not gonna continually bang against a web server especially one you don't control and so the other thing we just wanna caution you - don't go flying through this and look at 80,000 comments all at once try and you know don't just you know next previous previous try and take your time to read them even that gets a little bit of time for the web server you're not it's overloading it with a thousand calls a second you also don't want that in your real-world application either so if you've got a for loop that's looping through and hitting that API with every single call that might be an issue because you may have you may have it then rate locked where it's it says okay you only make 500 calls a minute or something like that and some web applications do that or you may even cause a problem where they say we just can't afford to keep this open in general and now the application that you build the depends on that API well the API is no longer there so to fit a good citizen that way and that's one of the ways that caching helps you and the other way is that it makes your application faster it's always wet should always be faster to go to the disk and get information off it compared to going to the Internet to get information so if you cache this information then you don't have to go the internet to ask for it now I'm not saying cache the images necessarily that I'm not sure if that would have violate their terms or not but that would take a lot more space but what you could do is cache the information that tells you which image and which number it is that's very easy to do very quick and it would save you some time so just keep that in the back of your mind I'm not doing any caching here but I would recommend it okay so there is our our application for browsing the xkcd comic now let's look at something a slightly different and that is the Sun information the rising the Sun and a setting of Sun want the the sunrise time and the sunset time now we know we're doing let's go ahead and create the model let me do the comic model let's get the model for our sunrise and sunset let's go over here and grab the sunrise and sunset times and let's just run one of these and see the result to get back which actually the response is right here so notice this one's a little bit different and this is one of reasons why I want to show this one as well is this actually has two properties if you notice one is results in one is status under results it has properties and that's where we actually want to get the information the sunrise sunset and all the rest which we're going to worry about sunrise and sunset for this application so it actually has two layers in well how do you do that in your application and it's actually quite simple so let's create a new class called this class the Sun result model make it public and I can't play properties in here yet I'm going to create another class and I call this the Sun model then many NS it's hard but we'll go with Sun model and in here I'm gonna create a date/time property called sunrise and a date/time property called sunset so the two things that I want from this API I'm gonna take a Sun model though and come over here and create a property of type Sun model called results so a Sun result model just has one property called results it's of type Sun model and it seems like why did I even create this because you don't really need it well yes you do when we're going to map this JSON surveillance over here again we're a map that's JSON it's gonna look for the matching properties it only has two at the root level results and status if it doesn't find match for those it's not going to do anything but if it finds a results property it's going to expect that to be an object type a class because the results the value is an object and it's object has sunrise and sunset that's what we're targeting so therefore the object of will be the results property so the results property the object is son model that's the eventual goal for us so that's how we set it up now let's go over and create a processor for it so call this the Sun processor make it public and this is gonna basically a copy and paste from this in fact I'm gonna copy and paste it which yes we can probably collapse us down I do if I was going to do this long term and have multiple of these is that make it generic and make these of type T and pass them in that way and then pass in the URL as well that way you could we could knock all this out into one generic method but I'm not going to do that in this video I'm just gonna create essentially a copy of it and then modify into connick model it's going to be son model and yes son mile not son result model because the only cares result that's that doesn't matter to me that's just kind of fluff what I care about is the just a sunrise and sunset so let's add our using statements and instead of comic model we're going to get the Sun result model this is why I hate copy and paste you to modify a lot of stuff and not miss anything okay so I did there was I'm getting back a Sun result model which is not the mall I want but it has all the information I need and then I'm just returning the results which is that actual Sun model so now that should have my sunrise and sunset the last thing to do is to get the URL which I've actually created my own based upon my location so there you go as my my latitude and longitude roughly so now this will when I not load comic I want to load Sun information I call this I'll be able to get the information at the sunrise and sunset for today let's go over to our code behind for actually let's go to the code for the Sun we have is load Sun info I call it right on the button for load and I could call this on the window loaded method but or the event but I'll let's call it I button click and so the code behind now for this will make this async and now we can say let's just do VAR we could do the actual model name but okay Sun info be careful that step that toad the actual model equals son processor dot load son information and now that we have that we can say sunrise text dot text equals most of a message here let's close our thing there sunrise is at and then it's our Sun info dot sunrise why is that not giving back the AH because I did not await it if you don't wait you're a seeker has methods that's a task not the type you expected return there we go sunrise is at sunrise but here's a trick and if you read the website you would tell you the times are in UTC so we need to convert it to local time which cooling off to local time and I do it too short time string and that will give us a nicely formatted time for in local time for our sunrise there'll be sunrise tomorrow this will be the sunset text this will be Sun set this will be sunsets and that's it that should take care of loading the Sun information now notice it's still using I should have point us out already but it's still using that same API client that main window is so are you there sharing a saying API client just kind of want to point that out alright last thing to do is to wire up in the main window this button to actually load that other form here we go click equals new event handler I got a code behind for this and in here we can say son info and we gets call us sign info equals new site info son info dot show that should be it we run this it's gotta load the comic notice that the form load and the comic had not yet as it is a brief delay but it was their son information oh I forgot one thing and it was loaded on the other pages what I was doing so we need to go over here to our sign fell there we go and go here go to properties and change this to center screen I show you this form bigger by default but I didn't oh well son information load son info sunrise is at 6:06 a.m. sunset 808 p.m. there you go there's Benny sunrise and sunset times for today and that's all there really is to call an API from your c-sharp application doesn't matter if your C sharp application is the APF WinForms console application ASP that the UI doesn't matter because the reality is we're not actually calling this in the UI and that's me people often get confused they're saying well how I use this with with this UI it doesn't matter I've got this in a demo library which is a class library any of the full dotnet framework you eyes can use this class library and make these API because coming out of this there is nothing in here that deals with the the web or anything of that it kind of puts a filter on it and says I'll deal with the stuff you know deal with actually calling the API and all the rest what the UI deals with is it deals with objects so it gets back an object in this case a Sun model or in the case over here it give back a comic model and that's a the load image so this it gets comic model back so that it shelters us it shelters the UI from having to worry about well how do I do this doesn't care doesn't matter our class library doesn't that's why I encourage you to think through is the idea that your UI doesn't have two currents ER itself shouldn't concern itself with a lot of these logic things or in this case data access because that's really what it is is data access instead of accessing a database or accessing the API which is from a database so is one more layer but really is just database calls and that's why this looks an awful lot like calling sequel so if you've seen my my sequel calls they're all you know dapper wraps it and dapper getting back just a model or a list of model the same thing is happening here I'm getting make a model or a list of model from the API call now in key things remember the API helper which is where I have the HTTP client we're opening at once per application now if you want to do that the base URL information you can do that and there are ways to set that up so you can have multiple base URLs per application and typically you're not calling 18 different API so if you want to look into that you can that's kind of beyond scope of this video but I want to talk about that as briefly the fact that if you want to change that base client there's trickier ways of getting around that and doing that otherwise your two options are do tight adhere or to open up multiple HTTP clients preference is on not opening up multiple clients if you don't want to point out is I didn't deal with all with authentication these api's are open you can call them directly you don't have to authenticate there's no rate limitations or other things like that the reason I didn't deal with authentication and that's it is a big topic but the reason I didn't is because every API does authentication a bit different seems like and whether you use tokens will use a login and password every time there's multiple different ways of doing that and so try and cover authentication is a much bigger thing to tackle because of the fact that I have to either cover a couple major ones and then leave you know 95% of all api's out or I'd have to cover a lot of different ones and we just be buried in well this one does it this way and this one does it that way and so I decided not to cover that because the API itself the documentation should tell you this is how it works so for Twitter it gives you a token you have a set the application you have to set up the callbacks it's a little bit of a process but when you do you get a token you can use that token then to make Twitter API calls that include that token with the cost well that's a different setup than what you and Facebook has or the other big API is out there so I didn't cover that but I figured that each API should cover that in their documentation pre thoroughly my bigger concern was that you see once you are able to talk to the API how do you get data out and how you use that data then in your application the other thing I didn't cover in this video is I didn't cover posting information to an API it's pretty similar but there are some differences and I will be covering that in a future video I may even cover this in a start to finish series because Dylan api's is something we are on do on a somewhat regular basis hopefully more regularly one of the things I encourage is if you have data make an API that way isn't mad at the API is only for you internally at some point you're gonna want to expose that API someone's gonna say well I want my computer to talk to your computer how you gonna do that if you have just a UI you can't so if you have um asp.net MVC site you can't really expose that data directly to another application but if you have an API in between those two then you have that flexibility in the future so I recommend that pretty much if you have data you write an API for it so hopefully in the future you do you'll deal with more api's but you're gonna have to learn these things you know based upon the API itself okay so like I said I'll cover this in a future video as well some more in depth but hopefully that gets you started in dealing with API in your application using the information consuming it and really normalizing it making it turning it into models that you just pass those models around like you've been doing for hopefully time with things like database access I hope you enjoy the video if you have any questions if you have any thoughts on this please leave them down the comments below I appreciate all the interaction we get on all these videos and the more the merrier so and as always I am Tim quarry [Music] you [Music]
Info
Channel: IAmTimCorey
Views: 273,494
Rating: 4.9213452 out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# training, C# tutorial, api, c# api, webapi, C# webapi, api best practices, c# api tutorial, c# api get request, c# examples, c# example projects, c# sample projects with source code, c# sample, c# sample projects for beginners
Id: aWePkE2ReGw
Channel Id: undefined
Length: 60min 58sec (3658 seconds)
Published: Mon Sep 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.