Build your first API in 10 minutes | API tutorial | FastAPI tutorial | Build API from scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the next 10 minutes I am going to show you how you can build your own API by writing very few lines of code and host in your machine Let's see that guys before moving on please go ahead and subscribe to the channel if you have not done yet and also press the Bell icon so that you receive all the notification so guys what all we are going to learn is first of all definition of API and I am saying relearn definition because you have to forget everything you know about API and relearn it okay then we will understand Basics I am going to show you one API and what is endpoint and API key API request and response types I am going to explain you with fast API demo pretty important concept here and API test through GUI I am going to show you and some homework for you okay so going to be very informative video guys please ensure you watch Talent first of all let's relearn the definition of API is okay so if you go to Wikipedia if you you go to any other place the definition is very complex let me make it simple for you suppose this is a computer program on the left hand side on the right hand side is another computer program this is program one and this is program two okay the name of the program one is let's say aman.py okay aman.py in aman.py I want to add two numbers what I want to do I want to add two numbers Okay add two numbers it's a very simple task I want to do in my program but for some reason I don't want to use Python sum function or I don't want to write the logic I am lazy to do that okay so there is a provision through which I can ask this computer program too so what I can do I can ask this computer program to hey can you add two numbers for me and the program will say me okay give me those two numbers at the moment I give those two numbers this second program will return me the response which means two numbers added it will give me as output okay so what has just happened is I am using the logic written in program 2 in program one okay and this entire concept is known as concept of apis in this case this is your root program main program and right hand side program you can think of it as an API so what is an API an API is a place or is a program that you use in your program if you are ready to write code or if you don't want to write code that has been written by somebody else so you ask question in a particular format and you get response in a particular format now here I am saying adding two numbers so you cannot ask add three numbers or you cannot ask multiply two numbers you just have to ask hey give me the addition of these two numbers so format is fixed what you can ask and what you can get in return that format is fixed okay let me show you one simple example of how an API looks like so come here I am in a website visualcrossing.com okay let me enter one more time here I will click this link weather API so I am clicking this weather API so this particular page is called endpoint of an API so what is an endpoint of an API for asking your question you have to go to the end point of an API a point where API receives questions okay so in this case API is receiving question here so here I will enter Bengaluru so Bengaluru is the city for which I want to know the weather updates and I will say create query at the moment I say create query the query has gone to the API and response also I received within minutes within seconds so here you can say I can get the response in Json CSV Excel Etc and if I click on this chart right so I have data of Bangalore so what is the temperature what may be the forecast Etc okay all these things I have so what has happened just now I went to the API endpoint I asked my question and I got my response but remember there is a format fixed I cannot ask any random question or I cannot give any random input okay what is an API endpoint API endpoint is a place where you go and ask your question okay now let's try to understand how we can build our own API in Python in few lines of code just few lines of code okay so I am going to my visual studio guys and I am having a file here app.py fine I am installing a package fast API you can install this package as well very simple to install and I am assuming my machine learning model is a very very simple model so line number 8 to 11 watchm what I'm highlighting now is my machine learning model so what my machine learning model does it takes two parameters age of the person and sex of the person and it is a suppose Titanic data set So based on the age and sex it will say whether the person survived or did not survive okay so this is a simple rule based machine learning model I am saying below is a demo rule based model assume a complex model here fine but I will host this in the API form okay so what what I'm trying to do is I will try to give a age and I will try to give a 6 and try to find information based on whatever logic I have written here through this API so for that there is a decorator I have written slash predict then definition of the of the uh function that runs inside predict so very simple logic here just you have to initialize first API create a decorator and write your logic okay so this is my code in my desktop there is a file called Fast API folder is there and this file is there okay and let me go to my command prompt and I'm inside desktop and fast API folder okay in Windows machine I will just go here and say python 3M UV con UV corn is the server on which it will run app app is the name of your file here app.py is the name colon app and then reload okay you can run this command as well you say enter at the moment you say enter it will say say you UV con running on 127.0.0.18000 that is your Local Host let me go to my browser and try to open that that address so localhost eight thousand eight thousand so if I do like this you will see details not found why because we are not asking any question API is running here but we are not asking any question okay so just to Showcase you just to demonstrate you it is not not how we do in the real world but we do in the development process okay so if you go to the docs right then you will see a UI like this fine in this UI if you click on this get right and you click on this try it out so here you can enter your age for example 21 and I will say F so it will run based on our fl's logic whatever we have written here so any anywhere where sex is f it will be a survived case okay so let me go to my gear and execute it so I should get a response you can see here response body survived is one okay so that is what we expect so this API is working fine and as you can see here if I go ahead and make the this as male then it will be survived 0 because that is the logic we have written in if else Loop okay so this is how you develop an API using first API but there are things to learn here so as you can see here guys request URL goes like this so this is my request URL in the URL your age and sex are going as input in the URL your age whatever age you give whatever sex you give that goes in your input and then you get survey for example if I make this as f it will say me capital F actually let me make it F then it will say me survived one because female is surviving based on our if else logic but whatever we did now that is for development purpose testing purpose if our API is working fine suppose I gave you the example of two python programs right suppose I want to call that API in a Python program then how can I do that let me show you that as well consume API dot py is another file I have here here I am importing requests then I am saying my age is this text is this go ahead and call this URL so as you can see this URL is very similar to the URL I showed you now in the browser just that we are passing agents X as the variables here okay at the moment I run this you will see that I will get a response survived 1 or 0 based on whatever the logic okay so here survived one is the response fine so whatever way we are doing this particular development guys this is known as the Gateway okay so there are two ways in which you can you can build your API or call your API one way is called get way other way is called post way in the sorry put away put away not post way put way okay and here what you can say is uh we are passing the URL in url we are passing the variable but this is not a very good practice okay so there is an another way in which we can do that and that is by not passing you in the URL the data so for example we can write a code like this there is a package called pad antique I am using here but there are other ways of doing it okay so from pedantic Pi identity you call it piantic okay from pedantic you got a base model in this base model you can give your inputs and same way you can write your logic the difference between this method and previous method is just the way you are passing your input to the API okay so this is supposed to be more protected why because if I want to consume this API then I will create a payload like this okay so you are saying consume put you are seeing this payload and I will pass data equal to payload in this way my API will be protected and my data will not be passed in the URL like it is getting passed in the previous method okay so few important things to remember in the concept of apis for using an API it should always be running for example this API is running here suppose I stop it I press Ctrl C so API is stopped now I can't go and ask any question to that API because that service is stopped okay so if I if I click this right then it will say it cannot reach that because I have stopped that so to to you know ask any question to the next program your API should be running and there are two main ways of doing that I showed you you should not pass your queries or parameters in the URL that is not safe so you will want to use the second way and in Practical more sophisticated ways as well okay so what all we learned let's revise it quickly guys and I will give you a simple homework will you learn the definition we understood Basics keys and endpoints okay keys I did not tell you key is basically a way to protect unwanted requests suppose I went to this weather data and I gave Bengaluru and I got the output right so somebody can misuse it somebody can do it multiple times or misuse it or sometimes some people want to charge their apis with some money for those purposes you know key concept is their API key okay request and response type explain your demo and then we learn how to do using GUI homework for you guys I built a simple model here if else model please go ahead and try to host a machine learning model in the API and try to access it through the browser so that you get a good understanding of how the API is working okay so I hope you understood all these Concepts guys see you all in the next video wherever you are stay safe and take care
Info
Channel: Unfold Data Science
Views: 78,315
Rating: undefined out of 5
Keywords: Build your first API in 10 minutes, API tutorial, FastAPI tutorial, Build API from scratch, python api fastapi, fastapi in python, fastapi rest api, fastapi python, fastapi vs flask, python fastapi tutorial, python fastapi web app, python fastapi project, api python, api python tutorial, api python project, rest api in tamil, api testing in tamil, api tutorial in tamil, api testing in hindi, rest api in hindi, api interview questions, api interview questions and answers
Id: _nH3ufRvrmQ
Channel Id: undefined
Length: 12min 13sec (733 seconds)
Published: Tue Jan 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.