Create your own Public API in Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so what is up guys in this video i'm going to be showing you how to create your very own public api so as you can see right here we have some json which was returned by this url and just to show you that it's live when we click on refresh you'll see that this timestamp will update so as you can see that changed to a 64. and i also want to show you that you can add endpoints so if we go ahead and type in slash request which is a custom endpoint that i created and add a user as a query we can just type in code palace and click on enter and it will load a different page which is called request page and the message will now say got the request for code palace and it will give you the most recent timestamp and if we click on refresh the timestamp will continue to update so you can see that it is live and this can work with your android apps or any kind of site but i'll show you how this works and i also have to specify that we will be using ngrok to create the public url because we will have to use some tunnel link to make our local server public so make sure you go here and sign up it's completely free and when you log in we will have this page but we'll come back to this later so let's create a new python project and the first thing we want to do inside here is open up the terminal because we need to pip install flask as the first import and once we have done that we can go ahead and call pip install pi engroc and this will allow us to use ngrok with our terminal in our python project and once we've done that we can go ahead and close the terminal and delete all of this sample text and the first thing we have to do is import from flask everything then we're going to import json and time then we have to create the flask app which is just going to be flask and the project name just like that and right after that we can get started with creating our first endpoint and to do this we just have to call at app.root and then inside here we need to specify what the endpoint will be so for our homepage we'll just have it as an empty endpoint which will be using the get method so we're just going to type in methods equal and then we have to insert get and this is an annotation that we have to use on a function so we have to go ahead and create a function which i will call home page and to return our text as a json we should create a data set and it's just going to be a dictionary with all the data that we want to return so the first thing we want to say is that there is a page and it's going to have the value of home when we call this endpoint then we want to say that there's a message and this message is going to be success fully loaded the home page and finally let's try to create a timestamp to show you that it is live so timestamp and that's going to equal time dot time and this method will just give us the current time as a timestamp under that we want to create a json dump which will turn this data set into json so we're just going to say that is equal to json.dumps and it's going to take the data set then we just have to go ahead and return this json dump when this endpoint is called so json dump so every time we call this it will create this and give us the exact time and it will return it to us as a json file now let's make something a bit more custom so we're just going to copy all of this and paste it right under and the first thing we're going to change here is the endpoint which we want to change to a request or you can even just change it to user if you want you can actually add whatever endpoint you want there just make it readable to yourself and we should change this page to the request page so for the second function we will want the user to be able to input something into the query and to be able to retrieve that we have to create a new variable and we're just going to call this one user underscore query and that's going to be equal to the string of the request which will request an argument and we want to get the field of user so essentially the endpoint is now going to be slash user slash question mark user and equals and then you can type in your username inside here and this right here is going to be the query that we will use to get a user and we can actually save this for later to make it easier to remember just by commenting it out then we have to go ahead and edit this data set so inside here we're going to type in request instead of home and then we're going to write successfully got the request for and then we need to create a formatted string so we're going to add these curly brackets and add an f in front of the string and inside here we will insert the user query then it's just going to return the json dump as earlier except this time once the user enters a user in the query it's going to return a json file with that value inside the json then we're just going to go down and we're going to call if name is equal to main then we want to run our app and i'm just going to specify a port if you don't specify port i believe it's going to default to 5000 but i like to pick my own port so i'm just going to pick 777. and if that doesn't work it's probably because the port has already been taken so try 776 or and yeah that's actually all we need to do for the python file so now if we go ahead and run the program you'll see that we will have a local server that's running and if we click on that it's going to open in our browser the json that we've specified and let's zoom in a bit so as you can see we have the home page it successfully loaded the home page and a timestamp if we refresh this the timestamp will update and also let me show you that the endpoint works so if we add the slash user question mark user equals username it will provide a query that allows us to insert this into the json so now the page is on the request page and it says successfully got the request for username which means you can insert any value you want have it processed in python and then return it as json for your application but now let me show you how to host this on a public server because right now if you run this it only works on devices that are connected to your local network but now let's go to ngrok and when you create a profile you'll have this page so as you can see right here we are under setup and installation and inside here you'll have something that tells you to unzip and install and then there will be an option to connect your account but we just need to copy this part right here which is going to say ngrok auth token and your authentication token so copy that without the dots or the slash and go to your terminal then you just want to paste that in there and it's going to say downloading ngrok which might take a minute or two but i will fast forward to the point that it is done and once it has finished downloading you'll see that it will say auth token has been saved to the configuration file and it is somewhere in our configuration files so the next thing we need to do is copy this line right here which says ngrok http without the slash and you paste it in there and then you just need to refer to the correct port number that you're using for this server which in my case is 7777 and when you click on enter okay so i received an error because i was already running another server at the time of this video and the free account allows us only to run one public server at a time but if you don't have any service running you should end up with something like this that tells you that your status is online and it will give you your account name and it will tell you where you can find your public host so now these urls down here forward to my local host which means anyone can use them and if we click on them it will work just as the local host but now you can use this for your web app or your android app you can use it for anything you want because it is public and it works exactly the same way as earlier so let's go ahead and type in slash user slash question mark user equals federico and it will load that request so that's how you can make your api public for free of course you need to worry about running this all the time for it to successfully stay up but for a free api i think it's definitely worth it and really cool to have in case you have a test project and that's actually all i wanted to go over in this video if you have any questions just leave them in the comment section below and i'll do my best to answer them but as always guys thanks for watching and i'll see you guys in the next video see you
Info
Channel: Indently
Views: 174,199
Rating: undefined out of 5
Keywords: android studio, code, coding, program, programming, tutorials, android, studio, java, kotlin, python, c#, how to, app, applications, developer, developement, learn coding, learn programming, free, coding in flow, androidevs, code palace, windows, computers, ios, technology
Id: 5ZMpbdK0uqU
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Wed Jan 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.