How To Create Your Very First API With FastAPI In Python Tutorial 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be showing you how you can create your very first API in Python using the fast API package and fast API is just a very quick and convenient way to create an API so I just wanted to show you the basics of how to use it so in the future you can create your own apis very easily so the first thing you want to do in your new python project is open up the terminal and type in PIP install and you want to use quotation marks here so quotation marks fast API and we're going to install everything inside it so all and close it with quotation marks then tap on enter and wait for everything to install and once it is done indexing you can close the terminal and we can import from Fast API fast API and we're also going to import random since my API is going to return to the user random numbers that we query now the first thing we have to do inside here is create the app and this is the entry point to our API so when we run this we will be able to query information from the API so fast API and we will instantiate it there now let's create our main endpoint so here we'll type in at app.get and here we need to provide the path that we want to use to get this information and we're also going to create an asynchronous function here so async Dev root and this is the root of our application so every time the user loads the API this will be the first response that user gets so here we can return some Json and you can also insert a dictionary the package takes care of passing it from a python dictionary to the Json format for us so we don't have to worry about that so you can either return Json or you can return a dictionary I'm just going to return example and the example is going to hold the text of this is an example and we're going to perform or return some data as well and the data is just going to hold the value of zero so here's a basic API response when we call this API without any endpoints it's going to return to us this data that we can then later use in an application or we can share with our friends or other users so they can use it in their applications so that was quite simple and that's actually all we need to do to get started with using fast API so now let's open up the terminal again and I'm going to type in clear to get rid of that mess and here we're going to have to use something called uvicon so UV cone so that we can call our app and so that we can reload it automatically every time we make changes here and this is one of my favorite features that comes with this fast API package so here's the script and now we have uvicon then we need to call our script name which is Main and we need to call the app as you can see right there I'm using a colon between Main and the app we have to use this positional argument which is called reload and when we tap on enter it's going to start a local server for us so now we have this API up and running at our local server and I'll explain real quickly what it means when we used this reload flag it's actually very convenient and I love it but let's go ahead and see whether this API actually works and it does as you can see in this very small corner it says this is an example and this is the response we get when we call our API so now the API is live at this address here as you can see if we were to reload this we will get the data back once again and you can even play around with this data I mean you can copy this link here if you want and then we can create a new script and we can call this sample it doesn't really matter in this scenario and we can import the requests module so here we'll import requests install the package of requests so we can actually use it then we can close the terminal close the sidebar and we will type in request is going to equal requests dot get and I'm going to paste in my local address here the one that we had inside the terminal down here and now we can actually use this request we can print request dot Json and if we run this sample script we're going to get the same data back as we got inside the web page so it's working on our local address now we have the base of our API which we can test and continuously improve through fast API but let's close this that was just to show you that you can use it inside other projects immediately in case you want to do some testing but otherwise you can just play around with it in your browser now as I mentioned earlier inside the console we used this reload flag and what does the reload flag actually do let's pretend we want to change something in our API some functionality some data let's say that the data should actually be 999. now usually you would have to rerun your script or do some sort of refreshing but thanks to that optional argument we added to the terminal now when we edit our script and reload the page it automatically updates it for us so this code is automatically applied to the API as we update it which is really cool because if there's one thing that's really annoying as a python developer is continuously having to rerun a program so so this was the root of our application let's create another one let's type in at app.get and he will type in random and let's add the slash random because that's the path we want to follow to get a random number now we can type in async Dev and we'll type in get random and here we can type in random number of type integer it's going to equal random.random integer in the range of 0 to 100 and what we will return is some user-friendly Json which says number and the number is going to be the random number and we also want to inform them about the limit that we used so limit is going to be set to 100 and we have this new endpoint that we can use in our API if we go back to our browser and here this time we type in slash random you'll see it's going to return to us a random number plus the limit and we can refresh this as many times as we want and it's going to continuously give us that random number with this endpoint but what if you want to set your own limit so here we can go down and we can copy this and just paste it under but this time we're going to add our own parameter so here we'll add the Slash and we'll add some curly brackets and inside here we need to enter the variable name that we're going to use which I'm going to call limit and the limit is going to be of type integer now instead of returning random number to the range of 100 we're going to return it to the range of the limit and we will also replace limit with the limit variable that we've created so now we're querying for this value here which means that once we run this request and insert our limit here it's going to insert it into our function and it's going to return to us the data that we have queried with the specifications that we have included so going back to our API you can see up here now we can type in slash random slash and here we can enter a random number such as 999 and when we query it it'll say that we entered the number of 999 and the random number we get back will be 569 and we can refresh that as many times as we want and it will give us a new response each time because it's applying that functionality you can even put 100 000 if you want and it will still give us a random number back now it's also important to mention that if you have two paths that are named identically the first one that it finds is going to be used first so that's a detail you should keep in mind and the last thing I want to cover is that when you are creating apis with fast API it automatically generates documentation for us and to show you that you just have to use the endpoint of docs and it's going to give you this default fast API documentation now I'm not going to be explaining how you can edit this documentation in this lesson possibly I will in another video if I see enough people are interested but for today's lesson I believe we've covered everything that we had to regarding how you can create your very first API using the fast API package and what we created essentially was a random number generator that allows the user to specify their own input but now it's up to you to play around with this to do a little bit more research to maybe create a chat Bots or something that returns a cool response based on the script that you've provided instead of having a random number here being returned of course you can insert your own functions that maybe do some machine learning or maybe inter interact with another API so that you can get more information back and you can edit this accordingly to return interesting information for whoever wants to use this and again it's really cool that you can just install requests and play around with it inside here because it also works if we type in slash let's say random and we run this script one more time you'll see we'll get the number of 44 and the limit of 100 back each time we run this we will get a new result so you can also use it to play around with data on your computer which doesn't come from a live API which can always be good for testing and you're also going to get a lot of the logs down here and at some point when you're tired of running this you can just hold down control plus C to stop the server and it's going to end it there so now whatever you edit inside here will not be reloaded and it's not going to be live anymore but that just about covers everything I wanted to explain in today's video do let me know what you think about fast API and whether you would like to see a follow-up video on this one going into a bit more detail on how to create the documentation or how to create a more advanced project of course but with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 20,664
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: F43rgxq4CKw
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Sat Feb 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.