WEBVTT

0
00:00.150 --> 00:00.360
Now,

1
00:00.360 --> 00:05.360
one of the most important aspects of an API is the API endpoint.

2
00:07.110 --> 00:10.710
And you can imagine that as a location, right?

3
00:10.740 --> 00:15.740
So we said that if we want to get data from a particular external service,

4
00:16.080 --> 00:20.940
then we need to know what location that data is stored. So for example,

5
00:20.970 --> 00:22.710
if you wanted to get money out of a bank,

6
00:22.950 --> 00:27.360
then you need to know where that bank is and what is its address.

7
00:28.200 --> 00:30.090
And that, when it comes to API

8
00:30.090 --> 00:35.090
lingo, is called the API endpoint. And that's usually just a URL.

9
00:35.940 --> 00:38.580
So for example, if you wanted to get crypto data,

10
00:38.610 --> 00:41.700
you might use api.coinbase.com.

11
00:42.270 --> 00:46.740
This is the location where the Coinbase data can be found.

12
00:47.940 --> 00:51.630
Now, in addition to knowing the API endpoint,

13
00:51.720 --> 00:54.900
you also have to make a request over the internet.

14
00:55.530 --> 01:00.530
This API request is kind of similar to going to the bank and trying to get some

15
01:00.870 --> 01:04.530
money out, so trying to withdraw some data from their vault.

16
01:05.400 --> 01:09.270
As you can imagine, the world would be completely chaotic

17
01:09.270 --> 01:12.900
if everybody was allowed to just go into a bank and take out the amount of money

18
01:12.900 --> 01:15.810
they need without any sort of checks and balances.

19
01:16.380 --> 01:20.490
So that's why, in the bank, you have a bank teller

20
01:20.550 --> 01:25.550
so somebody who is there to ask you, "Can I help you? What you want?" and to prevent you

21
01:26.730 --> 01:30.420
from going to the vault by yourself. And to also call the police

22
01:30.420 --> 01:35.190
if you try to do that. Now this bank teller is kind of acting like the API.

23
01:35.520 --> 01:40.520
It's the interface between you and the external system or between you and the

24
01:41.310 --> 01:45.510
bank vault. And you can make a number of requests to this lady.

25
01:45.900 --> 01:48.540
You could, of course, ask her to give you some money,

26
01:48.570 --> 01:51.390
but that obviously is going to involve checking your ID,

27
01:51.630 --> 01:55.860
making sure that you have your account number and she'll have to do a whole

28
01:55.860 --> 01:56.693
bunch of checks.

29
01:57.150 --> 02:01.230
But you can also go into the bank and ask the bank teller for things that

30
02:01.230 --> 02:06.000
doesn't require any form of authentication. For example, you can say,

31
02:06.030 --> 02:07.830
well, what are your opening hours?

32
02:08.370 --> 02:11.370
And this is the equivalent of making a very,

33
02:11.370 --> 02:16.370
very simple get request where you just try to get a piece of data from a website

34
02:17.550 --> 02:19.890
using their API. Now,

35
02:19.890 --> 02:23.970
one of the simplest APIs that I like to introduce students to is the

36
02:23.970 --> 02:27.570
international space station current location API.

37
02:28.350 --> 02:30.660
This is the endpoint,

38
02:31.080 --> 02:35.850
and you can see that it returns the output in the form of a JSON,

39
02:35.880 --> 02:38.400
which we've already played around with quite a bit.

40
02:39.870 --> 02:42.990
If you want to make a request to this API,

41
02:43.320 --> 02:47.880
you can of course use your browser to do it. Just paste the end point,

42
02:47.880 --> 02:51.180
which is a URL into the address bar.

43
02:51.510 --> 02:54.660
And once you hit enter, you can see the result

44
02:54.690 --> 02:57.750
come back in the format of a JSON.

45
02:58.380 --> 03:03.380
And this describes the current ISS position in terms of its longitude and

46
03:04.570 --> 03:07.750
latitude. If I refresh this,

47
03:07.870 --> 03:10.840
then I will make a new request to this end point

48
03:11.230 --> 03:16.210
and I will get a slightly different piece of data back because the ISS is moving

49
03:16.300 --> 03:20.040
actually quite fast through the air. So every time you can see that

50
03:20.130 --> 03:23.000
<v 1>it's moved by a little bit. Now, if</v>

51
03:23.160 --> 03:25.500
<v 0>reading the data like this is a bit difficult,</v>

52
03:25.650 --> 03:30.270
I recommend installing a free Chrome browser plugin quote, JSON Viewer

53
03:30.300 --> 03:31.133
Awesome

54
03:31.410 --> 03:36.090
and what it will do is whenever it sees JSON data being rendered in the

55
03:36.090 --> 03:36.923
browser,

56
03:36.960 --> 03:41.960
it will display it in a nice tree structure and it will look something like

57
03:42.240 --> 03:42.990
this.

58
03:42.990 --> 03:47.370
So you can collapse each of these parts and you can see that this is a

59
03:47.400 --> 03:52.020
dictionary effectively with three items, timestamp, message, ISS position.

60
03:52.380 --> 03:54.570
And then when you tap into the ISS position,

