 Hey guys and welcome back to another Python tutorial. So in today's tutorial we're going to be doing something really interesting which is creating an API which is going to be based on an EPOS system. So this is going to be a really basic tutorial on showing you guys how to create a really really basic API using Flask and the API is going to be around the concept of a restaurant management system or an electric point of sales system which is EPOS. So to begin with what I want you guys to do is open up command prompt so that we can install all the required modules such as Flask which will allow us to basically host our API. So first off open command prompt and for me I usually like to open it as administrator to basically avoid any errors and stuff that can happen. So open it as admin and then pip install Flask. Now it's going to do its thing. For me it already says requirement satisfied because I've already installed Flask before actually starting this tutorial. So that's one of the things you're going to need along with another module that's called Flask underscore restful. So this module basically lets us create a restful API meaning that it's going to give us the ability to create, delete, update or forgetting another word or read data from the API. So that's basically what an API is in general it lets you request or post data to it based on different endpoints and you can also do other fancy stuff with it. So enough of me talking let me actually install this and as you see again it's saying requirement already satisfied because I've already got this installed before actually starting this tutorial. So once you've got both of this installed we're actually good to go. So I'm going to close down command prompt, go ahead into visual studio code and then open up a new file, save that as I'm just going to save it to my desktop for now. Call that eposapi.py and then save that up. Now I'm going to zoom in a little bit just so that it's easier for you guys to see and the first things we're going to do is actually import all the modules slash libraries that we're going to need to basically code this API. So first things first import from flask we want to import the flask class and then the request. We might not actually end up using request but we're just import it anyway. The second thing you want to do is from flask underscore restful you want to import resource and API. Now these two classes are basically going to let us well look at the different data that's being passed in by the user as well as just handle different actions of the API and initialize the API itself. So once that's done we also want to import another thing from flask restful which is basically going to let us pass the data that's being given to us while users do post requests. Now I'm going to explain that a bit more in depth a bit later when we actually code the post requests. Now post request is nothing more than just the user wanting to store some data into a database so they basically send that data to an API then the API will post that data save it up in a relevant table somewhere in database so that it's saved. So we're going to from flask request we're going to import request pass basically what it does is it passes all the requirements or parameters that are passed by the user and then it will give it to us as a dictionary but I will explain that in the end. First things first we want to initialize the flask class and the API library so this is just part of the setup before we actually start coding the actual meat of the system which is going to be a few functions and classes but we have to first of all initialize flask and then the API needs to be attached to the flask web server as well so to start with create a new variable called app and then assign that to the flask class that we just imported and then we will have to pass in name as a parameter as well. Now the next thing we want to do is actually attach this API so the API module needs to be wrapped around flask because it was built on it so what we need to do is create a new variable called API equals that to capital A because that's the class API and then we need to assign that to app so basically what that does is it wraps around the flask web server or the flask class that's all the initialization stuff done. Now what we want to do next is just for the sake of this tutorial and to keep it simple I'm not actually going to implement this with the database but rather save all the data into a dictionary so I'm going to create a dictionary that's going to be called just let's call it database for now actually let's call it RMS which will stand for restaurant management system and then that's just going to be a blank dictionary for now. Now in a dictionary what we could have saved is things to do with the restaurant because an ePort system will of course have to remember things about the restaurant such as the restaurant name the phone number the address and so on and the main sort of thing that the ePort system will have to remember or must remember is the different items on the menu the pricing of the item on the menu and then the inventory such as the item so how many the quantity of the item so let's start by creating a default key in here which is going to be called restaurant and then the value of that key is just going to be let's say another dictionary which is going to be name and we can call our restaurant KFC for now because it's quite a popular one then we can create another key for phone number and the value would just be a random string so 0789 something something something and then we can create another key for address now obviously you can be as creative as you want but just for the sake of this tutorial I'm just going to create three keys and values inside this restaurant key so I'm going to do something like 122 new clothes London right so we'll just do that for now so we've got a restaurant we've got the restaurant details saved up into the RMS dictionary nicely done now we're just going to put a comma in here and then I'm going to create a new key called menu now this key called menu is basically going to store all the details such as the different items on the menu and then the quantity of those different items as well so just as an example we're going to end up with this item whenever we start our server so as an example inside this menu dictionary I'm going to add a key which is going to be called let's say wings and then we'll assign that to another dictionary so basically we've got menu which is the key that's inside RMS and then we've got wings which is part of menu so it's one it's actually the first item in menu now since wings is a key and the value of wings is another dictionary we could add some more details in there such as the price so obviously each item will have a price and let's say wings are pretty expensive and they're 5 pound 50 for now and then another important one could be quantity or remaining or you could just call it quantity I'm just being very specific and calling it quantity remaining and then I'll do something like 30 for now so that basically will be the structure of our RMS sort of dictionary that's acting as a database for now in a in a real-life scenario you would obviously save these details into an actual SQL database or a non-relational database such as MongoDB but just for the sake of this tutorial we're just going to save it into a short dictionary now the API will obviously let us read the data from this RMS dictionary and give it to the user based on different end points update the data in this dictionary and also delete and add more data to this dictionary so we're mainly going to be focusing on this menu dictionary here so once this dictionary is done this is sort of our presets or template because we want to have something in it when we start the server otherwise it's going to get really confused so once we're done with that what we need to do next is let's create a class which is basically going to be called a restaurant so our class is going to be called restaurant and then inside the class what you want to do is as an argument pass in the resource class that we imported earlier so up here we imported the resource class from class gristful we need to pass that class into our restaurant class right here now once we do that there are specific class names that are reserved in here that basically represent different requests so basically I said class names but what I actually meant is methods because since we're in classes we can create different methods or functions so the specific names that have been reserved for certain requests so if I did def and then get which basically stands for a get request and then I can basically type in self and then something like return and I'm gonna say welcome to RMS so what I've just now done here is I've created a class and then pass then resource as the parameter and then inside class basically what flask will do or flask underscore restful will do is it recognize recognizes get as an inbuilt sort of method that will be a get request so every time a get request is made and this class is run basically what we're doing what we're going to be doing is we're going to be returning a string called welcome to RMS now obviously this is not just magically going to work so we have to sort of assign this to something before we actually get it to work so what we need to do is we need to create sort of a map for all of this to be shown so there's different endpoints on an API based on which data is shown so we're going to be creating those endpoints right now so we're going to be calling this mapping classes to different endpoints right so let's call it that for now and then earlier we created a variable called API which was assigned to the API being initialized so we're going to be using that to basically map so we're going to do API add resource which is like adding a page or adding an endpoint if whichever way you want to think about it and then in this what you need to do is provide the API with a class that it can basically run so I'm going to say the class I wanted to run is called restaurant and it's obviously up here so it's readable and then I'm going to say the endpoint I want this to trigger on is going to be forward slash so basically what that means is every time a user loads up just a default page of my flask server they'll end up on this endpoint which is forward slash and then this restaurant class will be run when the restaurant class is run the flask library is going to go oh is there any requests in here that I can deal with and it's going to see the get requests right here and then it's going to say okay I've got a get request so I'm just going to return to the user welcome to RMS now that's the very theory sort of explanation of it but it will make more sense once I start the server now there's only another step to actually starting the server which is adding the main guard or the name guard so we do if name is equal to main and then we'll do app dot run so app is obviously the initialization of the flask sort of web server and then we're going to do debug is equal to truth now the reason we do debug is equal to true is because if there's any errors while running while running the server the server won't crash it's just going to show us what the error is and then it's just going to carry on its operation unless it's like a serious syntax error in your Python code so once you're done with this sort of statements this could just be called starting the server and API that sits on it well it's technically just starting the server the API is automatically assigned to it but anyway starting the server and then once I run this as you can see we get some messages and then we get told what link to actually open as well so this is the link that we want to sort of open up so I'm going to copy that link and what I'm going to do now is what why it's a really good way to actually view this would be opening up a browser tab and then pasting it in there and pressing enter now when you press enter as you see what it goes ahead and does is with all is it says welcome to RMS now the reason it does that is because whenever we type in enter here we're making a get request to forward slash so obviously we've mapped forward slash and the get request to forward slash with returning welcome to RMS now we don't want this to sort of be a thing so we're going to get rid of that and actually start making our API a bit more practical so I'm going to stop our server right now and what I'm going to do is instead of doing return welcome to server I'm going to return the basically I'm going to return the full dictionary that we have so I'm going to do something like return I'll get rid of this I'm going to do return and then I'm going to say RMS because that's the name and then I'm going to say restaurant now what that's going to do is it's going to return RMS which is the dictionary and restaurant which is the key so it's basically going to return the restaurant details so it's going to say name KFC phone number blah blah blah and address blah blah blah so that's what it's going to do makes a bit more sense to have it in the API because the user can basically make a request and it will show up now obviously we can change the endpoint to instead of being forward slash we can do something like forward slash and then restaurant that makes more sense so when the user goes to the address that the server is running on and does forward slash restaurant it's basically going to run this restaurant class and then there's a get request in restaurant class right here which says return the restaurant details so let's run that again to see if it actually works open up Chrome back into it and then I'm going to localhost 5000 which is the equivalence of doing 127.0.0.1 and then I'm going to say restaurant and as you see it works pretty well so now if I refresh this it's going to say not found because we obviously got rid of the forward slash routes and we got the restaurant route in there so if I did restaurant again we get all the restaurant details so as you see name KFC phone number blah blah blah address blah blah blah so that's a basic get request and an endpoint done so that's how you would create an endpoint and basically get a request basically send a response back to a request as well now I'm going to close this off and at this stage what I'm going to recommend you guys do is download something called postman so I'm going to type it in and I think it's the first yep it's the first result that actually comes up so if you guys could actually download this it would be pretty useful because that's what we're going to be using to further test our API along the line now it's nothing too complicated this is what it looks like so if I open it up this is what it looks like and it basically lets you make different requests to your API and lets you test it so if I create a new request in here so you click on new click on request and then or you don't even have to do that you can just click on this add sort of tab button here and then it will be called untitled request or whatever it is and you type in the address where you want to make this request to so one two seven point zero point zero point one five thousand forward slash restaurant now the very important bit is you need to specify what type of request this is going to be so it's already set to get so hopefully that should be fine now when I click on send you'll notice down here that I get the response with name KFC phone number blah blah blah and so and so so basically our request is gone through fine and we've got a valid response as well cool now enough of the really basic stuff what we want to do next is let's just close the server down and what I'm going to do next is add a menu class which is going to help us get different items on the menu because right now what we can do is we can only get the restaurant details but what I want the API to be able to do is allow us to basically get all the menu items either individually or as a complete list as well so let's start with being able to get individual details about individual items on the menu so let's create a new class called menu and I'm going to zoom out of the screen a bit because it's becoming a bit hard to code I'm going to create a new class called menu so class menu and we always need to pass resource through because it's it's sort of like dependencies the way it works and then what we want to do is we're going to be creating a get request and then we're going to call this self now when we do a get request and we want a specific item we want details about a specific item what we're going to be needing is to allow the user to actually add a parameter for a specific item so we've got wings as our menu right so if we did something like this menu we created an endpoint called menu what we want to be able to do is do forward slash menu forward slash wings press send and then be able to get price details and quantity details about wings so that's what we want to be able to do now obviously we don't know what the user is going to type at this end point so we're going to need to be able to make this flexible so that it can be sort of like a variable so what we wanted to want to do is go in here into the API dot add resource section and then we're going to add resource and what we're going to add is the menu class and the endpoint is going to be forward slash and menu because that's the first endpoint we want the user to go to then forward slash again and in small greater than and smaller than size I'm going to create sort of this is sort of going to be a placeholder for whatever's typed in here so it's sort of like a variable so whatever you type in the link is going to get replaced in here now instead of just doing now when we're coding the menu class we've got self over here but we can also get the item so basically whatever the user enters over here is going to be passed in here so when I do this and print item for example and let me run the server quick it's running let's just make that bigger make this a bit smaller and I'm going to say I want to go to 5,000 under menu and then winks so when I send a get request to this endpoint what we get is obviously it says no over here but what you see over here is it says winks so it will actually it actually knows what item is being typed in by the user now if I change this up to let's say burger we should be able to get a response well get get a print that says burger now we basically know the item that the user's adding on so it shouldn't be too hard to progress onward so what we want to do after this is we want to check if the if the item that the user has typed in is actually a valid item on the menu or not so we're going to do a try and accept because if the item is not valid then we'll get a key error since we're checking it against the dictionary so we're going to try returning RMS because that's the main dictionary and then under menu we're going to try returning the item that the user has typed in because if this item is a valid key such as wings then it should show up the properties of it if it's not then we should have a key error so I'm going to do something like accept key error and then we're just going to return an empty dictionary saying that we don't have any data about the item that the user has just typed in let's go ahead and run this to see if it actually works okay and now if I go menu burger now bear in mind we don't actually have burger in our dictionary if I just show that to you guys let's go up here we only have wings and that's it with a capital W so when I send this I should get just an empty dictionary back which I do so basically the API is going oh is this a is burger a valid key in the menu dictionary and it's saying nope we get a key error so it's going to return an empty dictionary but on the contrary if I typed in a valid menu item such as wings it should return the price which is 5.5 and then the quantity remaining which is 30 amazing so we've got the we've got basically different endpoints and get requests working and we've added a little bit of validation as well so now that we've done all of that what we could also do is be able to create a put request so put request is very simple it basically just means update so what if we wanted to update something from the menu so let's say we wanted to change the price for wings or we wanted to change the quantity remaining for wings we could easily do that as well by creating a put request so let's go in our menu class and then create another method for a put request so you may have already guessed since get is called get put is also called put so we just type in put as a method and then flask will recognize that once again self as a parameter and then we go also need the item so that we can actually update the dictionary according to the item name so first things first what we want to do is we want to check if the item that's being typed in that the user want to update is actually a valid item in our dictionary so we can literally copy and paste the lines from before and paste it down here so basically what we're doing is we're checking instead of returning anything though we're just gonna do try and then rms menu item so if it is a valid item then the program will just continue if it's not then we're gonna get a key error and we can return something like an error and then we're gonna say item whatever item they're typed in does not exist on the menu so we've added a bit of validation there so we're checking whether the item that the user wants to update is actually part of the dictionary if it's not then we just return sort of like an error and the function or method just stops there now we actually need to be able to allow the user to add some arguments onto their post requests so that we can update arm we can update our dictionary with those arguments so to deal with that we need the request parser so that's what I was talking about earlier on so we need to first off initialize a parser so we're gonna call this parser and that's gonna be equal to req pass or request pass dot request parser so it's the library we imported earlier on so it's gonna basically let us grab data that the user wants to send to us and we can use that data to basically update our dictionary once you've initialized your parser and assigned it to a variable the rest is pretty simple we need to think we need to basically think about the different the different sorts of data that would be one from the user so one of them is obviously gonna be the item no actually one of them is actually gonna be the price because we already have details about the item from up here so we don't really need to put it as an argument so it's gonna one of them is gonna be the price and one of them is gonna be the quantity remaining so that's the two arguments the user can make so I'm gonna say parser to add argument and then price so basically what we're saying is we want the user to be able to add a price as an argument and then we're also going to say parser dot add argument and then I'm going to add a quantity remaining argument as well now you could have called this anything but just for consistency purposes and for everything to make sense I'm adding it as what it actually means now we've basically allowed the user to send us data about price and quantity remaining by doing this now once we're done with adding these arguments to the parser we actually need to create a new variable called result and then call parser and then do dot pass arguments so what this will do is it will go ahead into the put request and check what data has been sent to us and it's only going to filter out the user data the data from the user for price and the user data for quantity remaining the rest of the data that's posted to us will just or put to us will just get ignored now the result variable is just going to be turned into a dictionary with basically the data that was sent to us by the user so if I do print results at this stage and then if we just run our server we have a key we have an indentation error so we've got up here online 27 back one and try again and our server is running which is good let's go back to postman and now what we need to do is I'm going to go on to menu and then wings which is actually an item and then I'm going to do a put request now if I send this off what you'll see here is when I move this a bit to the left you see that price is set to none so we as I said result is going to turn into a dictionary and it's basically going to look for these details whether the user actually sends them or not now since the user didn't send any price details or any quantity the remaining details in the put request it's saying none now we've also added validation to make sure that it gives an error when we try to update or make a put request to something that doesn't exist so if I did burger and send the error that we created earlier shows up it says error burger does not exist on the menu so that would be the section of code where we have a try and catch for the checking whether the item is actually a key from the menu that's good as well now to be able to send data you basically need to be on the right endpoint such as wings because it's a valid item and then put a question mark in there and then type in your first parameter the first argument that we allowed the user to type or added to the request password was price so the user can type in price and then equals that to something like 20 for example and then any other any other basically any other requirement any other arguments or any other parent parameters that the user wants to add in or any other data that the user wants to send to our API needs to be followed by an and so the other argument we added to the request password quantity remaining so we need to make sure we're typing that correctly and then we'll equals that to something like 200 now what happens there is the price will be updated for wings to 20 and the quantity remaining will be updated to 200 but we haven't actually done the updating bit yet but when I do send what you notice is it no longer says price none quantity remaining none it will actually take whatever values I put in my put request which is here so I said price was 20 and as you see it got that data it says price is 20 and then I said quantity remaining is 200 and as you can see it got that data now what if I misspelled this so if I said quantity something something and send again it's actually going to say none because in our request parser we've created an argument for quantity remaining so we need to be very specific when we're right here so we need to be very specific when we are adding this data on so since we're able to grab the data that the user wants us to update the only thing left to do now is to actually update it in our dictionary so result is what the user senses so data that the user has sent to us and then what we want to do next is update the menu dictionary with data the user has sent okay cool so now when we want to update the user dictionary first off we need to make sure that the user has actually sent some data through because we don't want to save none into the into the dictionary because if the user doesn't send any data as you saw before result gets set to none so we're going to say if the result price is equal to none and the result quantity remaining is equal to none as well then what we want to do is just return an error to the user that's going to say something like update either needs price or quantity or both because obviously without them providing these details we can't really update it and there's no point of them doing a put request if they're not going to give us the price and the quantity remaining that we want to that they want us to update so that's that will handle that and then as else we can basically do the rest of the checks so we can do an else statement up here and then we're going to update the dictionary so we're going to say updating dictionary dictionary and then we can do an if here so we can say if the results and price and price so basically the price that the use data for price that the user senses if the data for price is not equal to none which means it's valid data then we want to go into the RMS dictionary then on to the menu key which will results to giving us access to the the sort of child dictionary then we want to type in item because the item would actually be a valid item since we've already checked that up here so we want to go on to the specific item on the menu and then we're going to do the price and then we'll equal that to result price so we're basically updating from our RMS dictionary the price for the item the user is on to the price the user wants to update it to now we do the exact same thing with the quantity remaining as well so we're basically pretty much repeating ourselves over here where we're checking to make sure that it's not none and if it's not none then we update it so we say we go into menu we go into the item the user is on but this time instead of the price we're going to update the quantity remaining set that to results and quantity remaining hopefully I haven't spelt that wrong anywhere so that will basically update everything and then we can go one end and back and return a message that says that we have successfully updated the the item so we can say something like item plus updated so that will basically tell the user that we've updated whatever they wanted to update with whatever data that basically sent to us so now that we've got the pot working which was quite long but we've made sure that it's pretty robust so we've added quite a bit of validation we could have done without the validation but there they would basically expose our API to the user so that they can somehow break it but we don't want that so now when I run my server if I go on to my put request and I'm going to get rid of this I'm going to change this to a get request and then send and as you see initially I haven't done a put request yet but the price for my wings is 5.5 and the quantity remaining is 30 now if I essentially when I do the put request to wings and then I send new data so if I said price is equal to 1000 which is crazy expensive for wings but oh well and then quantity remaining is equal to one so let's say we just had one pack left when I do this put request what we want it to do is basically updates the dictionary on our flask server so the next time I make a request to menu wings so when I make a get request to menu wings I should see this stuff being updated so when I send this off voila it says menu message wings updated and then if I go ahead and get rid of all this stuff because I'm about to change this to a get request and send it off we actually don't see any updates for some reason but we will figure it out we don't see any updates on the price but we do see an update on the quantity remaining so let's take a look at the thing again so we've got price is equal to 1000 and quantity remaining is equal to 1 let's take a look at our code so I'm pretty sure I might have spelled something wrong with the price I said if result price is not equal to none then we will go into menu the item and then update the price with the the price that the user has sent to us so that should be correct I'm not sure why it is not okay let's try that again so I'm gonna make another request so another put request and send that off it says wings updated so hopefully it will actually show up let's do a get request there and it still shows 5.5 that is that is a bit weird but we can figure it out because we still let me try another put request where we do quantity to one something and then we'll do another put request send we'll go back to wings do a get request and obviously it does update but the price for some reason isn't updating feel like with it's going to be a really simple mistake that I have made I saved this up we've got RMS menu item price is equal to result price then let me just check up here I've got price then I have if result is none and okay is equal to none we give an error else we do this don't seem to see anything wrong but let's just try it again so we'll go do a get request and it should be 5.5 and 30 which is we do a put request and then I'll send all the data that's required so we've got price is equal to 1000 and quantity remaining is 10,000 let's send it off do a get request get rid of this data here send again and voila turns out it was just a need to refresh my server which is a bit crazy because I would have been spending ages just to look for it but either way the put request is now successful because we've been able to update the price and quantity remaining for wings now if you had additional items you'd be able to do that for them as well which is pretty cool now we still need to go through two more methods which is a one one of which is a post request and the other is a delete request once we're done with that we have basically created a a restful API that allows us to create read update and delete which is pretty cool so once now that we're done with the put request and it's working fine we're going to go ahead and explore how we can basically delete we can how we can delete a menu item using the API as well so what we want to do is inside the menu class again we're going to scroll down past put past the first put request that we made and I'm just going to check that I'm on the right indent I am and then I'm going to go ahead and create a class for delete so death delete and then we're going to pass in the self and item as well because we want to know what item from the menu of course the users trying to delete so now we like always we check if the item is actually valid so if it's actually in the dictionary so checking check if the item is valid do a try and then rms menu item now obviously if it's if it doesn't exist and we shouldn't get a key error so accept key error return error and then we can do item plus does not exist so that will sort that out so we're basically validating to make sure that the item is actually valid and then what we want to do next is if obviously the item is valid then deleting item from the menu so we just use the delete command or Dell and then we do rms menu and then item simple as that so basically saying go to the rms dictionary go into the menu key and then in the menu key if you see another key called item just delete it off so obviously item is going to be replaced with whatever endpoint the user is on and then we probably want to return a message to the user saying that the item has been deleted so return message item plus space deleted so that should look after the item item being deleted and that's how you would create a very simple and basic delete request as well so let me run this up just to make sure that it's actually working so servers running fine which is great news I'm gonna do a get request to menu wings and then send it off and as you see prices 5.5 again because obviously we restarted the server so everything defaults back again but obviously if I did a put request and updated the price and quantity remaining that would work as well so let me just do a put request and then put the price to something weird like a hundred and then the quantity will just do it let's say 20 send it off and go back into wings do a get request send and obviously it's been updated now let's let's say we wanted to delete delete wings the only thing that would need to change is the type of request so we'd have to change the get request to a delete request because that's how we've coded it and then we're basically saying we want to go on to the menu and then we want to go on to wings so we want to delete wings we want to make a delete request to the wings endpoint so it basically deletes wings from our RMS dictionary send it off and let's hope it works and it says wings deleted now if I change delete to a get request to try and get wings let's send it off and we get return nothing because wings does not exist anymore since we just deleted it and hence we basically get the empty dictionary since we coded it before now if I try to delete something that doesn't already exist we should probably get an error so try and delete that it says wings does not exist which is perfect we've coded validation for this as well amazing so now the only bit that's left is to be able to create a post request where we're able to add new items onto the menu as of now we only have wings which we manually added to the dictionary but we want to be able to make a post request and then add those items to the menu so let's close this off and what we actually want to do for this is we want to create a new class and I'm probably going to call the class let's call it main for now so let's go above here and then obviously we have a menu class down there but I'm going to create a new class and then I'm going to call this main pass resource brew again because that's important and then the first thing I'm going to do is I'm going to create a get sorry a post request and then in the post I'm just going to pass self now one thing you may notice is for a post request you don't really need to be on an endpoint you we do we don't need to know where we're adding this we just need to be on the menu endpoint and then we can make a post request passing the details for the item name the price and the quantity remaining that's why we don't have item as an extra parameter here now that we have a simple post method in here I'm going to add this to the API resources so let's scroll down a bit and then I'm going to do api.add resource and then I'm going to say main because that's the function I want it to run and in main what we want to do is we're going to assign it to the roots forward slash menu so basically what that's going to do is going to say anything any post request or any requests that come on the forward slash menu are going to be forwarded to the menu class so let's go up here and we've got a post request so let's start coding our post request so what we need to do first is create a parser because obviously we need to get the data that the user is sending so like before we have to create a parser so I'm going to copy and paste the lines one before because it's going to be very similar copy the parser and the bit where you add the arguments and also copy the bit where you pass the user data so I'm going to paste all of that in there so we're basically creating a new parser to deal with all the data the user is going to be sending us now we need an extra argument which is going to be the item since the user is adding a new item to the menu obviously we want to know what the item is going to be so item is going to be a new argument that the user can specify in the post request and then once the result runs the we're basically result is going to turn into a dictionary that's going to have item price and quantity remaining so we can simply add that to the rms dictionary now we need to run a few checks so we need to make sure that none of these none of these items so item price and quantity remaining we need to make sure that none of them end up being none because we want values in all of them otherwise it's going to be a bit weird so we're going to run a validation we're going to say if result item basically the data the user senses if the item from the data is equal to none or the price from the data is equal to none or the quantity remaining from the data is equal to none then we're just going to return an error and the message to the error is going to be missing fields right because we basically need all three fields in order to add an item to the menu make sense because we can't add an item without the price and we probably shouldn't add an item without the quantity remaining because that will ruin the integrity so if this is not the case and if all three items obviously have the data that they need then we can go ahead and add this onto the dictionary pretty simply so we're just going to do adding item onto rms dictionary and then we can do something like rms then onto the menu key and then the next key is going to turn into the name of the item so we're going to go into result and then item because that's the name of the item and then we're going to assign that to a new dictionary which for which the price is going to be the price that the user sent to us so price and the quantity remaining is going to be set to the quantity remaining that the user just sent to us so basically all we're doing is we're going into the menu dictionary adding a new key which is the key name is going to be the item name that the user just sent to us using the post request then we're assigning that to another dictionary just like we did with the wings where the price is going to be the price the user sent to us and the quantity remaining i need quotes around that by the way just add quotes and then the quantity remaining is going to be set to the quantity remaining the user just posted to us as well oh why did i do that okay going back to it again where is it now there is going back to it again this should basically do the job for us now the only thing that's left is we can do a return statement here which is going to return a message and the message is going to be let's just say an f string because we want to be fancy and the message is going to say result item so basically the item name that the user sent to us added to the menu successfully and voila that should do the job now that's the post request you might say it's very similar to a put request and it is because all you're doing is you're grabbing data from the user and then in the case of the put request that data already exists somewhere you're just trying to update it with the data users giving to you but in the case of a post request you're adding it to something so it's fresh data so i'm going to run this again hopefully there's no errors nice and we're going to go in here and i'm going to do a get request to menu obviously nothing shows up but if i did a get request to wings something should show up and we get price 5.5 quantity 30 so let's do a post request to menu and let's send it off and it says that we've got basically a 404 but let's try doing a post request again and obviously i was getting a 404 because i had an extra forward slash in the end these type of things can cause errors so we did add validation to make sure that the user obviously needs to add the item name the quantity remaining in price if they don't provide that data then we just return an error which is doing perfect so let's actually provide the data let's provide item name to be let's say wings no not wings because we already got wings so we'll do burger let's send it off and it should still give us this error because we're still missing the price and the quantity so let's do price as well this time price is equal to just to troll 10 pence and then let's send it off still missing fields because we're missing the quantity remaining i do and quantity remaining is equal to 100 and now if we send it off we have an error because it says item is not defined okay let's go back in here and it says item is not defined result item okay that's because item actually let's go back in here result item oh it's because item is obviously needs to be in quotes since it is a key and not a variable so let's run that again that was very silly of me we can basically just run the same query again so run that again and it says burger added successfully to the menu now the only way to verify this would be to do a get request under the menu and if we did wings here obviously we get the price for wings and quantity remaining for wings but if we did burger here as you can see the post request did actually work because now we're able to grab the price which was 0.10 that's what we set it to and the quantity remaining we did set it to 100 so it's working flawlessly now another last thing that i would like to do with this API is to be able to return all the items on the menu if the user does a get request to just menu because it'll be so much easier for the user to just be able to get sort of a dictionary with all the items on the menu and all their details rather than having to go forward slash burger because bear in mind a user that didn't use your API might not know what these items are so it will be a good functionality to add so let's do exactly that let's close this off go into the main again and we're going to do a get request so we're going to say def get and then self now bear in mind this time we're not asking for an item because we're not going to request or return data about a specific item we're just going to return everything that is in the menu dictionary so we're going to return rms and then we're just going to return menu so that will basically just return the entire rms menu that we have if it's empty it's just going to return an empty dictionary so that's pretty much the final ending touch that i wanted to add to this let's see if it works let's go ahead and try that off now if i go to menu let's just make sure it's not in full screen send that off as you see i get wings price 5.5 quantity remaining now i no longer have to go into wings because oh wait why did that uh oh of course because i have to go into menu first then wings so i no longer have to go into menu and then wings i can just go into menu and be able to see basically all the items inside menu now you might say it looks the same but obviously that's because we only have one item i can prove that to you by making a quick post request so let's make a post request to add an item and then we'll say the item is equal to burger again and price is equal to 10 pounds and quantity remaining is going to be equal to 100 let's make that post request burger has been added successfully now i can prove my point by going back to the menu end point and then just doing a get request and i should be able to see wings and menu so as you see in the correct order as well it says wings first and then burger now obviously we could change the order and stuff if we had a more more usable more viable more generic database which we could just make a query to to sort by but i'm pretty sure we could sort by keys as well if we added timestamps in here but that would just make things complicated either way guys that was just a short tutorial on how you would create a simple basic api using flask being able to use all the methodologies from crud such as create read update and deletes so that's all the points we covered while also being able to wrap this logic around a sort of real-life ecosystem hope you guys have enjoyed this tutorial and were able to learn something new from it i would like to apologize for not posting for so long but i would try and push out at least one video per month now that i'm back if you guys have any ideas as always make feel free to join my discord chat and post your ideas there or you can leave them in the comments as well if you would like to subscribe go ahead and do so if you'd like to like or any of the following go ahead and do so i would really appreciate it as it really helps and guys thank you so much for sticking around i will see your beautiful faces in the next tutorial peace