Learn how to build your first API | Valentin Despa | Skillshare
Drawer
Search

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Learn how to build your first API

teacher avatar Valentin Despa

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Introduction

      1:16

    • 2.

      Introduction to Postman for working with APIs

      3:45

    • 3.

      What is glitch?

      1:30

    • 4.

      Create an hello world API

      6:07

    • 5.

      Troubleshooting problems

      6:29

    • 6.

      Defining the database

      5:10

    • 7.

      Getting data from the database

      3:37

    • 8.

      Save to the database

      5:58

    • 9.

      Assignment

      0:14

    • 10.

      Assignment solution

      1:19

    • 11.

      How to read data from incoming request

      4:46

    • 12.

      Deleting data from the database

      8:25

    • 13.

      Validating data

      9:16

    • 14.

      Conclusion

      0:33

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

338

Students

2

Projects

About This Class

APIs are the bread and butter of how the Internet works. Every app you use on your phone, every web application used in the background APIs.

In this course, you will have the chance of expanding your understanding of APIs by actually building one. We will use a fun and easy platform called Glitch.

You can use Glitch for free to write the code needed and also to deploy it. Trust me, it has never been easier.

What are the requirements for taking this class?

While it is not mandatory, it will make things easier if you already know what an API is and what are the basics around HTTP.

Do I need programming skills?

It won't hurt if you already know a thing or two about how to write code but this is not a requirement. I will take you step by step through the process. However, this class if focused on building a simple API, and less on programming.

Do I need to install any software?

No. All you need is a browser and a stable internet connection. 

I will show you how to use the API in Postman, but even Postman has an online version that you can use. 

Does it cost anything?

No payment is required. The free plan on Glitch is more than enough to complete this class and build a few projects on your own. No credit card required or anything like that. 

