How to use a Public API | Using a Public API with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody welcome back to another video today we're going to take a look at what an api is and then i'm going to show you how to use it [Music] 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 meets 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 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 onto 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 circulating 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 gonna have to sign up so you're gonna 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 gonna 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 head 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 um 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 gonna set it up for a future video which i create an entire project around automating this and creating automated polls every hour or a day and then we can create the dashboards for it and it's gonna 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 we 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 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 hd tps 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 all we're going to do for that is we're going to search and we're going to say anaconda [Music] 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 it'll have this at the bottom but you need to put in jupyter notebook and we're going to increase the limit of the data 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 um which i 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 how this one works uh nope that didn't work either all right guys so i got it to work i it was honestly by dumb luck 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 is i just copied this local host that said copy and 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 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 and so just a warning that if you are trying this and if you're actually using this writing 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 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 kind of this json format so use this json.loads in order to get it kind of into usable state but you know 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 type of data it's in a dictionary format data type and so we're going to do is import pandas as pd and then we're going to do pd dot set 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 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 everything in it but inside this j this json which is kind of like nested there is data in here let me see if i can find it 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 i mean you can see some of the um data that we can pull in i think it's 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 time stamp this is the error message the total count some of the things you see right up here right 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 these things over here we have um you know this 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 we now can look at the price um 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 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 um you know see it and 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 in the next video [Music]
Info
Channel: Alex The Analyst
Views: 40,153
Rating: undefined out of 5
Keywords: Data Analyst, Data Analyst job, Data Analyst Career, Data Analytics, Alex The Analyst, how to use an api, using an api, public api, how to use a public api
Id: 2HfSFdPEFRg
Channel Id: undefined
Length: 14min 1sec (841 seconds)
Published: Tue May 31 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.