Now,
one of the most important aspects of an API is the API endpoint.
And you can imagine that as a location, right?
So we said that if we want to get data from a particular external service,
then we need to know what location that data is stored. So for example,
if you wanted to get money out of a bank,
then you need to know where that bank is and what is its address.
And that, when it comes to API
lingo, is called the API endpoint. And that's usually just a URL.
So for example, if you wanted to get crypto data,
you might use api.coinbase.com.
This is the location where the Coinbase data can be found.
Now, in addition to knowing the API endpoint,
you also have to make a request over the internet.
This API request is kind of similar to going to the bank and trying to get some
money out, so trying to withdraw some data from their vault.
As you can imagine, the world would be completely chaotic
if everybody was allowed to just go into a bank and take out the amount of money
they need without any sort of checks and balances.
So that's why, in the bank, you have a bank teller
so somebody who is there to ask you, "Can I help you? What you want?" and to prevent you
from going to the vault by yourself. And to also call the police
if you try to do that. Now this bank teller is kind of acting like the API.
It's the interface between you and the external system or between you and the
bank vault. And you can make a number of requests to this lady.
You could, of course, ask her to give you some money,
but that obviously is going to involve checking your ID,
making sure that you have your account number and she'll have to do a whole
bunch of checks.
But you can also go into the bank and ask the bank teller for things that
doesn't require any form of authentication. For example, you can say,
well, what are your opening hours?
And this is the equivalent of making a very,
very simple get request where you just try to get a piece of data from a website
using their API. Now,
one of the simplest APIs that I like to introduce students to is the
international space station current location API.
This is the endpoint,
and you can see that it returns the output in the form of a JSON,
which we've already played around with quite a bit.
If you want to make a request to this API,
you can of course use your browser to do it. Just paste the end point,
which is a URL into the address bar.
And once you hit enter, you can see the result
come back in the format of a JSON.
And this describes the current ISS position in terms of its longitude and
latitude. If I refresh this,
then I will make a new request to this end point
and I will get a slightly different piece of data back because the ISS is moving
actually quite fast through the air. So every time you can see that
it's moved by a little bit. Now, if
reading the data like this is a bit difficult,
I recommend installing a free Chrome browser plugin quote, JSON Viewer
Awesome
and what it will do is whenever it sees JSON data being rendered in the
browser,
it will display it in a nice tree structure and it will look something like
this.
So you can collapse each of these parts and you can see that this is a
dictionary effectively with three items, timestamp, message, ISS position.
And then when you tap into the ISS position,
you can get the longitude and latitude.
Now we've already seen the JSON data format,
and we know that it was originally created for JavaScript,
but later became almost the standard way of transferring data across the
internet. And the reason for it is really simple.
If you take something like a Python dictionary, let's say you have a wardrobe,
right? It's kind of like you're going to Ikea. You spotted a wardrobe
you would like, but you don't want to carry this home in its entirety.
It might not even fit in your car. Well, what do you do? Well in Ikea,
at least the way they've solved it, is they sell you a flat pack.
So all the pieces flat against each other and you go home and you build it
yourself. This is the equivalent of a JSON. It's very minimalist,
it doesn't have a lot of spaces and a lot of indents.
It just has a couple of symbols to denote which are the keys,
which are the values, which are the separate key-value pairs.
And this can be transported across the internet very,
very quickly because of how small and flat it is. Now,
once you receive this JSON,
then you can reconstitute it back to your original Python dictionary or a
JavaScript object depending on what programming language you were working with.
And this is the equivalent of taking a screwdriver and putting it together that
Ikea furniture.
But luckily for you, this process is a lot less painful than putting together
Ikea furniture. It's actually super easy.
Let me demonstrate. Here
I've created a brand new project from scratch day-33 and inside my main.py
I'm going to try and make a request to the ISS location API.
So the first thing I need to do is I need to import a library to help me to do
that.
And the one that I'm going to import is called requests and note that there's an
's' at the end.
And this has to be installed because it does not come prebuilt with Python.
So you can click on it, and then once you see the red light bulb,
click on it and get it to install the package called requests.
Once that's done, then you'll be able to use it. Now,
what we're going to do using this requests package is we're going to call one of
its methods called get.
So this is going to help us get the data that we want from the endpoint.
The endpoint goes in in an argument called the URL,
and we have the check the documentation to know what the endpoint is.
So in this case, if we want to get hold of the current location,
then this is the endpoint URL.
So we can simply copy it and paste it in here as a string.
Now, once we've gotten the data from this,
then it's going to be returned and we can capture it inside a new variable
which we'll call response.
So this is going to be equal to the response that we get back from this
particular website's API.
Now let's go ahead and print this response.
So let's run our project and making sure that you are connected to the internet
because this line of code is going to go across the internet and fetch this data
for us. We get back a response.
Now we have successfully made our very first API request from within our Python
code. However, when we print the response,
we don't see the JSON data like we did when we made the request directly inside
our browser. Instead, we see this number 200 printed here.
That is our response code. And in the next lesson,
we'll dive deeper into how to work with these responses and what exactly these
response codes mean.