Meet Your Teacher

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: Hi there, my name is Valentine and I'm a software developer. Practically all companies nowadays use APIs in their day-to-day activities to get data from others or to exchange data within the organization. Apis are the way to go to integrate the different IT systems that we have to make them work together as a cohesive system. I know that for many of you, what an API actually remains a black box. In this class, you'll get opportunity to build your own API based on the principles I will demonstrate. Together, we'll be creating an API to help a pizzeria tick to go orders from customers. In this process, we'll be creating a rest API by adding endpoints and learn how to read and write data from a database. If you have never written code before, this will be challenging. But if you want to get your hands dirty and don't mind encountering some programming errors. Be my guest. I'm sure the experience will be very rewarding in the end. And don't worry if you get stuck. I'm here to help. So let's get started building APIs. 2. Introduction to Postman for working with APIs: In this lecture, I'm going to introduce you to a development and testing tool called postmen. Bozeman is probably the most important tool that you need to have installed on your computer when working with APIs is already used by 10 million people around the world, by 500 thousand companies. And a lot of APIs are being developed and tested using Postman. And postmen is initially free to download and use. So in most cases you do not have to pay for it. So right here from postman.com, click on download the app. The app is available for Windows, MacOS, and Linux. So all operating systems are supported. Now, click that Download button if it has correctly detecting your operating system or if it's not a case, you can download it for Windows or Linux or whatever else you have here. The installation process of postmen itself is pretty straightforward. And when you open the app for the first time, you may be greeted with such a screen. So on this first screen, you are asked if you have an account or if you want to create with an account. I highly recommended to create an account. So you have to click here, create an account. Or if you already have a Google account, you can use the Google account and to do a single sign on with Google. So that is one way to do it. If you don't want to create an account, you can simply click somewhere here below. And we'll have something like take me straight to the app. And that will allow you to use the app without creating an account. But as I said, I highly recommend going ahead and creating a new account. I'm going to go ahead and create a free account. And there's of course, some additional information that you are being asked here in this configuration guide. Now unfortunately, there's no direct way on how we can skip this. So just try to do your best and fill out the required information and continue. You don't need to set up a new team, so click here on maybe later. And then you will be taken wide in Postman. Now, initially you will see this launchpad. It provides an overview of everything that you can do with Postman. But I'm going to close it for now because I will guide you through postmen step-by-step and to make sure that you understand what's going on. Initially, I just wanted you to have this tool installed on your computer so that you can follow along with the next part of the course, which will be totally hands-on. So now we have Postman installed, and we will use postman in order to submit API requests to different servers and to try to understand how HTTP works, how APIs work. And postmen is the tool for this, because postman allows you to craft HTTP request by setting different parts. So for example, if I'm opening a new tab, will be able to see here that you can select the HTTP request method. You can type in the request URL. You can set parameters, you can set headers, you can set the body. So there's a lot of things that you can control it postmen that you would normally not be able to control from your browser. Remember which browser we can just send a get request and inspect some information. And that may work for some APIs. But if you want to get into more advanced stuff and really understand how HTTP works, you need a tool like Bozeman. The postman is the tool out there that is the most popular and really well-designed and easy to use. It may look scary. In the beginning, I will take you step-by-step and make sure that you understand how to use postmen in order to understand APIs. 3. What is glitch?: So let's first quickly talk about what glitch is and why are we using it? Which is this amazing online platform. You can even call it a community that allows users to build on applications. And a lot of projects are being published by other users. So you can use existing projects to build your own. And you're going to see in a second what that means. Typically, users can be built for building web applications like web sites, but also application that only serve data, like what we are building an API which offers many things that makes our life easier, offers a very nice code editor where we can write code, but also takes care of the deployment process and gives us a lot of those that help us understand what is going on. With that. I am here at github.com. To get started with an account, you can either click here on joint glitch or click here on signup. Center process is very, very easy. You just have to select an existing account that you have with Facebook, GitHub or Google, or maybe even other options. In just a few seconds, you'll have an account. So I'm going to do just that. So after logging in, this is exactly what I'm seeing. There are no other projects, so you will see next time when we create a new project, you will see your projects here. But right now this is what you will have to get started. And it's perfectly if you manage to go so far, now you have an account with glitch. And in the following videos, we'll start building the IPI. 4. Create an hello world API: In this lecture, we're going to use glitch.com to create our first API. So yeah, let's get started with that. Once you're logged in in village, you should be able to see that you don't have any product. So what I'm gonna do next is go here on the search button. I'm going to search for an existing project that I created for this course is called pizzeria Valentino in it. And should be very easy to spot. It has also this material logo here, so just click on it. So this will show you the project and we can click here on View Source. And this is how our project looks like now, this is the way I have created it initially. Now in order for you to make changes, we need to sort of we'll make a copy out of it in order to edit anything here, you have to click here on remix to edit. Or additionally, there must be an option somewhere to remix it. So I'm going to click here on remix to edit. And practically when I'm remixing, I'm taking an existing project and I'm taking all the code and all the settings and everything. It's what is in that project. And I make a copy of it so that I can edit and has absolutely nothing to do with the previous projects. So this is why I created this initial project just to make our life a bit easier when we're getting started with this. So as soon as the project has loaded, you'll sort of see this interface, this server.js. This is the main file that we are going to write, the code. This is where we're going to define what our API does and so on. You don't have to worry about the rest of the files, but any other project will contain also additional files that are needed for it to run. Also here on top, you can just rename this project as you wish. Surgeon a main, I name mine materia Valentino. You can call it criteria as you wish. So just simply rename there. You're probably wondering next, okay, so I have this code here. How do I know if it's working? What can I do with it? I'm going to show you in a bit how to get started with it. We'll see you right on top. You have this button show. What you can do is click here to be displayed in a new window. So what this will do is we'll open up a new tab on your browser. And we'll show you here an error cannot get, it cannot get slash. And that's totally fine. It shows that this is working. So let me give you a quick idea of what's going on here. Why do we need this code and how everything is working? So as I mentioned, the server.js file is the place where we're going to make changes. The code that you see here is called JavaScript is a programming language, very, very popular, typically relatively easy to use. Now here in the beginning, you see that I've tried making some comments just to give you an idea. In the beginning, we're loading some code libraries. These are some additional code that will make our life easier and will enable us to write the API much faster than it would have to write everything from scratch. Additionally, here we are going to also define a database that we're going to use. This will be a database that will be saved on a file, so it will be a file based database, nothing to worry about. Finally, here, we're going to start actually defining our API. And the part that we're definitely interested in is this would end point. So what's happening here is that I have defined a status endpoint. The way I've defined it is by saying app, dot, get and get the HTTP method that I want to use. And here I defined this value slash status. So let me quickly explain what that does. And I'm going to go directly in postmen gonna copy the address of the applications. You will see in this case, my name is pizzeria dash fu, that glitch that me. If I go in Postman, open up a new tab, paste the address here and then write status. You will see that I get back. Okay? So this ok, came from here. So now let's try and make a small change. So for example, in an R right here, low and don't have to touch anything. I don't have to change anything. The application will be automatically deployed in the background for me. So this is what glitch does. It publishes the API to this address. So if I go back here in postmen, going to see that if I click again on sand, this comes back hello Valentine, and this is the address, so this is the name of the server, and this is the first endpoint that we have defined. So it's not doing a lot. But at least this endpoint is telling us that everything is okay with application. So we know now that the applications working. This is actually the first change that I invite you to do to this project. Make us more change. See how the application is working. Again, you can use here the show panel to display the address in case you're not sure what the addresses. So it once again, you can simply click here to be displayed on a new window. Now this will be the address of your application, will be a different address than what you see here. Just keep that in mind. And you can change that address by renaming your project. And that can be done also pretty easily. If you manage to make a small change and get it deployed and cesium postmen, Well, I have to congratulate you because you have just managed to create your own API. And based on this small step, you will see that we can proceed to add more endpoints and add more functionality to this. 5. Troubleshooting problems: So in this lecture we're going to take a look at some of ideas how to troubleshoot any issues that you may encounter when using delete.com, which is an online service. And from time to time, the user interface features that you see may change. May be buttons will move somewhere else. And over such changes, I have absolutely no control. I'm sure that main idea, the main way on how you can interact with this, will be pretty much the same. But don't get frustrated if I'm showing you a button somewhere in the application and you cannot find anymore. If maybe it's somewhere else, maybe has moved, maybe has a different color or something like that. Expect that not everything will be absolutely exactly as you see it in the video. That's just part of the process when dealing with such external tool. When you're loading the interface, when you're loading this part where you can edit the code, the code editor, you may face some issues loading the page. And if that's the case, if you see like it's loading and loading and nothing is happening or you're getting some error messages. I would recommend that you check your browser is up to date. Maybe try different browsers. Typically a browser like Google Chrome or Firefox is a good choice. Because if the, if you keep them up to date, these kind of applications are tested on the latest browser, so different BME issues there. So definitely if it's not loading, if it's slow or something like that, make sure that your browser is up to date. And as soon as you start making changes, it's important that you don't break anything. Or if you break something, you understand that you know something broke and how you can fix it anytime you make a code change. For example, here I am inside, okay? I am removing this double-quotes. This will be incorrect because here I'm trying to input a string and I'm opened here, quote, double quote, I've not close it. So you can see it's marked here in red, so it's under DOE K. You see some red will also here on the left side you will see this red bubbles. So this, if you see something like this, means that there's something wrong with this. Additionally, if there's something wrong with the code, you'll also get some indications here below. So you'll see here in tools that there is an error. So if you click on it, you'll be able to see the logs. And the logs will also give you more ideas why something went wrong. You know, just in case you ever get stuck and you have no idea what's going on, make sure that you send me a screenshot with the code that you have with also opened a log so that they can see what the exact error is. Ideally, you should try and figure it out on your own. It's the fastest way to find out what's going on. But just to give you an idea how you can, how you can do something like this. It's available there. So I'm going to fix this bag. And then you will see glitch is starting to deploy the application again. Showing no errors. So everything should be good. Just in case you want to go to the logs again, simply click here on tools. And you'll have to possibility here of opening the logs and keeping the logs open from time to time is a good idea because it shows you a bit what applications doing. This case, you see the last message here is your app is listening on this board. So very technical message, Don't worry about it, but it's showing that it's doing something. Actually this information is coming from here. So every time the application starts again, this message here will be displayed because the application is running. If you need to close the logs, you can simply close them from here. Sometimes it may happen that you make a change somewhere in the code and nothing happens. You go to postmen or you're trying to address once again. And instead of Mozambique, you have changed here your name. You can still see, okay, even though the code is different. And this can be very frustrating. In case that happens, go to Tools and click here on terminal. But this will open a terminal where that application is running and will give you some additional features that are normally not available. So this really allows you to do really complex stuff. Now the only thing that I invite you to do at this point is to open the terminal and typing the command refresh. You type refresh. This will force the project to reload and cause a redeployment. So just in case do you think that application got stuck, it's no longer updating or something like that. Opened a terminal type in the refresh command and this will restart. This is just an idea how to do debugging shouldn't happen very often, but it's a good idea to know where to find it. Tools, Terminal, and a refresh command. Finally, as glitch is an online server running on a complex infrastructure, sometimes maybe it is their fault. Maybe something is going on. I have this page status that glitch.com here to show you if there's any problems, if there's anything that's not working, especially would be interesting to see here if the editor is operational, probably also the API and the community side Project Hosting. So just in case something here is not operational areas indicating any problems. And you can even scroll here below to see if there are any past incidents. This will give you an idea of how the application itself, the platform itself is running. So of course, if you are facing some issues and it says here on glitch that know something is broken, then, you know, that's normal. But if you are facing an issue and here everything seems to be okay. Most likely either your browser or there was a problem with the code or follow any of the previous step that I've mentioned. Just in case you cannot figure out what's going on, feel free to use the Q and a section to write me a message, but make sure that you include as much information as possible. Please add screenshots, please add your code. I don't have access to your computer or to your project to understand what's going on. So from that point of view, always make sure that you share this information so that they can help you out if you are just seeing is not working. I have no idea what is not working. So I'm more than happy to help you, but help me help you with this. 6. Defining the database: Our pizzeria works with a database to store information about orders, but also what is available on the menu of the pizzas. So far we have defined a database, but it's absolutely empty. We are storing two kinds of objects in our database, and one of the objects is pizzas. So what kind of pixels we have available on the menu? And the second object that we're storing are the orders. So this light the objects that we're storing in the database. Now both of them are empty. We'll see here that this is an empty array. And here is an empty array because of this square brackets that have nothing between them, this means that the array is empty. So what we're gonna do next is to define our menu. Let's what's available in terms of options to select from. So I'm going to define here variable and then start with const. So this defines a constant. And Pietas is the name of the variable that we are going to use. I'm going to start here with a square bracket and going to also ended. And I'm gonna open this curly brace that will also be ended. We have defined an array, and inside that array we have an object. So I'm going to start adding properties to this object. Now the way the object looks resembles very much. Json is just a JavaScript object that you will see a lot of resemblance with Jason here. Whenever defined, for example, a property called ID. We don't have to put it between double quotes as we did it with Jason. So that's a difference. But many of the roles that you have noticed in tourism still remain there. So we have a property called ID. Gonna use ID, one can also define a property called name. So this would be the name of the pizza. And between double quotes, let's say the first pizza is prosciutto phony. And let's define a final property called price. And prosciutto fondly will be 1299. So this is the first pizza that we have available and gonna copy the structure. And I'm going to make sure to add here coma after each object. Otherwise this would not be a valid object. So let's do this four times. The first ID, second id, the third AD, and the fourth ID. Let's play a bit with the prizes. So let's say 1399, and this will be only 999. Also, lets write some other names. So this would be, say, a margarita. Another pizza can be a carbonara. And our last pizza menu, fruit in America. So this is the most expensive one. This is what we have done so far. So we have defined this object which represents an array. And we have defined all these beetles that you see here. Just to make our life easier at this point, nothing will change in the database itself. We have only defined these variables. What we need to do additionally is to use the database object. You'll see here DB. And on database objects to I'm going to use here a function which is called set. So practical setting data. First we have to say what we're setting. So we're trying to set this Pietas property or type of objects that we're storing, and also to provide the data. So the data can be provided either by referencing this variable that we have here, or by directly inserting the array. I just wanted to make it a bit easier. Then we're going to simply say, Right? So it's sort of like make sure that you save this information. Now, right? You can notice here that our database is stored in a file. Now unfortunately, you will not be able to see this file here because this is not the public file, not one of the files that you will be storing directly in the project, sort of like private data. But if you copied this address that you see here, you'll be able to go in Tools open here the terminal. I will use a command called CAT. Cat. I'm going to simply paste the address to the file to our database that I copied from the code. What will happen here is that all going to get the contents of the file. I'm gonna get contents of the database. Now this is a database that is stored in a JSON format. And you can see here now that everything is using the JSON format. So you will see we have two objects. We have the pizzas object and we have the orders object. And of course now the pizzas have been already populated with our menu. Our job in the following lectures is to start accepting orders through our API. 7. Getting data from the database: Before we can start accepting orders, we need to tell everyone what we have on the menu because nobody else from outside will be able to see our code a little bit to see what we have defined in the database or what we have inside the code. So we need to define an endpoint to give this information about all the pizzas that we have available. So now inside the code, we're going to go here in the API part of our code. I'm going to simply copy this code here. So this is the end point that was this finding the status endpoint. We're going to simply copy and paste it here. And this will be the pizzas endpoint because we want to get here a list of all the pieces that are available. Now instead of status, I will use pietas because this is how I want this endpoint to be called. If you call it, you'll get list of pizza. So that's a normal thing to do. Before we continue. Let's simply right here in the response, something like pizzas, and first give it a try to see if this new endpoint has been deployed and is working supernatant gonna go to Postman. Here in Bozeman, I can simply open up a new tab and right, pizzas. And you will see now that this endpoint has been defined. And then getting here back pietas, if I don't define an endpoint in my application, it doesn't exists. Or if, for example, I try here to get the orders, you will see that you get a 404, cannot get orders because I haven't defined any order. So this is why it's working or not working. You get a 404. Make sure that you check that the address that you have defined inside your code in glitch is the same with the one you're trying to call. Now that we have established the endpoint is working, it is time to read data from the database. Now imagine the following. We're going to define here, constant. So this is a variable. Let's say it's simply data. And from the database, now we are trying to get information. So we're gonna say ket, and we're gonna specify that we are trying to get pietas. We're going to also call another function that will simply tell us, give us the value, give us the information that's available in there. Don't have to worry too much about Understand this entire code. It is not a programming class. It's simple. Ok, if you follow along, if you do exactly what they do to still give you an idea of how things work in terms of APIs. And now we have this data here available instead of giving a string here as we did before. So response, send this pietas that you still see in Postman. Replace this and make sure that you replace and remove also the quotes. And we are going to give you this data object. So without the quotes, we are actually giving a variable. If you would have used the courts will just giving a string. Going back in postmen gonna submit this again. All of the sudden, this is starting to look like an API. So you will see here the exact data that we have in the database is a JSON format. It looks very nice. So unordered list of all the pieces that are available with ID. So this is how you can read information from this simple database and make it available through the API. If we wouldn't have defined this endpoint, this data wouldn't have been available. 8. Save to the database: Now that we have a menu representing all the pizzas that someone can order, it's time to get started working. And our orders API that can take orders from customers. We're just taking to go orders. Were trying to keep the numbers simple, but still important that we saved them in a database. So let's go ahead and define an orders endpoint. Again, I'm going to copy the code that we already have here just to make sure you're not making any mistakes. What you see here in gray is a comment, so it's something that's not executed. You can write anything there. And just as well, you can write something like this here. This is a common. Now for the orders and point. As you remember from rest APIs, we want to give a significance to the HTTP verb that we're using. So we get, we have been getting information from the database. But now that we want to create one, to create an order, it makes sense to change that word. So instead of get, we're going to use here post. Which means that if you try to get this endpoint, which is currently pizzas but will be orders. If we're trying to get his endpoint with a get request, this endpoint will not exist because we have defined it only for the post HTTP requests at forget, as we've done with pizzas and status for example. Let's write here the response order received. And I'm going to delete this. So the first step, as always, just try to make sure that this endpoint works. Going back to postman opening a new tab. Now as you remember, if I try we get this shouldn't work. The endpoint doesn't exist. You see here a 404 status code is not there. If I select post, the endpoint exists now it says order received, but of course, we haven't sent any data and we definitely haven't received any data. So we have to change a bit application. We have to try to understand how we can save information. So let's start thinking about the structure, the kind of data that we want to save. Now defined here and the object will start with the name of the variable will be order equals. And now we start adding some properties. For example, will need an order ID. This is something that typically generating by a database. But since our database is pretty simple, we're going to use an external tool that were generated unique ID. We want to uniquely identify that order. So we need like an unique string or unique number that doesn't conflict with others. I'm going to use here this function called nano ID and bio-waste answer wondering, where does this come from? This one of the things that we have loaded here in the beginning. So this is one of the magic of using other libraries, other pieces of code in our code just to make our life easier. So this will take care of generating a random string so that we don't have to do it. The second thing that we are interested in is for our order knowing which Pizza customer one to have. So for that reason we're going to add another property called Pizza ID. And let's say the PID is here too. I'm just hard-coding this information just to make sure that everything works properly. And finally, would be nice to have the customer name. And the simplicity here, John. And this will be the data that we have. So this is the data structure. This is how we would like to save the information in the database. We know who ordered it, we know the ID of the pizza. So by knowing the idea of the pizza ID to simply means that the customer has ordered pizza margherita and will also generate a unique ID. Of course, we can add other information like date of the ordered and so on. But going to make things as simple as possible. Now, so far we have only defined here variable, this is doing absolutely nothing. We need to save this information in a database. We're going to use DB that get to get the database to tell that we are trying to get the orders to want to work with the orders object, but we don't want to get them to retrieve them to get the value of m as we did previously with the pizzas. We actually want to add additional information. So we're adding additional information. We're going to use push. Pretty, pretty simple. We're going to reference the object here. So I'm going to say what we want to push, what do you want to add? We're going to add the order. So this is the order. And we are also instruct to right. Now we have here the order. So let's do the exact same thing and take a look at what's going on in Postman. So I'm gonna submit this again. Here. Nothing has changed. Here unfortunately, again, we have to look into the file to see what's going on. So I'm gonna copy again the path. Open the Tools, open a terminal cat. You'll be able to see now that in the orders object, we now have an order. So this is the random idea that has been generated by this library and will be all the time different. This is the pizza ID and this the customer name. We don't need more than that. And we have just demonstrated how to save data to the database. 9. Assignment: Opening a terminal, trying to see how the database looks like is a bit annoying. So for that reason in this assignment, it is your job to create completely new endpoint that will give us all the orders that are available. 10. Assignment solution: So in order to create the orders endpoint, we don't have to do a lot. We already have a pretty good idea on how to do that by looking at the pizzas endpoint. So I'm gonna copy that, paste it here. So will be the orders and point and we can even rename citizens a place order and point. And this is the get orders. So just for us to know that we are trying to get into situations, of course, we are not trying to get pizzas, were trying to get orders. So the end point will be orders. This time we're going to use HTTP get. So app.get will make sure that we use HTTP GET. And of course we're not interested in pizzas. We are gonna get orders. And these are the only changes that are required in order to have the orders and point. It's going Postman and take a look. So here instead of post and honest, simply switch to get. And I am here at the orders and point. And you will see that I only have one order. Still is much easier to use this end point to see the data in the database. Then looking inside the JSON file. 11. How to read data from incoming request: So far our orders endpoint is pretty useless because we have some data that is hard-coded, that never changes. And we completely ignoring anything that's being sent by the client. In this lecture, we're gonna take a look at how we can read information from the request and use that in the script and save it in the database. So going back here to the request, let's try to understand what we are supposed to send. We're not going to send the ID. We want to send a pizza ID and the customer name. So this is the only information that we are interested in sending. So going to postmen gonna select here a post request. I'm gonna go to the body, select row. And from the drop-down and select Jason. So this wall sent, this will allow us to send JSON open here. Practically what is adjacent payload JSON request. Now, since I copied this from JavaScript, as you remember, this will not be valid decent. So we need to put that between double quotes for it to look as it's supposed to look. This is practically the data that we're sending. So let's send for example, or sunny here, p psi d one. And postman, we can even do like very interesting things like creating a random variable. So this random variable will allow us to have some RAM that data hearing for the name. Let's look for random firstname. This is just a way to make our life a bit easier when we're sending data. At this point, if we send it, we'll just gonna create a data with the Harcourt information. Just want to have to post requests prepared with everything that we need, just to make sure that when we get back, everything is working properly. Here inside the code, we still have a few things to do because the information is hard-coded in, we need to read it from the request. As you have noticed here, we have two variables available. One is the request and one is the response. In any HTTP communication, we have the request that's coming from the user and we're creating the response. You always see here. We are writing response, send, and we're deciding what to respond with. What is the data that we're sending back. We can send ok. We can send data from the database and so on or the received. At the same time, we still have the request. And so far we haven't used the request in a way because the user has only entered, we have used Postman only to access a specific endpoint. We got information there. We're actually sending information to the server, pool, our application in this case, and we need to read that information. So for that reason we have to use requests. So we're gonna replace, for example here pizza instead of this id to kinda right here, request dot body. So inside the request object, we are getting the body property. And the body will contain pizza ID, at least we expect it to contain this property. The same will happen here with John. So instead of John, We're gonna use request dot body, dot customer. Now I need to make sure that what you're sending matches what you're reading here. So Pizza ID is not the same with just pizza or customer is not the same with customer name. So if you're expecting Customer Name, this is what you need to send from postman as well. I'm going to demonstrate that DID stays the same. We have now the PID, which we are reading from the body of the request. And this kind of a convention here that we'll be sending JSON and also the customer will be coming from there as well. So going back to our request, we have the PID were generating a random customer name. So I'm gonna hit here submit. It says order received. Let's check if the order has indeed been received. We're making here a get request on the orders and would be able to see that indeed we have two orders. This is the second order that has been generated. Bozeman has generated a random name for us markers. We have the pizza ID, which is one, and this id here. At this point, our application is now useful. We can take orders. And that is absolutely great because now our cook can start working on pizzas. 12. Deleting data from the database: Now assuming that the customer has already paid for the pizza, has picked it up, we no longer need information about existing orders in a database. So in this lecture, let's take a look at how we can clean up our database and delete any orders that we don't need anymore. Before we can delete an order, we have to think about the address that we want to use. So let's go in Postman and try to understand what are we sending and how can we delete an order. So far have disorders. We're using the orders and point to get all the orders. And we're using the same endpoint with the different requests a week pose to create a new order. But now if you want to delete something, we don't want to delete all the orders. We just want to delete a specific order. For that reason, the address itself cannot stay in this way because it will be hard to tell exactly what we're trying to delete. Because we have these ideas here. It makes our life easier because we can say something like this. We can add a forward slash here and specified this ID. If you had a Get, It is we'll specify that you are trying to get only a specific order, not the entire list of all the orders. We haven't implemented that. But for example, by bio parallel, if you want to delete a specific order, we can switch from get to delete. And this is how we'll be able to specify in a very nice way that follows the rest conventions. And we're gonna specify the ID here so we know that we're deleting here in order if indeed, even if the string looks very weird, we're here inside orders, we using the delete. So the semantic is they're trying to delete something and we providing this parameter here, we don't need to send anybody or anything like that. So very, very easy way on how we can express this endpoint. Since now we understand what we are trying to do in terms of how we want our endpoint to look like. Let's go inside the code editor and try to implement this. Again, we're going to start with something that we have and make our life easier and will reduce the chances of making mistakes. Let's see here that we are trying to delete an order, the order. And instead of get where I'm going to use delete, because we want to use the HTTP for Arab delete, in this case, still going into orders, but we still need to sort of tell our application, hey, there it is with orders, but after the orders there's still something coming up. And the way we do that is pretty similar to the way we do it in Bozeman also it's soluble convention. We use column and here we can specify a variable, call it order ID. It doesn't really matter how it call, it's just a convention. So what's happening here is that the application will understand, okay, we will get here something that changes all the time is not fixed like orders or pizzas or anything like that. It's a variable, practical. So it will allow us to read it in order to access this data. Will you have guessed it? It's still coming from the request. So it is request dot params cause it's sort of a considered like a perimeter. And we are using exact same knew that we've specified here. Let's go ahead and save it in a variable called Order ID. So we have order ID initialized with this. Don't worry about the arrow dots showing up here is just because we are using this data variable that no longer exists. So we can see here or deleted. And we can even and here the order ID that we have read just to make sure that it's the same order ID. So this is the syntax. We're sending something as a string and I'm also adding this additional information that we have read from the request and sending back to the client. So if I'm sending this request here, we'll see something like order deleted and this will be the ID. And you should notice that the ideas absolutely the same. So this is just a test that our endpoint is working. As you notice in the entire development process, we're taking this iterative process. We don't want to get into writing a lot of code, not understanding what's going on. We take it step-by-step. And if something is not working, go back to suddenly that work before and try to understand why isn't it working anymore? In this case, we tested that the endpoint is working. We're providing this ID here. Doesn't really matter what we provide because there's no functionality behind it. But as you can see, we're sending this back. So at this point we know that we have this ID. We just have to talk now with the database and tell the database, hey, we're going to work again with the orders until eating anything else. I'm going to say here, the close here. And we're gonna say here, Hey, we want to remove this time something. And we're going to specify a bit like based on which criteria we want to identify this data. This is why we have IDs. So as you can easily identify, we don't want to delete orders from database based on the customer name. Maybe they are two mikes that show up at the same time. That will not be a good idea to use that. So this is why we're using this unique IDs. The lamb specify here ID, and this is again the order ID that we're giving. Whereas you can notice the parameter that's being sent to dysfunction is an object, and that object has a property ID. We are setting that property with our Order ID. We also want to persist this one to make this change to the database omega naught use, right? Just as we have done when we have pushed an order, we also have used, right? So this just tells the database saved this version that we are sending here. Let's take a look at the following. So again, I'm going to call all the orders in a different tab so that we can take a look at those. You see we still have two orders in the database. I'm going to use the first idea or the second ID doesn't really matter. Provided here as a parameter, consent. And it says order deleted. If I look in the database, you will see that the order is no longer there. So the order has indeed been deleted. If it's hard to work like this, I can simply replace this year with a column as well. Call it order ID. And you will see that postmen will automatically add this new block here of path variables. It makes it easier for me to just define a variable here. Let's also delete this other order that still available here. So this is an appointed as getting all the orders. We're gonna delete this one as well or deleted. So if you call it again, you'll see that it's empty. We need to create a few more orders. Simply click on send a few times. We can look inside the orders. And you will notice that we have more customers for our pizzeria. Even if is a very simple example. It still shows how you can define an API that tells how we can work with it. Especially when it comes to delete following this rest conventions using the proper methods. It's definitely something that makes sense. Now, I'm not saying you can use here app.get and simply use a get request for leading this information. But that would be rather unexpected because somebody will think that they are just getting the order and not actually deleting it. This is why it makes a lot of sense to have this romantic. When you're selected here in postmen, when you're selecting get, you're expecting to get data. When you're selecting post, you're, you're sending data, changing something on the server and we're selecting delete, you shouldn't be surprised that you're deleting data on the server. This is why following this convention in your code as well, in your application that you are writing here does make a lot of sense. 13. Validating data: I'm just as excited as you to release this incredible API to everyone, but I don't think we're quite ready yet. Let me show you in a second what I mean. If I'm going, for example, inside postmen and I'm looking at the request that responsible for accepting orders. Let's say I rename this Customer Name and I submitted. It still says or the received. So for example, if one tried to get all the orders, all see here probably at the end of the list that I have an order without a customer name. Well, that's not good because I don't know who ordered this. This is the point where we need to talk about validating data that's coming from the client. In this case, Postman is the client. We're sending this data to the API, not accepting, not having a name when you're placing an order. It's not a good idea in this case. What can we do? We need a way to figure out if we have this information before we save it in the database for we say sure order received. So let's do the following. We know that we have this information here. So let's say, for example, have a variable and that is the customer. We're getting this information here and we putting it inside a variable in case something is not right with this. For example, if customer and we're gonna use this exclamation mark just before it. So this will indicate that if customer is not valid or doesn't contain any data. And we're gonna say or, and this is the sign for this pipe. So we have here two pipes. And we may say that, okay, if customer is defined, we can use here a property called length. We want the length to be greater or equal than two. If it's not greater or equal in 2D, then we practically have to say smaller than two. So if length of the length of the customer name is smaller than two, or if the customer doesn't exist as a variable or its empty or something like that. And we want to do something. So for example, what we wanna do is say something like order or customer name not valid. So this will help us like tried to understand, also give this information further on, look, there's something wrong with the customer name, it's not valid, something's not working. Additionally, we can set a response status and we can set the response status to 400. This will indicate the client that, hey, you have done something, something is wrong, and not even sure if this call will probably work. But let's give it a try and see if it solves our problem. We're going back here, still renamed the customer name and something else. And we are getting back the error, customer name not valid. We go back at a requests. We'll see here that we still have a new request or there's something was still saved. So within it to improve a bit our code, because what happened is we didn't stop the execution of the rest. So let's try here adding a return. And this will make this piece of code. And at this point, so we will not add new entries to the database. Submitting is again, customer name is not valid. That's perfect. Let's take a look here to see if we still have a new request and you order coming in and we will only have the 22. It seems that at this point, at least is no longer accepting orders. Now let's change it back to customer because we don't want to affect other customers. So that seems to be working still. So you can see here, we have a new customer from bread and a new order from bread, sorry. And we also want to make sure that it's still working properly. So if we have something like bow order received and we only have one letter that shouldn't be allowed. Customer name is not valid. And you can also see here that the status code is now 400. From this point of view, we have managed to solve this problem. Looking again at all the orders that we have, we can see here that only valid customer orders are coming in. Now to seam goes with the pizza ID. If we send here PTI D ten, it says order received. We're looking at the orders will have an order for PSI D ten. But if remember, we only have four pizzas in the menu, where is that endpoint? We only have four, so we don't have a pizza with ID ten. There is something there that is not right. We shouldn't allow orders for things like this. Going back to the code, there's still a few things that we need to do, but I think we're kinda like study to understand what's going on and how things are supposed to work. Idea would be to have another block here and we're going to fill it up real, real soon. And this time we're gonna save in a variable. Instead of customer. We're going to have here Pizza ID. And of course this will be from the body, the property Pete ID. So what are we trying to do here is to search in a database to figure out, do we have this? I did the database. Of course, we could check to see if it is greater than one and smaller than five or something in this interval. But when we add more pizzas to our database, then we also have to change the code. So that's neither good ideas. We always check with the database to see if that particular pizza is available. What we can do in this case is to write the following statement here. So when our, if we can go to the database and say hey, database, we're working with pizzas, pizza US. And don't get, don't give us all the pieces we want to filter. We are only interested in a particular pieta ID. So we are giving here our filter criteria, specifying and we're filtering by dy. And we only want you to give us this piece ID. Additionally, there are a lot of money to make sure that we're closing everything properly. So we are here at this point. Also, I'm gonna say here value. This will actually read from the database. And this will give us an array with all the pizzas to have this idea, of course, there are array should only contain one items were not even interested what's inside the array and let's say something like length. We are only interested in the length property of this array that's coming back here. And the length of the array should be exactly one. If it's not one. And we're negating this, this way. Again, we are using the same code again. So if we are querying the database of researching a database for this pizza ID and it is not exactly one, then something is not right. So I'm gonna say here, Pizza ID not valid. We're gonna return again. This is the block that validating, first of all, a customer. And after that we'll PTI d, just to make sure that we are only getting valid orders. Take a look and see if that's working. It says here peta ID not valid, we haven't entered a valid PID. And if we look in here again at the order's shouldn't show up. There's nothing new there. So we have to enter Valid PID. Let's try it with four order received. In this case now, we really have an API that it's trying to account for unexpected things that may happen. It's giving back the proper arrow code. Don't worry if this code here is just a bit more complicated that you can understand that this point, that's absolutely fine. Just wanted to, I think the idea of validation to an API and this is exactly what you are getting when something is not right, when you're using an API, you're not providing the exact data that API is expecting. Then there is a piece of code somewhere that says, hey, you were supposed to send me this. And if you didn't send me this, again, work with it. This is the same here. If you're not giving me a customer name, sorry, I cannot take you order. The pizza ID that you provided is not valid. Again, sorry, I cannot take your order. 14. Conclusion: So I hope this introduction in APIs and how to build them has helped you understand what an API actually is. How does it look on the other side and has allowed you to get some practical experience. And I highly encourage you that you look into expanding the examples that I've shown here by adding other endpoints, but trying to do other things with the data that's available may be spanning the endpoints to accept more data, may be adding additional endpoints, playing with different HTTP request methods. And definitely there are still many things to learn.