Hey, guys.
In this module that we're about to begin, we're going to be learning all about APIs, the technology
that powers communication between software on the Internet.
What is an API?
Well, it stands for an Application Programming Interface, and it's essentially a set of rules and
protocols that define how different software can interact with each other.
Now, what does that mean in real life?
I remember reading an article by Marc Andreessen, the founder of A16z, one of the most famous venture
capital firms in Silicon Valley, and I remember him writing an article about, "Why software is eating
the world."
And it's still incredibly relevant in today's day and age.
But one of the things that he started out was the statement that "Software is eating the world."
And he reflects on the fact that books are now bought on Amazon instead of bookstores,
TV has been taken over by Netflix and so on and so forth, and essentially the whole world runs on software.
But one of the things you have to think about with so many different pieces of software existing out
there on the Internet or on your computer or on different places or even in your fridge, you have to
think about the communication between those pieces of software.
Let's say we had a program called Program A, and we had another one called Program B, if we wanted
program A and program B to talk to each other because they provide different pieces of functionality
and we want them to work together, then we would need them to be able to communicate to each other.
But unfortunately, in our case, program A speaks Chinese and Program B speaks Japanese.
So what can we do for them to be able to interact with each other?
Well, we would need an interface in between these two programs.
So that they know how to interact with each other through this bridge in the middle.
And that is essentially what an API does.
It bridges the communication between different pieces of software.
Now let's take some concrete examples.
Maybe you're building a diary website where the user can jot down an entry for each day in their life.
In addition to all the text that they get to write,
you also want to add a little icon at the beginning of each entry that shows the weather for that day.
You can, of course, get the user to look it up and pick a weather icon, but it's much better if this
was automated.
Now, while you're building your website, you look around and you see that there is a company called
OpenWeather that stores and captures weather data and they have that weather data available in their
database. And it's updated frequently and it's available for all the locations in the world,
and you think, wow, perfect,
that's exactly what I need.
I need to be able to talk to these servers to bring their weather data into my website.
So how do you do that?
You don't know what their database is written in.
It could be some sort of programming language you've never heard of.
You don't know how to access their backend servers.
You don't know what the rules are that governs those servers.
You basically have absolutely no idea of how this website or their data storage works, and that is
when the API comes in to save you.
So in this case, OpenWeather creates an API that tells you how you can interact with their services.
If you want data from them,
then there are these and these things that you can access.
For example, if you pass in the latitude and longitude of the location you're interested in, well
they can give you the weather for that location.
They can tell you how that weather is structured.
For example, maybe there is something called, Temperature that gives you the temperature in degrees
Celsius.
Maybe there is an icon for the weather type.
So maybe it would be, cloudy that you would get back.
All you have to do then is to make a request from your website through this API to say, well, these
are the things that I want and because you now have this interface to know how to interact with their
services, you're able to get this data back and populate it into your website.
Now this is almost a simpler problem, because essentially what you're doing is you're making a GET request
to get some data from OpenWeather map and in the response they send back some data. And the
simply tells you how you can make these requests and how the data that's coming back is structured.
Now let's think about a different problem.
Let's say that you want to have a website where you want to let people sign up for your newsletter.
You're writing a newsletter every week and you want all of your fans and all of your friends to sign
up to this newsletter.
Well, so you might have an email collection box, but where does that get stored?
Well, what if you wanted to send your email using Mailchimp and you want to be able to send this email
that you collect each time on your website into a database on your MailChimp account,
how would you do this?
Well, again, through an API.
MailChimp could define an API that tells you how you should structure your POST requests so that you
can send over the data to your email database on your Mailchimp account.
And they in turn can respond with whether if the process was successful or if there were errors.
So in this case, the Mailchimp API defines a structure for the data you want to send, and also it
tells you what kind of responses you can expect in which kind of situations.
Now let's take a look at an even more extreme example.
Let's say you want to build a program on your computer where you can send a tweet over to a plant that
you have at home where every time you tweet @ your plant account, then you've got some sort of Arduino
or IoT devices that's going to water your plant.
So how would this work?
Again, through an API, you could create an API for your hardware,
in this case, the watering device, and you could get it to listen for requests coming through the
internet from your program.
And when you monitor the account of your cacti or other plant, when it sends a request to a particular
path, say /water, and when it sees that request, then it's going to start watering your little plant
and releasing the valve that allows your plant to live
even if you just decided that you no longer want to live in a cold climate and you want to go and move
halfway across the world. At least your plant is taken care of.
And an example of a response from this API could be how much water was released or what is the moisture
content of the soil and so on and so forth.
So I've shown you three different examples of APIs in use in very different situations, and there are
many, many other situations,
but essentially what they have in common is the fact that they are an interface between two pieces of
software.
And by defining a protocol or a set of rules that you can follow, then the different pieces of software
know how to interact with each other and what they should expect when they do interact with the other
service.
You might have heard of some of the different types of APIs such as GraphQL, SOAP, REST, gRPC and
a whole bunch more.
Essentially, what these are is they are architectural styles
just as maybe you would create a house and you decide that you wanted for some reason Ionic columns
in the front of your house.
This is an architectural style, right?
And this is how you decide to design your home,
but somebody else might decide that they want to build their house with a different type of column.
Maybe they want to have Doric columns that have these fancy little squiggles at the top and it looks
super duper fancy and they want to impress all of their neighbors with these beautiful columns.
And I've definitely seen houses that look like this.
They pretty crazy, but this is a different architectural style.
So similarly, we've got different architectural styles for creating an API because essentially an API
is just a set of rules and they each decide different rule sets for creating APIs.
Now which one do we choose to learn or do we learn all of them and spend our entire life trying out
all the different types of APIs?
No.
As always, my belief in terms of learning is just in time learning.
So learn what you need in the moment because that is the best way that you're going to get to grips
with it because you have a strong motivation.
In our case, when it comes to web development, there is one type of API that rules them all and it
is the REST API.
This is the most popular, most commonly used and most important type of API to know if you are going
to work with the web.
What is a RESTful API?
It's not APIs that make you sleepy.
It's APIs that follow a particular set of rules.
But the most important rule of the mall is the fact that you use the HTTP Protocol to interact with
the API.
And lucky for us, we've already learnt about all of these HTTP Protocols:
GET, POST, PUT, PATCH and DELETE.
If you skipped over past lessons, search through the curriculum and take a look back at the different
HTTP Protocols.
So what does this mean for us?
It means that we've already got a leg up on understanding how REST works and how to use it in order
to make requests and access different APIs.
So now let's go ahead and try an exercise where we make a request to an API and get some piece of information
back.
We're not going to be building a website for this API.
We're going to interact with it using Postman.
And let me set the scene here.
You're staring at the night sky,
it's a beautiful cloudless night, and you see thousands upon thousands of stars in the sky and you
can see the Milky Way,
and it's just incredibly beautiful.
Now you see these flashes of light going across the sky and you say to your companion, "Wow, it's a
shooting star."
And your companion tells you, actually, it's probably a satellite.
And today, at any given moment, when you look at the night sky and you see something that moves,
it's most likely to be a satellite.
Now, one of the most famous satellites is the ISS, the International Space Station.
And luckily for us, there is an API called, "Where the ISS at?" that gives you real-time ISS location, so
that the next time when you look up at the night sky and you see some sort of object moving like it's
a shooting star, you can check to see if it's the International Space Station and if there's actually
astronauts right above your head floating around eating space food.
When you look at the documentation for an API such as the one for, "Where the ISS at?" you will see it
almost looks a bit like a menu.
It tells you what is the URL that you need to access in order to interact with the API, and it tells
you what kind of response you can expect to get back. And it gives you a little bit of data on each of
these endpoints and depending on what it is you want from the API, you can figure out by looking at
the database, how you should access it.
When we access an API such as one like this where we're just
getting some data back, such as the latitude and longitude of the ISS,
then we can simply make a GET request to the URL that they specify.
You can imagine our API documentation like a lunch menu where you pick,
"What do I want?"
"Well, I want some bread and I want some calamari."
And then you make the request using HTTP and depending on the type of request maybe you need to make
a POST request or maybe you need to make a GET request.
Once you've looked through the menu of options, you can decide what you need.
In our case, what we're trying to do is we're going to locate the live location of the International
Space Station.
That's the goal.
And once you've located it using the API, you're going to check the latitude and longitude and you're
going to type it in to Google Maps.
The first number that comes is Latitude and the second is longitude, and you separate them with a single
comma, such as how I've written it over here. And you can paste that into the search bar in Google Maps,
and Google Map will drop a pin on the location that is the live location of the International Space
Station.
So that's pretty cool.
And that's the goal of this exercise.
Open up Postman and take a look at the API documentation for, "Where the ISS at?" at this URL location and
see if you can figure out how to make a GET request in order to get the live latitude and longitude
of the ISS.
Pause the video.
Give this a go.
All right, let's run through the solution together.
When you take a look at the documentation, as I mentioned, it looks a bit like a menu.
Now we want to read through the menu to see what we're going to get.
Are we going to get salad or are we going to get bacon?
It's very important because it would be really sad if you ordered bacon and you got salad instead.
If we take a look at the different endpoints that we've got here, one will show us the different satellites
that they track.
This is not so interesting because in the future they want to track lots of different satellites,
but right now, apparently, they only have the International Space Station that's tracked.
This is not the endpoint that we want.
Let's take a look at the next one.
It says that it returns the position velocity and other related information about a particular satellite.
And in order to get hold of that data, you have to give it the ID of the satellite that you want,
and given they only have one satellite, which is the ISS,
they tell you what is the ID of that satellite.
And they have an example where they've shown you how to access that particular API endpoint using this
URL.
And they also show you what is the structure of the data that you can expect to get back.
And if we take a look here, it has the most important pieces of information that we want,
latitude and longitude.
So we're now ready to go ahead and make this request using Postman.
If you remember from before, Postman is a really useful tool for us to be able to make HTTP requests
without needing to have a frontend or a backend.
Instead of having to create a website and then make these requests and install express and all of that,
we can avoid all of it when we want to test a particular request.
We know that we want to get some data, so we're going to use a GET request and we're going to copy
the example resource URL in here to hit up this particular endpoint, which is /satellites
/the ID of the satellite.
So the ID of the ISS is 25544 on this API.
And if I go ahead and hit Send, then it's going to send that request and return me some live data.
Now we've got the important pieces of information that we're after, the latitude and longitude, and
we can go into Google Maps and paste in the data that we got back. Going into Postman,
let's go ahead and copy our latitude.
Paste that in, add a comma and then paste in the longitude and make sure if there are any negative
signs in front of any of those numbers that you copy those as well.
Now that we've got our latitude and longitude structured for Google to be able to know where it is,
let's hit, Enter and let Google find us, "Where the ISS is at".
So it seems right now, at some point in the North Pacific Ocean, somewhere between Hawaii and Mexico.
If you happen to be right around here in a boat somewhere and you look up, you're probably looking
up at the ISS.
Now, if when you type in your latitude and longitude, it doesn't work.
You can check out this Google support article where they tell you how to structure your coordinates
and the different types of structures that are accepted and what correct and incorrect coordinates look
like.
You can Google for this by searching for discover coordinates or search by latitude and longitude from
Google Maps.
But hopefully, you've managed to figure out where is the live location of the ISS at the current time
point for you.
Hopefully, you had fun learning a brief bit about APIs and giving it a quick go.
In the next lesson, we're going to dive in deeper and learn about more complex types of API requests,
and we're also going to see how we can make these different requests using our web server.
So for all of that and more, I'll see you there.