 What's going on everybody? Welcome back to another video. Today we're going to be taking a look at what an API is and then I'm going to show you how to use it. So API stands for Application Program Interface and it basically acts as an intermediary between two different applications that want to talk to each other. An API typically has a client and a server and so the application or the person that is submitting the request that is the client and then the client is the application that is sending the response to that request. So an API is kind of like a middleman. Let's say you have you over here you're the user and you want data from this company and they have this API set up and in this API they're going to show you exactly what you are allowed to take and how often you're allowed to take it and they're not going to show you everything that they have on the client side because there are things that they don't want you to have access to. So it really keeps both parties happy because the user can still access the data and use the data but this company doesn't take all the risk of opening up all their databases and everything so it's a lot more secure. So to give you a metaphor for all of this imagine you're going to a butcher shop and you know they have all those big display windows of all the different types of meats that they have available and you call the butcher over and you say hey I want this this and this and he goes back and he prepares it for you and then he brings it to you and you are a happy customer and he is happy because you are buying his products and using his products and that's basically what an API is. You're not the one going back behind the counter and getting things for yourself and messing everything up you know that butcher is the one who's going to go get it for you and make sure everything is done properly. I know that was a fantastic metaphor and you guys all really enjoyed that but let's jump on my screen I'm going to show you how to actually use an API. All right so to show you how to use an API we're going to be taking a look at coin market cap and this basically is a website that tracks cryptocurrencies. So over here we have all these different cryptocurrencies and we have their prices their market caps volume circling supply. Now if you don't or if you're not into crypto you may not know what any of this is but it's data you know here are the columns here's the data and we want to use that data so we want to take that data for let's say just a personal project or we want to put it up on our own website and display this so we want this information and so what we're going to do is we're going to go all the way down to the bottom stick with me and under products we have this crypto API. So let's go ahead and click on that and it's going to take you to their kind of their developer section. Now all you have to do in order to get access to this API and the API key which again allows you to use the API is we're going to click on get your API key now and you're going to have to sign up so you're going to have to create a free account and that's all you have to do. You can just say you're a hobbyist and you want this data and you don't want newsletters and you're going to create an account. Now I already have an account so I'm going to log in really quick. So this is our dashboard and right over here we have our API key and you can copy this key or you can generate a new key whenever you want to use it for different stuff or you just need a new key and we have our API key usage. Now you cannot just call it all the time forever you have limitations and this is pretty standard with most APIs. They don't want you just overloading their system right. You're going to have credits so we can only use this 333 times today which is really generous and so you know we can use this and call this a lot of different times. As you can see I've called this many times I've you know tested this quite a bit and this is fantastic so we're going to come back and use this key in a little bit. One thing that you should always look for when you're using an API is the API documentation which is right over here. Now the documentation for an API can be extremely important because not all APIs are set up exactly the same and so they're going to show you how to access it how to use the keys and it's really important that you read through this because they may have certain limitations or stipulations on how to use it. So for us we we're going to be using this and we're going to go to this quick start guide and I do recommend going through all of this I did but for the purpose of this video we're just going to take a really you know high level glance at this. So right here we're going to go over here to Python this is under the the quick start guide right here so they're going to show you you just need to sign up for the free account get your API key make a test call using your key and then you can switch it over to the the pro coin market cap later once you actually want to hit off the production environment but they want you to kind of test it out first. So I clicked on this Python and they are extremely generous in that they provide you with this code right here that you can just copy and paste and use right away and it uses this sandbox API to make sure that your your actual key right here is working properly. So what I'm going to do really quickly is I'm going to pull up a Jupyter notebooks and we're going to test this and I'm going to show you how to actually use it. All right so I have my API test notebook right here I have my API setup which I've already done all of this. So I'm just going to show you how to do it I've already done all of it so I might copy and paste some stuff to you know save me some time but what we're going to go back and do is we're going to go and copy all of this oops let's copy this right here and paste it into here. Now this API key right here I don't know if it's going to work let's test it out really quick it does work but that's not our API key so I'm guessing that we can't just use this forever this is just the one that they want you to use in this sandbox environment but we want to use our actual API key so that we can make those you know 333 calls each day and then what we're going to do is kind of go a little above and beyond that to kind of standardize the data because as you can see this is not super usable in its current state and then after that we're going to have all the data ready to go and we're going to set it up for a future video which I create an entire project around automating this and creating automated polls every hour or day and then we can create the dashboards for it and it's going to be a fantastic project in my opinion so let's go right back up here and we are going to go back so that we can grab our API key so this is our unique API key we're going to copy that and we're going to come right over here and we're going to paste it right where it says pro API key now this is my unique key but feel free to use it I created a dummy account so you can use this as much as you'd like I'll provide all the code in the description you can go and click on it download you know the code and use exactly what I have now if we go back to the documentation right here and we scroll down just a little bit this is using the sandbox API but if you come down just a little further it's going to tell us how to actually use our API key and it's going to say that we should target the domain of pro dash API so let's copy this and let's go right here and let's see if it works and it failed and I think I know the issue I need to get rid of this right here so now let's try it because I had two HTTPSs in there so now let's try this and see if we can get the data using the correct HTTPS so I was hoping that this was going to happen because this is exactly what happened to me the first time I tried to pull data from this coin market cap and I hadn't had this happen in a long time and I wanted to show you how to fix it so I'm honestly glad this happened so what we need to do is actually increase the the data rate limit so what we're going to do for that is we're going to search and we're going to say in a conda prompt and when we go in here this is for me at least we're going to paste this right here and I'll have again this will be in the in the code that I offer you it'll have this at the bottom you need to put in Jupyter notebook and we're going to increase the limit of the day that we can pull or increase our data rate limit and we're going to hit enter and it's going to do a bunch of stuff here give it one second and I think it fixed it let's run it and see if it worked all right so this obviously didn't work like it did last time for me which did not see coming if I'm being honest so I am on stack overflow trying to figure this out I'm going to kind of walk through this with you and see if we can figure this out together because if this happens to you it'd be really great if you know this would help you figure this out all right now I have this other one telling me to put this in let's see if this one works uh no that didn't work either all right guys so I got it to work I it was honestly by dumb luck uh if I'm being completely honest what I would do is try using this right here that's what I did and it worked but what I ended up doing is I just copied this local host I said copy or paste one of these into the URL so I was like hey let me try so I copied it I put it up here I went over here to API test notebook which is the one that we're using um and then I ran it again and it worked um again the half of this stuff is just like tinkering with it and figuring it out if you've ever watched one of my projects I show that stuff because this stuff is going to happen uh and so just a warning that if you were trying this and if you're actually using this around the code you may encounter that and those are the steps that I took to figure it out as you can see I have a ton of stuff up here um from all my testing of the last like 30 minutes to try to get this to work but now it works so now that we have the actual data in here really quickly I'm just going to show you how to normalize it and make it look a little better so if you can see this is actually in uh kind of this JSON format so use this JSON dot loads um in order to get it kind of into a usable state um but you know we're I'm going to show you how to do that but just to show you this is a type and we're going to the this is data right here um type of data it's in a dictionary format data type and so what we're going to do is import pandas as pd and then we're going to do pd dot set um well let me show you this in a little bit actually I'll write it out now that we have this we're going to import pandas and what we're going to do is we're going to do something called JSON normalize which is just going to make this look a lot prettier and you'll see that in a little bit because it's basically going to put it into a data frame so we're going to say pd dot JSON underscore normalize and we're going to say data and I know I haven't really showed you this data super well or or everything in it but inside this j this JSON which is kind of like nested there is uh data in here let me see if I can find it um so we have the status so we can pull in the status which um for the sake of it I'm going to show you how to do that um and you can see some of the um data that we can pull in the status let's try that real quick I think it was capitalized oh it wasn't let's do that and this is the data that's pulling in this is a timestamp this is the error message the total count some of the things that you see right up here right uh then right here we have our data now this is the actual data that we want so let's pull that in and as you can see it is much much better looking than it was just a second ago has all the things that we want as we can see we have bitcoin ethereum tether oops uh and and so we have all of these things over here we have um you know this dot dot dot that's what this right here is so what I'm going to do is I'm going to run this again and now we'll run this again and now we should be able to see every single column that's what this does it displays the max columns so now we can see every column and we can look at the last updated this is today's date at today's time and here's the price so we now can look at the price you may be seeing a lot of standard notation on this and you may want to know how to get rid of that I'll show you how to do that in the future video when I make the full project out of it but that is how you use an api now we have all this data we can put that into a csv we can keep it in the data frame you can do whatever you want with it you can then use this and put this into a website so you can display this information on your website there's a lot of things that you can now do with this data and that's how you are able to kind of connect to this and you are able to use these this key to access their api now again I didn't have to do almost any of this or write any of this because they already had it all written for me in the documentation so I highly suggest reading a lot more of that documentation I read through so much of it just to make sure I got everything right you want to make sure you're using their apis correctly and so I hope that this was helpful I'll leave the code down below in the description so you can click on it download it try to run it yourself if you run into the same issue that I did I'm so sorry I hope that you can figure it out faster than I did or use the methods that I showed you on how to actually you know see it and see the data so I hope that this has helped you learn a little bit more about apis and how they work and how you can actually use them to get data out of them if you're watching this in the future I hope that I have that project completed so you can just click on the link in the description or at the end of the video I should have that available thank you guys so much for watching I really appreciate it if you like this video be sure to like and subscribe below and I'll see you in the next video