Hey, guys. 1
In this module that we're about to begin, we're going to be learning all about APIs, the technology 2
that powers communication between software on the Internet. 3
What is an API? 4
Well, it stands for an Application Programming Interface, and it's essentially a set of rules and 5
protocols that define how different software can interact with each other. 6
Now, what does that mean in real life? 7
I remember reading an article by Marc Andreessen, the founder of A16z, one of the most famous venture 8
capital firms in Silicon Valley, and I remember him writing an article about, "Why software is eating 9
the world." 10
And it's still incredibly relevant in today's day and age. 11
But one of the things that he started out was the statement that "software is eating the world." 12
And he reflects on the fact that books are now bought on Amazon instead of bookstores. 13
TV has been taken over by Netflix and so on and so forth, and essentially the whole world runs on software. 14
But one of the things you have to think about with so many different pieces of software existing out 15
there on the Internet or on your computer or on different places or even in your fridge, you have to 16
think about the communication between those pieces of software. 17
Let's say we had a program called Program A, and we had another one called Program B, if we wanted 18
program A and program B to talk to each other because they provide different pieces of functionality 19
and we want them to work together, then we would need them to be able to communicate to each other. 20
But unfortunately, in our case, program A speaks Chinese and Program B speaks Japanese. 21
So what can we do for them to be able to interact with each other? 22
Well, we would need an interface in between these two programs. 23
So that they know how to interact with each other through this bridge in the middle. 24
And that is essentially what an API does. 25
It bridges the communication between different pieces of software. 26
Now let's take some concrete examples. 27
Maybe you're building a diary website where the user can jot down an entry for each day in their life. 28
In addition to all the text that they get to write, 29
you also want to add a little icon at the beginning of each entry that shows the weather for that day. 30
You can, of course, get the user to look it up and pick a weather icon, but it's much better if this 31
was automated. 32
Now, while you're building your website, you look around and you see that there is a company called 33
OpenWeather that stores and captures weather data and they have that weather data available in their 34
database. And it's updated frequently and it's available for all the locations in the world, 35
and you think, wow, perfect, 36
that's exactly what I need. 37
I need to be able to talk to these servers to bring their weather data into my website. 38
So how do you do that? 39
You don't know what their database is written in. 40
It could be some sort of programming language you've never heard of. 41
You don't know how to access their backend servers. 42
You don't know what the rules are that governs those servers. 43
You basically have absolutely no idea of how this website or their data storage works, and that is 44
when the API comes in to save you. 45
So in this case, OpenWeather creates an API that tells you how you can interact with their services. 46
If you want data from them, 47
then there are these and these things that you can access. 48
For example, if you pass in the latitude and longitude of the location you're interested in, well 49
they can give you the weather for that location. 50
They can tell you how that weather is structured. 51
For example, maybe there is something called temperature that gives you the temperature in degrees 52
Celsius. 53
Maybe there is an icon for the weather type. 54
So maybe it would be cloudy that you would get back. 55
All you have to do then is to make a request from your website through this API to say, well, these 56
are the things that I want and because you now have this interface to know how to interact with their 57
services, you're able to get this data back and populate it into your website. 58
Now this is almost a simpler problem because essentially what you're doing is you're making a GET request 59
to get some data from OpenWeather map and in the response they send back some data. And the 60
simply tells you how you can make these requests and how the data that's coming back is structured. 61
Now let's think about a different problem. 62
Let's say that you want to have a website where you want to let people sign up for your newsletter. 63
You're writing a newsletter every week and you want all of your fans and all of your friends to sign 64
up to this newsletter. 65
Well, so you might have an email collection box, but where does that get stored? 66
Well, what if you wanted to send your email using Mailchimp and you want to be able to send this email 67
that you collect each time on your website into a database on your MailChimp account, 68
how would you do this? 69
Well, again, through an API. 70
MailChimp could define an API that tells you how you should structure your POST requests so that you 71
can send over the data to your email database on your Mailchimp account. 72
And they in turn can respond with whether if the process was successful or if there were errors. 73
So in this case, the Mailchimp API defines a structure for the data you want to send, and also it 74
tells you what kind of responses you can expect in which kind of situations. 75
Now let's take a look at an even more extreme example. 76
Let's say you want to build a program on your computer where you can send a tweet over to a plant that 77
you have at home where every time you tweet @ your plant account, then you've got some sort of Arduino 78
or IoT devices that's going to water your plant. 79
So how would this work? 80
Again, through an API, you could create an API for your hardware, 81
in this case, the watering device, and you could get it to listen for requests coming through the 82
internet from your program. 83
And when you monitor the account of your cacti or other plant, when it sends a request to a particular 84
path, say /water, and when it sees that request, then it's going to start watering your little plant 85
and releasing the valve that allows your plant to live 86
even if you just decided that you no longer want to live in a cold climate and you want to go and move 87
halfway across the world. At least your plant is taken care of. 88
And an example of a response from this API could be how much water was released or what is the moisture 89
content of the soil and so on and so forth. 90
So I've shown you three different examples of APIs in use in very different situations, and there are 91
many, many other situations, 92
but essentially what they have in common is the fact that they are an interface between two pieces of 93
software. 94
And by defining a protocol or a set of rules that you can follow, then the different pieces of software 95
know how to interact with each other and what they should expect when they do interact with the other 96
service. 97
You might have heard of some of the different types of APIs such as GraphQL, SOAP, REST, gRPC and 98
a whole bunch more. 99
Essentially, what these are is they are architectural styles 100
just as maybe you would create a house and you decide that you wanted for some reason Ionic columns 101
in the front of your house. 102
This is an architectural style, right? 103
And this is how you decide to design your home, 104
but somebody else might decide that they want to build their house with a different type of column. 105
Maybe they want to have Doric columns that have these fancy little squiggles at the top and it looks 106
super duper fancy and they want to impress all of their neighbors with these beautiful columns. 107
And I've definitely seen houses that look like this. 108
Pretty crazy, but this is a different architectural style. 109
So similarly, we've got different architectural styles for creating an API because essentially an API 110
is just a set of rules and they each decide different rule sets for creating APIs. 111
Now which one do we choose to learn or do we learn all of them and spend our entire life trying out 112
all the different types of APIs? 113
No. 114
As always, my belief in terms of learning is just in time learning. 115
So learn what you need in the moment because that is the best way that you're going to get to grips 116
with it because you have a strong motivation. 117
In our case, when it comes to web development, there is one type of API that rules them all and it 118
is the REST API. 119
This is the most popular, most commonly used and most important type of API to know if you are going 120
to work with the web. 121
What is a RESTful API? 122
It's not APIs that make you sleepy. 123
It's APIs that follow a particular set of rules. 124
But the most important rule of the mall is the fact that you use the HTTP Protocol to interact with 125
the API. 126
And lucky for us, we've already learnt about all of these HTTP Protocols: 127
GET, POST, PUT, PATCH and DELETE. 128
If you skipped over past lessons, search through the curriculum and take a look back at the different 129
HTTP Protocols. 130
So what does this mean for us? 131
It means that we've already got a leg up on understanding how REST works and how to use it in order 132
to make requests and access different APIs. 133
So now let's go ahead and try an exercise where we make a request to an API and get some piece of information 134
back. 135
We're not going to be building a website for this API. 136
We're going to interact with it using Postman. 137
And let me set the scene here. 138
You're staring at the night sky, 139
it's a beautiful cloudless night, and you see thousands upon thousands of stars in the sky and you 140
can see the Milky Way, 141
and it's just incredibly beautiful. 142
Now you see these flashes of light going across the sky and you say to your companion, "Wow, it's a 143
shooting star." 144
And your companion tells you, actually, it's probably a satellite. 145
And today, at any given moment, when you look at the night sky and you see something that moves, 146
it's most likely to be a satellite. 147
Now, one of the most famous satellites is the ISS, the International Space Station. 148
And luckily for us, there is an API called, "Where the ISS at?" that gives you real-time ISS location, so 149
that the next time when you look up at the night sky and you see some sort of object moving like it's 150
a shooting star, you can check to see if it's the International Space Station and if there's actually 151
astronauts right above your head floating around eating space food. 152
When you look at the documentation for an API such as the one for, "Where the ISS at?" you will see it 153
almost looks a bit like a menu. 154
It tells you what is the URL that you need to access in order to interact with the API, and it tells 155
you what kind of response you can expect to get back. And it gives you a little bit of data on each of 156
these endpoints and depending on what it is you want from the API, you can figure out by looking at 157
the database how you should access it. 158
When we access an API such as one like this where we're just 159
getting some data back, such as the latitude and longitude of the ISS, 160
then we can simply make a GET request to the URL that they specify. 161
You can imagine our API documentation like a lunch menu where you pick, 162
"What do I want?" 163
"Well, I want some bread and I want some calamari." 164
And then you make the request using HTTP and depending on the type of request maybe you need to make 165
a POST request or maybe you need to make a GET request. 166
Once you've looked through the menu of options, you can decide what you need. 167
In our case, what we're trying to do is we're going to locate the live location of the International 168
Space Station. 169
That's the goal. 170
And once you've located it using the API, you're going to check the latitude and longitude and you're 171
going to type it in to Google Maps. 172
The first number that comes is Latitude and the second is longitude, and you separate them with a single 173
comma, such as how I've written it over here. And you can paste that into the search bar in Google Maps, 174
and Google Map will drop a pin on the location that is the live location of the International Space 175
Station. 176
So that's pretty cool. 177
And that's the goal of this exercise. 178
Open up Postman and take a look at the API documentation for, Where the ISS at? at this URL location and 179
see if you can figure out how to make a GET request in order to get the live latitude and longitude 180
of the ISS. 181
Pause the video. 182
Give this a go. 183
All right, let's run through the solution together. 184
When you take a look at the documentation, as I mentioned, it looks a bit like a menu. 185
Now we want to read through the menu to see what we're going to get. 186
Are we going to get salad or are we going to get bacon? 187
It's very important because it would be really sad if you ordered bacon and you got salad instead. 188
If we take a look at the different endpoints that we've got here, one will show us the different satellites 189
that they track. 190
This is not so interesting because in the future they want to track lots of different satellites. 191
But right now, apparently they only have the International Space Station that's tracked. 192
This is not the endpoint that we want. 193
Let's take a look at the next one. 194
It says that it returns the position velocity and other related information about a particular satellite. 195
And in order to get hold of that data, you have to give it the ID of the satellite that you want, 196
and given they only have one satellite, which is the ISS, 197
they tell you what is the ID of that satellite. 198
And they have an example where they've shown you how to access that particular API endpoint using this 199
URL. 200
And they also show you what is the structure of the data that you can expect to get back. 201
And if we take a look here, it has the most important pieces of information that we want, 202
latitude and longitude. 203
So we're now ready to go ahead and make this request using Postman. 204
If you remember from before, Postman is a really useful tool for us to be able to make HTTP requests 205
without needing to have a frontend or a backend. 206
Instead of having to create a website and then make these requests and install express and all of that, 207
we can avoid all of it when we want to test a particular request. 208
We know that we want to get some data, so we're going to use a get request and we're going to copy 209
the example resource URL in here to hit up this particular endpoint, which is /satellites 210
/the ID of the satellite. 211
So the ID of the ISS is 25544 on this API. 212
And if I go ahead and hit Send, then it's going to send that request and return me some live data. 213
Now we've got the important pieces of information that we're after, the latitude and longitude, and 214
we can go into Google Maps and paste in the data that we got back. Going into Postman, 215
let's go ahead and copy our latitude. 216
Paste that in, add a comma and then paste in the longitude and make sure if there are any negative 217
signs in front of any of those numbers that you copy those as well. 218
Now that we've got our latitude and longitude structured for Google to be able to know where it is, 219
let's hit, Enter and let Google find us where the ISS is at. 220
So it seems right now, at some point in the North Pacific Ocean, somewhere between Hawaii and Mexico. 221
If you happen to be right around here in a boat somewhere and you look up, you're probably looking 222
up at the ISS. 223
Now, if when you type in your latitude and longitude, it doesn't work. 224
You can check out this Google support article where they tell you how to structure your coordinates 225
and the different types of structures that are accepted and what correct and incorrect coordinates look 226
like. 227
You can Google for this by searching for discover coordinates or search by latitude and longitude from 228
Google Maps. 229
But hopefully, you've managed to figure out where is the live location of the ISS at the current time 230
point for you. 231
Hopefully, you had fun learning a brief bit about APIs and giving it a quick go. 232
In the next lesson, we're going to dive in deeper and learn about more complex types of API requests, 233
and we're also going to see how we can make these different requests using our web server. 234
So for all of that and more, I'll see you there.