61
03:54.780 --> 03:56.640
you can get the longitude and latitude.

62
03:57.570 --> 04:00.240
Now we've already seen the JSON data format,

63
04:01.020 --> 04:04.020
and we know that it was originally created for JavaScript,

64
04:04.320 --> 04:09.320
but later became almost the standard way of transferring data across the

65
04:09.330 --> 04:12.120
internet. And the reason for it is really simple.

66
04:12.420 --> 04:15.930
If you take something like a Python dictionary, let's say you have a wardrobe,

67
04:15.960 --> 04:19.160
right? It's kind of like you're going to Ikea. You spotted a wardrobe

68
04:19.170 --> 04:23.460
you would like, but you don't want to carry this home in its entirety.

69
04:23.820 --> 04:28.260
It might not even fit in your car. Well, what do you do? Well in Ikea,

70
04:28.320 --> 04:31.980
at least the way they've solved it, is they sell you a flat pack.

71
04:32.250 --> 04:36.630
So all the pieces flat against each other and you go home and you build it

72
04:36.630 --> 04:41.430
yourself. This is the equivalent of a JSON. It's very minimalist,

73
04:41.730 --> 04:44.550
it doesn't have a lot of spaces and a lot of indents.

74
04:44.850 --> 04:48.240
It just has a couple of symbols to denote which are the keys,

75
04:48.270 --> 04:51.840
which are the values, which are the separate key-value pairs.

76
04:52.230 --> 04:55.650
And this can be transported across the internet very,

77
04:55.650 --> 04:59.640
very quickly because of how small and flat it is. Now,

78
04:59.670 --> 05:01.620
once you receive this JSON,

79
05:01.710 --> 05:06.710
then you can reconstitute it back to your original Python dictionary or a

80
05:07.200 --> 05:11.010
JavaScript object depending on what programming language you were working with.

81
05:11.370 --> 05:15.510
And this is the equivalent of taking a screwdriver and putting it together that

82
05:15.510 --> 05:16.620
Ikea furniture.

83
05:17.130 --> 05:22.130
But luckily for you, this process is a lot less painful than putting together

84
05:22.470 --> 05:25.440
Ikea furniture. It's actually super easy.

85
05:26.010 --> 05:27.750
Let me demonstrate. Here

86
05:27.780 --> 05:32.780
I've created a brand new project from scratch day-33 and inside my main.py

87
05:34.740 --> 05:39.740
I'm going to try and make a request to the ISS location API.

88
05:41.430 --> 05:45.840
So the first thing I need to do is I need to import a library to help me to do

89
05:45.840 --> 05:46.590
that.

90
05:46.590 --> 05:51.590
And the one that I'm going to import is called requests and note that there's an

91
05:51.720 --> 05:52.680
's' at the end.

92
05:53.310 --> 05:58.310
And this has to be installed because it does not come prebuilt with Python.

93
05:58.880 --> 06:01.940
So you can click on it, and then once you see the red light bulb,

94
06:01.970 --> 06:06.140
click on it and get it to install the package called requests.

95
06:06.710 --> 06:09.800
Once that's done, then you'll be able to use it. Now,

96
06:09.800 --> 06:13.820
what we're going to do using this requests package is we're going to call one of

97
06:13.820 --> 06:15.740
its methods called get.

98
06:16.130 --> 06:20.450
So this is going to help us get the data that we want from the endpoint.

99
06:21.110 --> 06:24.710
The endpoint goes in in an argument called the URL,

100
06:25.280 --> 06:29.390
and we have the check the documentation to know what the endpoint is.

101
06:29.990 --> 06:33.890
So in this case, if we want to get hold of the current location,

102
06:34.190 --> 06:36.770
then this is the endpoint URL.

103
06:37.130 --> 06:41.690
So we can simply copy it and paste it in here as a string.

104
06:43.100 --> 06:46.340
Now, once we've gotten the data from this,

105
06:46.520 --> 06:51.050
then it's going to be returned and we can capture it inside a new variable

106
06:51.050 --> 06:52.850
which we'll call response.

107
06:53.540 --> 06:57.440
So this is going to be equal to the response that we get back from this

108
06:57.440 --> 06:59.120
particular website's API.

109
06:59.900 --> 07:02.630
Now let's go ahead and print this response.

110
07:02.900 --> 07:07.310
So let's run our project and making sure that you are connected to the internet

111
07:07.580 --> 07:11.630
because this line of code is going to go across the internet and fetch this data

112
07:11.630 --> 07:14.450
for us. We get back a response.

113
07:15.830 --> 07:20.630
Now we have successfully made our very first API request from within our Python

114
07:20.630 --> 07:23.600
code. However, when we print the response,

115
07:23.660 --> 07:28.660
we don't see the JSON data like we did when we made the request directly inside

116
07:28.670 --> 07:33.230
our browser. Instead, we see this number 200 printed here.

117
07:33.950 --> 07:37.250
That is our response code. And in the next lesson,

118
07:37.340 --> 07:41.570
we'll dive deeper into how to work with these responses and what exactly these

119
07:41.570 --> 07:42.800
response codes mean.