Skip to main content
Loading Explore guides…
Understanding REST APIs · Acedra
Home
Explore
Computer Science
Understanding REST APIs
Sign in to save
Chat
If YouTube asks you to sign in, open the video on YouTube.
Watch on YouTube
Reload player
Transcript
34 segments
Copy
Hide video
0:00
hey guys welcome to another web technology informational video in this video we're going to talk about what a rest API is now I do have several videos on my channel uh showing you how to create a rest API a couple in in JavaScript and node and one in PHP um so you may have seen some of those but you you may still have some confusion on how a rest API really works now you may be saying well I can just Google this why do I have to watch the V this video well if you do Google it you'll get a
0:31
thousand different explanations that are so complicated that it's nearly impossible for an entry level developer to really grasp it so I try to explain things on a more basic level and hopefully that's why you guys watch my videos I try to take complicated Concepts and explain them in plain English uh I'm not a real sophisticated guy so I like things spelled out without nonsense um so that's what I try to give my view ERS all right so before we get into rest let's talk about what an API
1:06
is in general so it stands for application program interface and that's a very broad term there's all kinds of apis uh but we're specifically talking about web apis um there's apis in your computer operating system in your smartphone and even in some refrigerators and so on so um this is very generalized but it's essentially a contract provided by one piece of software to another piece of software uh it usually consists of a structured request and then a structured response
1:38
so one piece of software says give me this information formatted in this way and I'll give you this data or this function or whatever that response may be all right so to help you understand we're going to take a look at a couple analogies this first one is actually from another YouTube video which I'll link in the description I'm using it because it's one of the best anal IES I've seen so think of yourself at sitting at a table at a restaurant um and you're one piece of software such as
2:07
a web application on the client side and then the kitchen is the server or the service that processes certain requests well the waiter is like an API he's formatted to take a certain order in a specific way and then bring back a certain meal uh this would be the data or the response that you requested so an API is is basically a messenger or a waiter between running software all right so uh another good another good analogy is this box here this toy so if we think of this as an
2:41
API it's formatted to take certain shapes so the client has to format the request as a circle square or triangle uh anything else won't be accepted you'll get some kind of error message or error response so this is similar to how an API works and you can think of the shape as the API standard whether it's Json or soap or something else um so to make this operate even more like a real API we could have some kind of output once the formatted shape requests go in and
3:13
that would be a response okay so hopefully this gives you a good idea as to what an API is in general all right so now that we talked about what an API is let's talk about rest so it stands for representational State transfer and it's an architecture style for Designing networked applications it works on relying on a stateless client server communication protocol and in almost all cases this is going to be HTTP all right you guys know what HTTP is it's basically the the
3:46
foundation of the communication of the internet of the web uh every time you load a web page in your browser it's making an HTTP request to a server somewhere um it is possible to use other prot calls with rest but HTTP is by far the most used um because in order to use real world rest you need the delivery methods that HTTP offers um rest was made to treat objects on the server side as resources that can be created updated and destroyed or deleted um an example
4:21
of a serers side object would be a blog post in a database or something like that okay um we can have we can create these posts with uh a post request um a delete with a delete request and so on all right and what makes rest so awesome is that uh it operates using just HTTP uh and usually some kind of standard like Json so it can be used by virtually any programming language uh because most of the the good languages can make HTTP requests in some way whether it's PHP
4:55
JavaScript rails Java python all of these languages are perfectly capable of working with restful interfaces all right so hopefully I haven't lost you yet just remember an API is is the messenger and rest lets us use HTTP requests to format those messages um you you may also hear the term restful API and that just refers to um conforming to the rest constraints so rest API and restful API are essentially the same thing so now that we've established what AR rest API is let's look at the
5:31
specific methods and requests that can be made to a server through HTTP okay so a get request is the most common your your browser client makes get requests every day just by going to a specific server URI uh get requests are used to do just that they get data um or retrieve data from a specified resource okay next we have a post request and you probably use these every day as well because every time you fill out a web form you're making a post request in most cases you can also make
6:05
get requests from forms but it's not secure and the data you submit can be seen by anyone form form tags in HTML can take an action and a method attribute so the action would be the page that you're submitting to and then the method would be either post or get okay those are the only two requests that can be made from just a web form on its own so next we have put and put will update a specified resource usually you would have to send a request to an endpoint which is a URI with some kind
6:42
of ID in for that specific resource whether it's a blog post or a product um the resource itself doesn't matter but the server needs to know which which one you want to update okay you can't make a a put request from a form like you can a post you'd have to use something like Ajax you could use a standard JavaScript or you could use jQuery or something like that and then if you're using a framework like angular um you have different modules including an HTTP module that's capable of sending puts
7:15
and deletes okay a delete request is just that it will delete a specified resource on a server and again you have to let the server know what you're deleting so you want to send an ID along with that now there are other types of requests but they're very rarely used a head request is the same as get except it doesn't return a body in the response it'll only return the head info options can be used to see the supported methods of a server and then patch is for partial resources updating partial
7:49
resources but we're not going to get into that okay so let's look at some endpoint examples endpoints are the URI or the URL that our HTT P requests are sent to so here's an example using a sample API at mysite.com API okay you can choose whatever folder you want to to put your uh API endpoints in but many many times you'll use uh an API folder or even an API subdomain okay so this first one here is a is a um endpoint for a get request and this would typically just give you a list of
8:28
users okay it would return a response with all the users and we can make a get request to a specific user by going API users slash and then whatever that user's ID in many cases you may have an endpoint like this where you're saying instead of just users and then the ID it would be users details and then the ID or users show or something like that now in this case we're making a post request to API users and that's going to add a user to um to the server or to the
9:03
database now notice that this get request and this post request have the same endpoint but since they're different methods or different requests that's okay okay you can use the same the same URL but they have to be different requests in this case here we're making a put request to API users one or you may see something like this API users update one and that's going to update that user okay okay you'll have to send data along with that just as you would with a post request and then
9:35
delete you can make a delete request to API users one and that'll delete that user okay or you may see something like this uh users delete one okay now if we use endpoints like the ones we just saw without any kind of authentication of who we are those are public or open apis but sometimes you need to authenticate before using them now this can mean just register registering your app with the provider's website and even sometimes you'll have to pay for it you'll have to
10:06
purchase that data access so there's a few ways that authentication is implemented so usually you'll use ooth which involves getting some kind of access token and sending that along with your requests so if you temp attempt to make a request without that then you'll get some kind of unauthorized error so here's some examples of how it works with the GitHub API which is a really nice API for beginners it's really easy to use um and how it works is you can use it without any kind of
10:39
authentication but only up to I think it's like 100 requests per hour if you go over that and you didn't authorize then you're going to get an error and you're not going to be able to fetch the data so there's a few different ways that this is implemented in these examples we're using curl to make our requests curl is just basically a tool that we can used to transfer data using multiple protocols including HTTP so this first one here is by sending the token inside of the header
11:10
okay inside the HTTP header so you can include a value for authorization and you would set that to your token all right sometimes you can even send the token as a parameter in the URI and another common way is to not send the token itself but a generated client ID and secret as a parameter uh to your request all right so what I want to do now is jump in and take a look at a real world example we're going to look at the GitHub API going to look at some of the
11:43
documentation and I'll show you uh what happens when you make a get request and we'll request some user data all right so this is the documentation for github's API version three uh if you go to developer github.com V3 and this just basically gives you a bunch of information on the API and most public apis or most apis in general have some kind of documentation like this so you know what endpoints you need to use what uh methods are okay to use things like that um so if we take a look down
12:19
here uh let's see if we look at uh HTTP verb so so these are the different types of request that can be made in certain endpoints so head get post patch put and delete um it shows us how you can authenticate and this is the information we saw on the slide shows us that we can authenticate through the header through a parameter and so on uh if we scroll down here some more it'll also show you how to use pagination and you can use that by requesting a certain
12:51
amount of um of whatever it is users or repositories so in this case it's saying 100 per page and then you can also specify the page and then you can use your programming logic to create pagination okay so you only so you have 100 per page and so on um let's see if we go down some more let's see actually that's not what I want to show you let's go back up here and then over here I want to click on users okay so this gives us the how to get a sing single user we make a get
13:30
request to/ users and then whatever that username okay when you see a colon in front of it like that that usually means that this is some kind of placeholder um so this would be whatever username you want and then this shows you the actual response you get okay so a 200 status means everything's okay um and then it gives you this in the body okay it gives you Jason of All That user's information including their username their ID the uh URL to their image to their to their
14:02
Avatar um let's see how many um let's see how many repos they have how many gists their blog their company and then all these URLs as well which you can make other request to and if we go down some more let see update the authenticated user so if you're authenticated as as a specific user you can actually make request to update your information okay so you can edit your profile uh if you want to get all users you can do that by just making a get request to slash users
14:40
so what I want to do is is try this out now you can obviously do this within your your program but there's also tools that you can use to make requests okay Postman is what I suggest use you can it's a chrome uh a Google Chrome Plugin or extension whatever it's called so you can go ahead and download that and then all you have to do is put your endpoint in here and then the type of request uh in some cases you'll have to send data or maybe some header values uh for
15:10
instance if you need to do authorization you can put that in here and you can put the value so these are key value pairs um and if you need to send data you can do that as well usually if you can only send data you can't do it with a get request you can see that's grayed out but if it was a post request you can send the body different formats uh you can send it as a form or you can send raw data for instance raw Json all right but what I'm going to do is make a get request to https
15:41
api. github.com users okay and then this is what it gives us and you can see we have basically it's an array of user objects so this is the username the ID um all the URLs and all that stuff okay now now if we want to get a specific user we could say we can make a get request to I'll just put my username send and then that gives us all that information on that specific user okay now with the GitHub API and a lot of apis you can do it like this without any kind of authentication for a certain
16:20
amount of requests in fact let me see if I can get to it
16:30
it may yeah here we go and you can see API rate limit has exceeded so you get a certain amount of requests per hour or or per every couple hours or whatever it may be whatever they set it to um so what we would have to do now to be able to keep using this in our application is to register it okay so if we go to github.com settings applications new and let's say test app homepage just say test.com application description just say my app and then some kind of callback URL we'll just
17:10
take this and let's say register application and now you can see it gives us a client ID and a client secret so what we have to do now is just include these as a parameter okay so if we go back to our documentation here and go down to uh where is it authentication so we can add we can send these as a parameter you can see right here client ID client secret so let's take that starting with that question mark and go back to
17:48
postman and we'll just grab the client ID and we'll put that right here place these these x's and then the
18:04
secret and we'll put that right here we don't want that quote on the end and now let's make let's do send and now we can get it okay we can keep making them because we're authenticated all right so that's going to be it for this video hopefully this gives you guys a better idea on how uh restful apis work and if you haven't seen my videos where we create a restful API I would suggest doing that all right so that's it thanks for watching and I will see you next time
Here to help you learn
Sign in to keep a private conversation with your course tutor.
Ask me anything about the material…
Sign in to chat