FastAPI makes it quicker and easier to develop APIs with Python.
We just published a crash course on the freeCodeCamp.org YouTube channel that will teach you how to use FastAPI.
Tomi Tokko developed this course. Tomi has made many popular courses both on his own channel and the freeCodeCamp channel.
Here are the sections covered in this course:
- Installation and Creating Your First API
- Path Parameters
- Query Parameters
- Combining Path and Query Parameters
- Request Body and The Post Method
- Put Method
- Delete Method
Watch the full course below or on the freeCodeCamp.org YouTube channel (1-hour watch).
FastAPI makes it quicker and easeir to develop APIs with Python.
Tomi will help you understand how to use it in this course.
Welcome to this FastAPI crash course.
FastAPI is a modern, fast and iperformance web framework for building API's with Python.
In this video, I will show you how you need to get started working with fast API.
First API also has a very good and easy to follow documentation, which I can also recommend.
And it is also a great framework for building web applications with Python.
During this tutorial, there are some tips on keynotes that you might want to take down.
But I already did this for you.
So I made a fast API cheat sheet in which you can download for free using the link in the description below.
And if you'd like more tutorials, ideas, don't forget to check out my channel as Cody told me, where I teach more on Python, and web development in general.
The only requirement for this tutorial is that you have the basic knowledge of Python, and you have Python installed on your computer with a minimum version of 3.6 or higher.
Have you noticed said let's dive straight into this video.
So the first thing we're going to be talking about is how we're going to install fast API.
Now to install fast API on your computer, your laptop is very easy.
Because first API is a Python framework, we're gonna use the Python package manager, which is Pip.
So you can just open your command prompt if you're on a Windows, whether you're on a Mac, open your terminal, and we can just type Python, I think m pip install bust API.
Now it's very easy to just do command line you need to install fast API.
You can also do Dinamo, we like peeping so fast CPI.
Or if you're on a Mac, you can do PIP three install for CPI, just the normal installation command lines.
So I just like using Python, by free, just personally, but you can install it anyways.
So I that is running.
And as you can see it says requirement already satisfied.
Now if you walk with Python, you know that this means that I already have this particular library installed on my computer.
So because I have it installed, it's not going to go ahead and install that again.
Let me just type CLS to clear that up.
So what that did was it install fast API.
So if you do a fast API, you stored it, you go ahead and download some files or not just only fast API, some other stuff that you're going to use later, like typing or some other libraries, it's going to install that alone.
And there's one more thing we need to install to let's say, Pip, install you you'll recall.
Now the reason why we say p p.
So you recall this recall is what we're going to use to run our web server.
Now, if you work with other libraries, like for example, Django, you know that Django, you don't need to install any external team to run it or your web server.
Once you just type a command line, if you have Django installed is going to run into a web server.
But fast API is totally different.
Because working with fast API is a very light library.
So you need like an external library just to run that file on the web server.
So this UV con is going to allow us to run our first API project or file on the web server.
Once I press enter, now, I should also be expecting a requirement already satisfied, because I have this particular library installed.
As you can see, it tells me requirement already satisfied, the ledgers closed our proposal.
The next thing I want to do now, the first thing before I started this, I already gave it to my folder by came to this directory, and then I created a Python file name my api.py.
And in my Visual Studio code, I already open up the file.
Yeah, my api.py.
So this is the file we're going to create in our API's are running everything we're gonna do.
What I just want to do is to copy this particular directory for me to my command prompt and CD into that.
If I press dir, right now, you can see that I have the file, just to make sure that we're in that particular directory.
And now that I know I'm in that directory, let's just go ahead and create our first API.
Now the reason I just opt into this directory is just simply because when I want to run these on our locals on our web server later, I need to make sure I am in that particular directory.
So I can run the file in that directory.
So Oh, this is going to make sense later.
But let's just go into Visual Studio.
Now, first of all, let's just import past API.
So we can just do import past API.
And if we save this and come into our command line and run that Python, my API spy, so if that runs without any error, or anything like that, so as you can see it doesn't give us any early response does just because there is that's the teller, the first step has been installed successfully.
So let's just close that.
Now let's just go ahead and create our first API.
Now, well, we need to do to create our first API, we'll say from first API, import past API.
So this is just import first API, like an object to work when you use this object, or an instance of this object to create our API's later.
So now that we have this is imported, we're gonna set up is equals to fast API.
We're taking it from this first API, which we imported to our creating like an instance of the first API object, so we can assess this later.
Now this object, it has plenty attributes like the get, let me do let me go into that now by just some other things that we're going to use to create our API later.
So now that we have that created, what we want to do is to create an endpoint.
Now let me explain what an endpoint is, to an endpoint is one end of a communication channel.
So that's why so complicated.
But what that just means for an API, an endpoint in a URL, so let's say we have a URL like on our local hosts, slash, delete user.
list if a user so for this particular URL, the endpoint is this delete user.
So this is like the path in a normal URL, we call it the path.
Let's say we have something like amazon.com.
And then we have something that Yeah, just delete user or something like create user.
So this is we can say this is our endpoint in a normal URL.
This is the path, we're dealing with API.
This is like our endpoint to just have that at the back of your mind on each endpoint, where you're specifying your URLs do my like, perform different operations.
So my just get a particular information, a particular data store might be too late, for example, this great user, the endpoint is self explanatory is creating a new user.
So that means he will have to post some data to that endpoint, so we can create a new user.
So again, you're gonna understand this when we get straight into it.
And there are different type of endpoint methods, body cones, there, there are lots there are not too much, but they are a lot, but the core ones get not this get, we have posts, we have routes.
And then we have delete, let me just explain this quickly.
So the get is used to just get an information or return on information just to get or return a data or something like that.
And then of course, this is just to create something new, and put it like that with something new, or create like a new object in the database.
And therefore put this port is used to update a data or to update something in a particular object.
So all these again, you are going to understand when we are putting all these in all this practically, for updating on Delete is self explanatory, is just used to delete something.
So as I said, these gets used just to get or return data or information post is to create something new or just to add something new input is used to update something that already exists in that particular database, and delete it just to delete like a data from a database or something like that.
So now that we have all these known, I already understand all these, let's just go straight into creating the route HIV, the real API.
Now for me to create a new API, I'm going to do at the Add sign up dot get.
And as you can see, for this API, I'm using Git, and according to the explanation I did like a few minutes ago, it shows that I want to return on information or we just want to show an information.
So if I want to, like do something else, like posts, put delete, we're also gonna do all that later in this video.
But let's start with get laughter You say app dot get a note that this app is getting from the first API instance, which is what we imported, say app dot get.
And then we're opening a bracket to the current output slash.
So this is our endpoint, our API to the home or right slash, it means our own and our own can be like, what what we show when we just come to our own page, like google.com does the homepage.
So whatever our domain is, does Just our own page is something like slash, Bessie like delete.
So that is another like URL, it can be google.com slash delete.
But now this is just the homepage just to make sure you get that correctly.
But now that we have that we're gonna have a new function, let's name this index, our own or anything wants to name it.
And the no Ledger's return particular data, series name, ciphers data.
So what this is doing now, as we say we are taking from the app, which is also taken from the past API object hours in a get method.
And the endpoint you specified is the home page or the home URL.
And then under that, we're just going to have a new function, we can name this anything we want, what we just do is to return this particular data.
Now, this is just a dummy data that we just input.
But later on, I'm going to show you how you're going to use it as a Python dictionary.
And yeah, normally when we're returning data to be written in Python, your response.
This API first API uses JSON, but later on, so I'm going to show you how to use a Python dictionary and automatically converted to JSON data.
But for now, this is just a normal JSON data is being returned.
Let's save this particular file.
And first run this and really see what we are doing on our web server.
That's when UV call comes in.
But let's go back to our command prompt.
First, to be able to run these, let's just make sure that we're in the directory of this particular file.
And to make sure we just press dir, and then we see our file does good.
Now for us to run these we need to press after it is the first one we need to add is the name of the file, which is my api.py.
The second one is the name of the variable you use, most of the time, it's always up.
And then there's basically two things but ledger says we know that Ledger's roll this on the web server, what I'm going to do now I'm going to say yuvika gonna leave a space.
And I'm gonna say my API, which is the name of the file, but now we don't write about api.py, we just write only the name of the file, not with the file extension.
And then we'll put a colon, I'll say up.
And this is coming from right here, which is the variable we give to the object instance of fast API.
And then we just put through iPhones, save reload.
This is the basic command line just to run our fast API project for our server.
Once we eat, enter, that you can see it says you've gone running a PC application startup complete.
And it gives us this particular URL.
That is the URL that says where we should go to to see our project.
I'll just come into my browser here, paste our URL, hit Enter.
And boom, you can see that I have named first data, which is exactly what we returned right here as a response new first data.
So this is how you can basically just have a very simple API.
Now, pass API as is very cool thing that I like most of everybody that works with fastpay.
Like, we just got the documentation to automatically generate these good looking UI documentation for your API.
If we go to slash Doc's.
And we hit enter, CCS fast API swagger UI.
This is just like a basic documentation for all the API's you have on these your like website, or like your API application, whatever you want to call it.
But as you can see, it says default, we have from the endpoint of UI, just slash.
And the function is index, which is right here, function, give it to the index.
And as you can see, using a get method, that's what it shows you it is it gets metal, they just basically generate these, like API for, like this documentation for our API.
And we can test our API directly from this place.
But we don't need to use postman or any external service to test API, you can just come here, click on try it out.
I will say it execute.
And I come down here, he just give me the response, which is named first data of whatever is being done here.
Now, we're also gonna go complex, more in the video and show you how to like manipulate all this data very well.
But for now, this is just the response that is being given back to this page.
And of course, if we go to the Oh, students same response, so To this point we've talked about, first of all, we created our first API, we made sure we installed all our past API, you recall on everything we needed.
I explained, endpoint, what endpoints are and get post, put, delete, implemented.
And yet again, we created our first API, and also showed you how to use documentation of fast API.
So that's basically all about reaching our run our first API.
Now, let's talk about our endpoint parameter.
Name parameter is basically used to return a data relating to an input in the part or in the endpoint, basically.
So we can do that using either a part or a query.
So we have two input parameters, which are path parameter and a query parameter.
I'm going to show you I illustrate these right now.
But first of all, let's have low year as a new dictionary, our name is students.
It just filled out correctly, the day dense.
And there's a key with an ID of one, unless you have that as another dictionary, lesson name.
Give you like john.
These are the age this C 17.
There's a class yet.
So this is just a basic, make that small caps.
This is just a basic Python dictionary, we just have one field India, this is a key, and this is the value.
So what we want to do now is that we want to return the data of this particular student using the student's ID.
So in the field, by key, let's say slash, we can have a parameter like slash, it's then slash one.
So while you have one It means is you return the data with the student the as the ideal for.
So before I get into these, let me explain what I mean by a pass parameter.
So let me just come down here.
Let's say I have a website like google.com, slash, and they have get student then this is just the basic URL to get a student.
And it can just basically show me all the data we have in this dictionary.
But let's see how to get only a specific study like this to date, which has the ID of one, and I can add another endpoint.
But now this endpoint will be dynamic is going to be like a parameter that the user inputs, that will be something like slash, then can be one, then when we go to this URL is the user that has the idea of Wanda is going to be returned to his to his user as the idea of two.
So let's cancel this and just go straight into it.
So we have this object right here.
And then what want to do right here in app dot get place first of all, have a new endpoint, app dot get also why isn't using I get, and this time around, let's say get student.
Now we have this endpoint name gets to them.
And we'll put slash again, I now want to take a parameter dummies want to take an ID of what a user should input.
The first To do this, we'll use the curly braces, and they will say student underscore ID.
This is just telling us the previous ID huge basically be collected.
And now they Jeff, this creates in the normal function of Cassie gay student.
Now, remember right here, it was just blank in euros not in India, but now we're collecting students ID underscore ID.
And this should be equivalent to whatever you put right in here.
That does what he just being collected.
And we have to specify the data type of this, which is our integer so the ID should be our integer now, or we can just do is to return students.
We're returning students, which is from year is in the student ID.
So let me explain this.
One more Time, what this is doing now is that we have these adult guests slash guest today, slash why now why isn't it curly braces, that's fine like a variable mpg like variable a dynamic variable.
So whatever these are input here is what is going to be used to get the student.
So d f, get student there now, initializing the student ID thing we're giving is like a data type of int, that means it must not be a string or a boolean value, it has to be an integer.
And then we just say return students who are turning the student with the students ID.
But what I mean by this, you know, in a normal Python dictionary, if we just want to return these students, because it has a key of one because the students want.
And this is basically just going to return the value of this one.
But since one is to be dynamic, I want you to do what the user inputs, we change this one to the students ID damage whatever the user inputs, the key of that is what should be, and not, let's say user input like five, and we know of that here, just gonna give you like the channel found or something like that.
So let's save this.
First of all, let's just go test it out.
If we come back here, and let's refresh this page.
Now you can see that we have a large guest student.
And instead he knows the names are Roscoe ID that we call me utilizes students ID is required.
That means we can just test this out, I say execute the we try to try this and execute T, it says that we need to input something here.
Now we put one, I say execute.
We see now that gives us a response of named john 17.
On year two, that is basically this one right here.
So if we come here now, and then we try to put something that is not the like three, I'll say execute.
You see the ISIS internal survey Rodas because there is no detail like the, of course, let's just go to the URL direct directory directly, we say get student finish student then slash one.
You see now that we have john age, we just have basically dat data proceed to internal server error.
Let's go back to the docs, the does what's the path parameter is basically all about what we can make it more we can add more details to it.
Or we can say let's say we can make it compulsory.
So let me come back here.
Let's say we want to add more details to this student id like, you know, when we are serving this API to a user, the user manual really good like you really name this today's idea, we just say student ism, I know understand what this is like, do we need to pass the student name, or the student's age or the class or the ID.
So if we don't have a surface financial name, we can use like, something we need to import.
And then that is going to allow us to have a description of it that the user can know what to input.
But first of all, let's come up here and input path.
To say that we don't need to have an dialogue, we can just press comma inputs path, because it's from past API inputs in it.
So now that we have parts imported, we can just say student ID, below int equals two, then we can say path.
First of all, we'll say none.
Now this non is so there, if the student, if the person doesn't input anything is just going to leave it blank or is not going to once Iran is just gonna like bring out an empty data.
So just to like catch the error.
Remember, when we did it back year, when we tried to get without inputting something, he just told us that we need to input something.
That's just what this is doing.
And now we can add a description.
Now in this description, we can do something like we want the ID of the student.
You want to view like that.
So now this is just the description we are giving it.
And once we have this, if we save it on then recall to our code right here.
And let's just try to refresh the page to refresh dogs.
And then we say get student.
No, because he writes in distribution.
He says the idea of this today you want to say just more, giving us more description or more details of what we really want to collect.
What you also more attributes is not just only does it come back to our code, we have more attributes in this path, we have attributes like less than two, let me just quickly come down here again and experience it.
But we have let greater than gt GT, this just means greater than, is self explanatory, we have less than, we have D, which is greater than or equals to also have LD, which is less than or equal to.
So this is just telling us since we are collecting an integer, we can specify whether the integer want to collect must not be greater than 50, or must not be less than one or zero or something like that.
Let's say we don't want our integer to be greater than zero.
But we want it to be greater than zero, so it must be greater than zero.
Let's save now, we come back here, it refresh.
Now, let's try it out right here.
Let's say we try to use zero and say execute the gives us this error say, ensure the value is greater than zero.
But that's because we specified in yellow, we don't want it to be less than zero, it must be greater than zero.
And we can also add to, let's say we want you to be greater than zero, but less than, less than, like three.
That means the number should be from one to two.
So if we go against any of these rules, for me again, in the refresh button, and right here, we tried out the let's say we do three presses acute trauma give us an error, we say ensure the value is less than three, when I we do two is gonna give us internal server error does because that data is not found there is no data with the key of two with do one, you know, we have that data right here.
Right here, we just go here, the key of one to come back here.
Now you see that gives us a data that does our we can just add more values.
Or we can Yeah, we can add more values and details to this particular API.
Now let's talk about query parameters.
The query is used to pass a value into a URL that is quite similar to the path parameter.
Let me quickly explain that again.
Now in a URL, like I say, google.com, slash, and we have results.
Now when we have these question mark, and then we have variable name equals to something like Python.
Now in this URL, the query parameter is this search equals to Python.
So this is just basically giving us an like a key and a value.
So this is the name of the variable as we pass undefined is very, very, very similar to the path parameter, right here.
And the path parameter, you know, we have a name of student ID, or we have a value of whatever the user inputs, that's just our which is zero.
So we can have like a URL, and then instead of having slash or something to, you know, be on that same endpoint, for right now, is gonna just have a query attached to that end point.
to having this said, let's just go straight into using these practically, what I want to do now is I want to create a new person a new endpoint, I'll say app dot get also.
And then let's say slash get, I think by the want to get the student data by the name, this right here, we're getting by the students ID for the difference between the query parameter and the path parameter is that in the patch parameter, we need to add whatever the parameter wants to collect in the URL in the endpoint right here.
When it query parameter, we don't need to do that.
Or we just need to do so coming to our function.
This fire function, I say get student.
And then we'll just say, name is a string.
So right here is very similar.
You see it in the path with the student ID is equals to an integer.
But we already defined our students ID in the URL.
But here we didn't define any team but we are now coming into the function and defining it.
So once you see something like is it ready, takes it as a query parameter.
And then once we have that I'm just going to these our particular function.
And now, once a user comes to this URL and do something like this, I'll say name equals to like john, we want to get the data there as the name of john, to this as duties now, and I use a for loop and say for a student, ID, student.
If students were brackets, student ID, name.
posts are the name which was passed.
And what we just want to do is return to dance.
That particular students ID, none of this is just return.
What this is doing is, first of all, is looping through the values we have in this student wise in the key of students ID.
In this student, we say if the students have this student ID, the name of it is because with name which has been passed, let me explain his will to we are looping through this student's ID using while looping through this students dictionary using the students ID.
And this Jeanette ID is this.
So we can name this anything right here, we can just say ID anything we don't want to name.
But if I say ID pattern automatically see that as a keyword.
So I'm just going to put a student's ID so it's original.
So now we are looping through it using these, I will say if the student with this student ID, the name of it is equals to the name which is being passed, they want to return that particular student.
If not, you just say data not found.
Now to make a query parameter, let's just go and test it out.
Let's come back here.
So now we have guests by name, or we see yet we are name required.
We can just you know, we say john, once we execute that should work.
So as you can see now is named john, because it gets that is really a value by data with the new module where we try something like let's say, a team.
We execute, gonna say data loss, because we don't have any data rights in year with the name of team.
So that's basically how to just create a very simple query parameter and fast API.
Now to make these our query parameter option now, we can do the so is very simple.
Let's say we just want to make it optional.
Like we don't want to require we come here, you see that it says it is required right here.
Let's make this option now.
We can just say name, right here in this string Kathy equals to not equals to no is no more required, we can go year.
So dogs always say get by name, Katsina, it doesn't say da required again.
That means we can try that's gonna give us an error or something.
They just says data not found.
But now is no more require.
Now does one way to do it.
But the best practice is to use the default option now method that past API is for us.
For us to use these, what we just need to do.
First of all, we'll come right here is very easy, we just say optional.
And then equals known.
So this is just going to make it optional for us.
But before we can use this, we need to make sure we put that option now, they from typing import option.
That is just to make it the best practice.
Because these are facts epi recommends that if we need documentation, also do it the normal way or just the other way, not the normal way.
This is the normal way.
So we can also do it that way to keep things simple.
We just want to you have a clean code and a more readable code as the main points to make it more readable.
We can also use this so right No, this is gonna be like, not required is going to be optional.
So now, let's say we want to add another query parameter into eloquent to have to query parameters.
How do we do this? I'm going to show you something.
Yeah, let's just have a comma, I will say test it.
So we just have a query parameter of JSON is an integer.
So if I try to run this, we should get an error.
And I'm going to show you why we have that error right? Now.
Let me call me not here, a browser.
So okay, you can see that it's not even going if I come here.
Now you see dices syntax error, non default argument, follows default argument.
So what this is just saying is, we have, as you can see, right here, we have named optional.
And then we have test, which is optional.
So Python doesn't allow us to have like, an optional argument before, a required argument.
So fast the back covers is about Python doesn't just allow us because Python is a language writing.
So we also have to stick with Python.
If we come down year, one thing we can do is to just switch to places like changes, or bring it here or something, we can also do that.
But the best way to do this, so we can have a limited query parameters and have them anywhere we want.
It's very easy, what we can use to say asterik sa, coma.
So once we have these, we can now write it if we have a default parameter or optional, or whatever required is going to work since we have this now.
Gonna be able to take care of anything that comes after it.
Never call me on it, refresh.
It that should work.
Let's see, let's have that a reloading? Yeah, there we come, you know, she's ready reloading.
And that should work.
Let's give it one more trial.
Okay, that's taking a while to reload we shouldn't.
But that's just basically how to solve that particular error, or when you're having an optional parameter before a required query parameter.
So I hope you understand that concept.
Now let's talk about combining path and query parameters together.
Now, sometimes you might want to do this, well, most of the time, you might not.
Let's say you want to get two different values from a user, like let's say his ID on his name, or the class or whatever, you might want to combine a query parameter alongside with a path parameter.
First to do these, you know, right here, we already have weary parameter of name and test, the length of the path parameter, let's also call it the student ID, the slash K, students underscore ID.
And then since we are defining initializers, to net ID in the path, and we also come here, and then say, dent underscore ID, and then we give it int, j comma.
So since we have eta on here, that is a path parameter, which only in the function that is a query parameter.
So that's just the basic difference you just need to take note of.
So we are between these two places does that.
So now we can just easily test this out? Actually, we even when we collect the students, Id one organizer for anything? Yeah, because everything was doing is basically getting it by the name.
But that's just how you can combine a query parameter with us come back here.
We hit enter.
So if we say get, no, this is not what we need.
We want to get by name.
We say get no, you see that we are name test.
We should also have students ID to strike out as the key.
If we come back here.
Did I okay, we didn't save our file.
Let's make sure we save our file back ducks it and now we have student ID which is also required.
That just the basic concept of like combining parameter on a query parameter together.
This part we're going to talk about request body and the POST method.
So the request What it is, is like the information or data pass when creating a new object using the POST method.
Now to do this, first of all, let's import something right here for the base.
So say from by downtick import base model, just to be more elaborate.
The request body is like the data pass when you want to create a new object or a new data of t.
So, I'm going to explain this later, like, you're gonna see what we're talking about, you don't need to like know the definition or whatever that is, just to know how to use it in a practical case.
So now that we have this base, Moodle importer, we're going to create a new class.
And this class, we just want it to be similar to the student dictionary, and let's name the class student going to take from the base Moodle let's have a name should be a string is of an age and use that as an integer.
And then they have a class.
Now I would have loved to have a class boy, Cassie is seen it as a key word.
So let's change out to like year, the year of the student and that should be string also, they just call me Mr.
of class here.
So we can use it, you know that we have these, what we can just do want to create a new path now.
But this time around, we're gonna use a POST method.
First to do this just similar to say arts app dot post proposed.
And now we will say create students now wants to create a new object of students are going to take a path parameter, or students ID, I know what we do, when we take a path parameter, we have to define that in the function.
So create, underscore, the desk.
router, define our student underscore ID as an integer.
And what the next you want to specify is student.
The students know what it is, is basically the details that the that the user submits.
So in order to create a new students object, for using this particular class, to know the details, let's say the details, the name that the user saw me the age and the year is what is going to be saved in this student.
So want it to be because to this student Moodle, you can see that these with a small letter and capital letter.
So that's what's really different.
So this like key value, but now that we have that just go straight to say, if the students ID that was passed.
In students, what we just want to do was say, return like an error, say student exists.
So this means that if the student ID in is already in students, and the student ID right here, then what we just do is to say return an error, does history already exist, damage the concrete, the same student with a student with the same ID twice.
What he does not the case, Justice students.
Within the student ID equals to student I tell you that the student is whatever the user broad or submitted using this post metal, but now that we have these, we've created a new object of a student.
And then we can just basically return students of that student ID.
This is just the code we need for this.
So before we test this code, or let me explain what we did in this code, again, what we're doing in the score above register, we are taking a path parameter of an ID.
So this is per parameter alongside with all the details of the student, and they were using that to create a new student object.
So please just go ahead and test this code to save the Permian.
Now you can see that we have posts not get any more great students, our students ID is required.
And they can see it says request body also required.
That's what I talked about.
Talked about the request body is basically the details as the name PhD year it needs orders, let's try it out.
One of our students have to, though, we'll say, just say, Tim, give Tim age of 12.
Now, if I execute this, see now that it just gave me the response of what I just created.
But now we have a student ID of two, if I call to get by student's ID, so let's just get the students using the ID, we should love that now safe.
So let's say to say execute.
Now we can see that we have this team, with dummies, it has been saved right here.
But one thing about this is that it's saving it in like a memory.
So if we refresh this page, that is gone.
But you know, we are working with real world applications, you want to save like your database or something like that.
So you can see that we created a new object of that now.
And then we just use the get to get that URL, if we want to read what already exists like true, and want to create it again, let's execute, we say says error students already exist, we can create with the same ID twice, does what's good about that? Let's go back to our code.
So this is basically how we can just use the POST method and the request body to create a new object in your new object in our data in our database, basically.
Let's talk about the ports method.
As I explained earlier, in this tutorial, the put method is used to update something that already exists.
So to do these, first of all, let's have a new endpoint policy up those goods.
This time around, we'll say patch update.
affairs today student and they want to collect the ID of the student.
So we know the student in which we are updating.
Now let's have a function saying date.
Student we also have to defend us today and ID as an integer.
Now students just similar to what we did here, we know our collecting.
Vantage goes to this student.
Well, now, I want to explain something.
So right here you can see that it says to that is because of studio.
But when we are posting these posting a particular data to this URL, I will say this student is equals to student, everything has to be essentially all the field in the composer.
So you've become here now.
And we give a name field and we don't give an age field not going to work if we are using the students module for the update.
So we need to create a new model, but this time around to make all of these optional.
So the reason for these is so that because when a user want to update a value, you only want to update the name and not age only wants to update the year and not the name of a new our body we are using this student class to for the update method.
The scenario as the user as to input all the whole data now means he has to change audio data every time he wants to update something which is not a good practice.
Let's just quiet and create.
Plus our name is update.
Student and does also take from the base model.
Same thing name.
Now we'll say optional.
Trained person just to make it optional.
To do the same thing for the age.
Optional, trained integer person and then exactly Simply for the class right here we are using year optional and this is a string equals no now we can use this of this students model as a reference here.
So, these notes tonight again these objects today now with these each of it is optional see we use I gives only one value the other values can still remain the same does very good.
And now, what we just want to do to say if student ID not in student will say return an error of like these particular student does not exist or something the world this is okay let me quickly write that though that under students does not exist, what this is doing is first of all it checks if the student ID is not a student the damage if the the ID which was being passed to what we're doing is if we want to update this today we're going to pass the ID of the student then whatever we want to update.
So, it will first check if the ID is present in students will not present it to you the student does not exist today.
You also update does not exist at least you have to have a student before we can update that data.
We have to check that first.
But if it exists, we can just do something like students didn't ID equals to student and then right now we can just return student This is exactly the same thing we did right here dance ID exactly what I'm going to show you where the arrow is in this particular is not an arrow but where the touch is if we come near now on test days see the refresh button now you can see is the put we have the put method and no let's first of all let's create a new student so straight out this are creating with to let's say Team 12 nine now we execute this we have a new objects or new data or the counterpart want to update this student.
So let's try it out.
And what's up JD students with the ID of two witches team when I want to change the name to Tom not team we don't need the age or the dummies this option also we don't want that he doesn't know what to do.
If we execute this now you will see what will happen.
So now it says named Tom age no yeah no because we didn't specify these he says no.
They were all the way to get your student by ID and I just want to get this student into an execute what is going to happen now you can see he doesn't even show us the age again.
He doesn't show that he's 12 years old.
The is in year nine.
So it has been overwritten so it because we didn't provide this age on year when we are updating it also via ride rides as no damage has no value.
And this arrow is coming because we just did students with us today Id is equals to student needs to know is whatever the user passes.
So since we didn't pass anything, it just updated our student that already has a value of this it has nothing which is but the first take care of these we are going to have to do it in like a manual way to come up here to say if a student Let's name is not posting non numbers if there is something in what the user past then doesn't want to update on say to dance.
Dance ID does the name will be updated to the student.
This one does name is exactly the kind of code wants to use I'm also going to do the same thing for the age and the class or the year, the eve student that age is not equals to none.
See, the dance with a student ID, the particular student does age equals to the dent.
But ah, Now, last time, we're gonna have to do it for the year, that's here, now equals to none, I mean, you're giving us an empty value, then that's the only time when we now want to update sort of data particular value, but a particular key, so you don't give us something, we are not going to update it.
When we give us something, we're going to update it, then to a year.
This is just the code to take care of that error.
So now we save this.
Now, this is very good, because we've taken care of that particular error.
And now let's just go ahead and come back into your and then we eat fresh, this.
Great, but what wants to do first of all, let's create a new dent to him.
Both yes or no.
And now we have that, let's come to our outputs and update that team.
For now we want him to be 15 years old to Let's leave this name as blank.
Well, now the age wanted to be 15.
So you can see that now we didn't pass the name or the year.
But if it executes, only so let's see where the error is coming from this expecting proper name equals interval codes.
Okay, so it wants this to be in double cordial because it's JSON data.
Let's do this or execute.
Okay, especially improperly named interval codes.
Okay, this gives us an error enclosed in double quotes.
Now let me tell you where the arrow is from if you don't have anything after it, like you do have another value right here something, just remove this comma, we don't need this actually, we should not need that because an integer to make sure you don't have a comma at the end if there's not enough to read.
So now let's hit execute.
And if we come here now you see there are only 15 was updated and alighted at 15.
But only 15 was updated.
Now we have team a nice to intact.
So this is what we want, we can also do it again.
And now we can just update our name.
And we wanted our name.
Now be Tom.
The execute procedure, we now have Tom age of 15.
year over year nine, this is exactly what we want.
If we come back, you know and try to get to didn't try it out.
Let's try to do it, you can see that we have the named song working perfectly the age on the year.
So this is exactly what we want to get.
This is our main go.
I'll also come down here to get by name.
Let's test that out.
So let's try it out.
We have to we have the name of Tom.
And as I said this can be anything because we're not using it.
If we execute this now it gives us an internal server error does because of course that doesn't.
Now there are so let's see slash to the name of song on test 32.
So the reason why is because we definitely changed some values right here are some code right here in where we're start to get to take care of the liter.
But now that's basically how we can just update some data in our in our database if we are using a real data.
It's not like a memory or like I will say a cookie because the cookies in the browser or is it like a memory once we refresh or like a session once we refresh that is gone.
But now, this is how we can just simply do this on I hope you understood why we went about doing these.
So if you read this code will you see our saying if this, whatever the value is is not known, that means it is not empty, then that means there's something there that doesn't want to update it.
So I hope you understood what exactly what we did in this bar.
And this next part, we are going to look into the Delete method.
The delete method is self explanatory, just delete a data object from our database.
What we can just do know is to add a new endpoint, we can say add up dot delete, and then slash delete shouldn't blush let's take the idea of the student want to delete as a path parameter, and then define our function.
Elite student new student this guy ID wants you to be an integer as well we're collecting and now we'll check if the student ID is not in student.
So if he's not a student, that means it doesn't exist and we just return an error that what you're trying to delete is not even in our database, it needs to be in the database for you to be able to delete it to arrow judgment does not exist then you have to create us to know first before you can try to delete but it does not decay, go ahead with our code and delete that that's two different questionnaires ID from the students and they just return back a message thing that didn't deleted successfully now this is a basic way to just be able to delete data or value from a database first come here and test this out.
Now if we play around with the pose first of all we have to create a new object before we can do that.
So let's see do this a for Jerry 20 years let's see year 13 Now let's execute this case study exit so as you can see I press that twice so that's why I say students exists but I come here to my get without really created it when I press it the first time when I mistakenly press it again so let's say to execute we can see we have Jerry 20 years return.
If I come down now to delete you can see we have this delete function.
Now that we have this delete function is is right out now by just typing to an eating execute j student deleted successfully.
Now we might not know if they are related it let's just go ahead and press check to see if we execute now.
He says internal server error does because the student doesn't exist again.
So you also come in now and try to execute again.
He says student does no it is I'm a student I'm trying to delete does not exist.
So this is how we can use the Delete method.
The Fast API geysers gonna be all for this video.
Thank you so much for watching to this point.
In this video we talked about fast API's and all the basics and fundamentals you need to start working with fast API.
Thank you once again for watching this video and I'll see you in the next one.