Hello, everyone, and welcome to this new and exciting session in which we are going to look at APIs.
Now.
API stands for Application Programming Interface.
And in this section we're going to look at why we even need APIs and also how they work.
Now, supposing you've just built this model right here, let's call this Model M one, and this model
can take in an input and produce an output.
The question is how do I make my web app like this one or my mobile app or even my desktop app access
this model which have built such that a user of, let's say this mobile app can just by pressing a button,
get access to this.
My model's predictions the way we could go about this is by making use of APIs.
That's application programming interfaces as defined here the G two dot com website and API permits
software development and innovation to be easier by allowing programs.
That is, for example, your web apps to communicate data and functions safely and quickly.
APIs Does application programming interfaces accelerate innovation because more developers can build
products based on existing data and functionality?
Getting back to our example.
This means that thousands of users now on the web can get access to our model and make predictions.
Without this, developers of this web apps or mobile apps necessarily Mastering the art of model creation.
So we can make this concepts clearer.
Let's take this example.
So yeah, we supposing that you get into a restaurant, you give a command to the waiter, would then
take this command and then tells the cooks to make available the food you have in the command.
Then once this food is ready, it sends it back to the waiter.
That's the API.
And then the waiter now passes on this food to you who then eats it and is happy.
Now, in the case of computer software where we have, say, this mobile app which has to communicate
with this API right here, then this communication has to follow certain rules or a protocol known as
the HTTP.
This protocol guides the way information is being transmitted via the web.
Now, HTTP stands for Hypertext Transfer Protocol.
So this hypertext, then transfer protocol.
Now, the type of data exchange could be text, could be images, could be video.
It could be any kind of data, which is understandable by both this client right here and the server.
Also, communication following this HTTP protocol is connection less.
That is, each time this client needs to communicate with the server to know what objects are found
in the particular image.
A connection is created between these two.
Now, once the connection is created and then the client receives the output, that is the particular
location where the objects are found in an image, that connection is closed.
And so we we now have this which is taken off here.
Then once we want to connect again with the API, which is found in the server, we still create or
recreate another connection.
Then also, as we have said, the data exchange here isn't of a particular type.
So really this protocol doesn't force you to pass or receive a certain type of data so far as the data
you're passing or receiving is understood by the particular entity which is sending or receiving.
Then everything works just fine.
Then the last property of HTTP protocol is it's actually stateless.
That is, once you make a request and then you receive a response.
No client information has been stored in this request response cycle.
Hence, once data has been passed to the API and the response received, you should not expect to be
able to retrieve this data which was being passed to the server.
From this point we are going to go in depth into how.
HTTP works.
So you get to this address w w dot postman dot com.
You could sign in or sign up in case you do not have an account with Postman.
Now I have an account, so I'm just going to sign in.
You get to this point here.
You see you have workspaces, my workspace.
And then you have this link here which comes by default.
You have this URL, which is this information you pass in each time you want to gain access to a particular
site.
In this case, it's Postman Echo dot com.
Now your URL actually stands for Uniform Resource Locator.
And so this address right here permits us to get access to certain resources which are located somewhere
in the web.
And this brings us to the HTTP methods.
If you click right here, you'll see that we have different options, like if you have you have the
gate, you have the post, you have the port, you have the patch, and so on and so forth.
These are known as the HTTP method.
And each time you want to get access to a resource which is located somewhere in the web, you have
to specify the exact method.
So like with a gate, for example, we are saying that we want to get a resource for the post, unlike
the get method, where our main interest is to retrieve some data.
Here we submit an entity to the specified resource which often causes a change in status or side effects
at the level of the server.
So this means that we could make use of the get method when we want to retrieve a user's information
and then we can make use of the post when we want to modify some data or add information.
So like for example, if you want to get registered on a platform, then we make use of this post request
as we're going to add a row on the database.
So suppose we have this database right here where we have the user's ID, we have the user's name and
for example, the user's password.
So here we have this small database and then we have ID, let's say ID zero, we have the name friend
and password.
Let's say whatever value we have here now then we have ID one, let's say Sally, and then we have whatever
password here.
Then we could take two.
We have Rita and we have whatever name we want to have here or whatever password want to have here.
So this is it.
Now, with the get request, we could just retrieve that Fred has an ID of zero.
This name is Fred and the password is a given password.
Whereas with the post request, we could add a new user.
So that's why this pick, of course, and the change in state or side effects on the server.
So we could actually add this new user by passing new information via this post request.
So you could add this third and we could add say Mac, let's say Mac and that's it.
So that's it for the post request.
Now with the put request, we could update this data.
So this means that here we could say we're not interested in maybe modifying the name, but if want
to update this password, we could change this password now to some new password.
So now we could update this row right here and the database.
Now we could also delete, so we could just simply take this off with our delete request and that will
be it for that row.
We also have other methods like the head, the Connect Options, Trace and Patch, which you could check
out in this document right here.
But most times we make use of the get post, put and delete.
Then also this responses have some status codes which we have seen already.
So you maybe have seen a 200 or four.
Okay.
So okay, we make a request and it's okay.
You could receive that.
Let's, let's make this request here you are all empty.
Let's get back here.
And you see, you see your status.
Let's highlight this.
You see a status.
Okay.
So this as we have seen here, let's get back.
This has, as we have seen here, HTTP response status codes.
So you could again check in this documentation and have every detail about the different status codes.
And you need to understand how to.
Work with them as they're very important when you're dealing with APIs.
So we have information, our responses, successful responses lie in between 202 99, and that's why
you see here we have this 200.
Status code, which is a success.
And then we have redirections, we have client error, and we have several error.
So if this error is coming from the person sending the API request, then you'll be between 404 and
99.
The errors from the server will be between 505 99.
So when the server is down, sometimes you will see this 500 so you could check your internal server
error.
The server is in current situation it doesn't know how to handle.
So you could check out all this in this documentation.
Now let's get back here and make this error.
So let's let's put in whatever value one year.
Write that again and you should see your c404 not found.
So you get your you c40 for client error.
So this error comes from the client.
We are going to paste this out here and then select the post request, get to the body form later.
This is the information we'll be passing in.
So yeah, we're going to have email teams and learn that I the password, not a password and then job
alerts and then click on send.
Okay, What do we get?
You see, we have this output right here.
We have our email, the username, the job and a token status to 100.
Now you will notice that unlike with the get requests, we have now introduced this body data.
So if we don't pass this body data, you see that we didn't have the right output.
And this is because to log in to just any platform, obviously you need to pass in your credentials,
which in this case the email, the password and the job.
So this tells us that when doing an API call like this one, so when trying to make the client communicate
with the server, we need to specify the HTTP method.
We need to specify the U URL.
Then we also need to specify the body, which in this case is all this information right here.
That is the body information and the header information.
Now this header and body information could be broken up into the request and response.
So this is the request body and here is a response body.
And then here we have the request header and the response header, which you can get by simply clicking
on this right here.
You can also check out a list of HTTP headers in this developer dot mozilla dot org platform right here.
Then at the level of this response headers, take note of this content type, which is JSON.
JSON actually stands for JavaScript object notation, which is a very easy and lightweight format for
storing and transporting data from a client to a server and vice versa.
The JSON format is programming language independent, and so if we have a client with code reading in
JavaScript, it can communicate JSON data to this API via this request, which is reading, for example,
in another programming language like Python.
And then after processing data, the response in JSON format will still be understood by this JavaScript
client.
Now, to better understand the JSON formatting, let's look at this output which was generated after
we made the get request on this Postman Echo API right here.
You would first notice that it starts and ends with this curly braces.
So we start like this and we end this way and then we have information stored in key value pairs.
So we have the key and then we have the value.
And then each key value pair is separated by a comma.
So we have the first key here key, and then we have the colon, and then we have the value, we have
a comma.
And then the next lesson.
Q One value one, and then next we have key to value two and then separated also by a comma from the
rest and so on and so forth.
So here you see we have this is key one here, key one, value one.
We have the comma and then we have key to this headers, key two headers and then value to now value
two years all this year.
So all this data is of value to So all this.
So our value to and then we have a comer just like yours.
We had this comma and then after we have key three and then value three.
Now for a particular value, we could also have some sort of dictionary.
And so in this case, let's take all this off.
In this case where we had this empty dictionary, this was quite simple, but here we have this dictionary
field with its own key value pairs.
So here we have this key and we have this value.
And then this value is a dictionary made of its own key value pairs.
So you have the commerce again and just like that.
So you have the commas separating each and every key value pair and then you're you just have this string.
So basically what we have in here is we have a key, which is a string.
And we have a value, which is some variable.
Now this variable can be a dictionary like this.
The first two examples, or it can be a string, or it could be an integer or even a boolean.
Now, if you check on this other request, here's the push request.
You'll see this same formatting.
So we have the curly braces which open and close, and then we have each key.
And this value, the key is value.
Key value.
And finally, key value.
Now, this was for the response.
So this was the response body.
Let's go ahead and check at the request body, request body.
We had this form data, but actually we could have raw JSON data which is passed.
So instead of using this, like basically using this UI, we could have this raw and then pass in JSON
data.
So it's here is actually very easy to to write this out.
Let's have this here.
Let's check back at the form data.
We have email, password and job.
Yeah, we have the same so we have email.
There we go.
We specify the email, learn the AI, and then we have the password.
Our password is not a password, so not a password.
There we go.
Next and final one.
We have the job.
We have your new learns.
Okay, so we have that.
So notice how here we constructing this raw data, which we're going to pass in and we're going to see
the output we get your.
So let's send this and what do we get here is what we get as output.
You see your unsupported major type text planning requests.
So this means that the request we sent was text type and not the JSON format.
So you'll notice here we have text which was selected and we just simply have to change this to JSON.
So changing the suggestion, what do you notice is here a change in color?
That is because this now knows that here are the keys which are in red and the values in blue takes
it back to C turns back to all black.
Let's get back to JSON and then rerun this.
So we click on Sell On Send and let's see the output using pass error.
We check that here.
Let's take that off and then send again.
Okay.
So you see now that once we correct that and send this JSON data, we have this output.
So basically using this form data right here, which you could now take off, let's take this off is
the same as passing this raw JSON data.
So the role of post mine is to make it easier for people to test their APIs using this kind of graphical
user interface.
But you could always actually do it by passing this raw JSON data, as you can see here.
And so now you have the status.
Okay?
Whereas when we had this text, let's send so you could see the status, you see for one five unsupported
media type, let's get back to JSON and send and that's what we have.
You also notice again that the level of the headers, we have this JSON format, so the body, the output
we have here is JSON data.
Then instead of passing this, let's add in some random stuff here and click on send.
What do we have?
We have all four not found, but we'll notice that the content type that we have now is no longer the
JSON, but instead HTML.
And when you check out the body, you see we have this year preview page not found.
This shows us that we can have different content types exchanged between the client and the server.