1
00:00:00,150 --> 00:00:00,360
Now,

2
00:00:00,360 --> 00:00:05,360
one of the most important aspects of an API is the API endpoint.

3
00:00:07,110 --> 00:00:10,710
And you can imagine that as a location, right?

4
00:00:10,740 --> 00:00:15,740
So we said that if we want to get data from a particular external service,

5
00:00:16,079 --> 00:00:20,940
then we need to know what location that data is stored. So for example,

6
00:00:20,970 --> 00:00:22,710
if you wanted to get money out of a bank,

7
00:00:22,950 --> 00:00:27,360
then you need to know where that bank is and what is its address.

8
00:00:28,200 --> 00:00:30,090
And that, when it comes to API

9
00:00:30,090 --> 00:00:35,090
lingo, is called the API endpoint. And that's usually just a URL.

10
00:00:35,940 --> 00:00:38,580
So for example, if you wanted to get crypto data,

11
00:00:38,610 --> 00:00:41,700
you might use api.coinbase.com.

12
00:00:42,270 --> 00:00:46,740
This is the location where the Coinbase data can be found.

13
00:00:47,940 --> 00:00:51,630
Now, in addition to knowing the API endpoint,

14
00:00:51,720 --> 00:00:54,900
you also have to make a request over the internet.

15
00:00:55,530 --> 00:01:00,530
This API request is kind of similar to going to the bank and trying to get some

16
00:01:00,870 --> 00:01:04,530
money out, so trying to withdraw some data from their vault.

17
00:01:05,400 --> 00:01:09,270
As you can imagine, the world would be completely chaotic

18
00:01:09,270 --> 00:01:12,900
if everybody was allowed to just go into a bank and take out the amount of money

19
00:01:12,900 --> 00:01:15,810
they need without any sort of checks and balances.

20
00:01:16,380 --> 00:01:20,490
So that's why, in the bank, you have a bank teller

21
00:01:20,550 --> 00:01:25,550
so somebody who is there to ask you, "Can I help you? What you want?" and to prevent you

22
00:01:26,730 --> 00:01:30,420
from going to the vault by yourself. And to also call the police

23
00:01:30,420 --> 00:01:35,190
if you try to do that. Now this bank teller is kind of acting like the API.

24
00:01:35,520 --> 00:01:40,520
It's the interface between you and the external system or between you and the

25
00:01:41,310 --> 00:01:45,510
bank vault. And you can make a number of requests to this lady.

26
00:01:45,900 --> 00:01:48,540
You could, of course, ask her to give you some money,

27
00:01:48,570 --> 00:01:51,390
but that obviously is going to involve checking your ID,

28
00:01:51,630 --> 00:01:55,860
making sure that you have your account number and she'll have to do a whole

29
00:01:55,860 --> 00:01:56,693
bunch of checks.

30
00:01:57,150 --> 00:02:01,230
But you can also go into the bank and ask the bank teller for things that

31
00:02:01,230 --> 00:02:06,000
doesn't require any form of authentication. For example, you can say,

32
00:02:06,030 --> 00:02:07,830
well, what are your opening hours?

33
00:02:08,370 --> 00:02:11,370
And this is the equivalent of making a very,

34
00:02:11,370 --> 00:02:16,370
very simple get request where you just try to get a piece of data from a website

35
00:02:17,550 --> 00:02:19,890
using their API. Now,

36
00:02:19,890 --> 00:02:23,970
one of the simplest APIs that I like to introduce students to is the

37
00:02:23,970 --> 00:02:27,570
international space station current location API.

38
00:02:28,350 --> 00:02:30,660
This is the endpoint,

39
00:02:31,080 --> 00:02:35,850
and you can see that it returns the output in the form of a JSON,

40
00:02:35,880 --> 00:02:38,400
which we've already played around with quite a bit.

41
00:02:39,870 --> 00:02:42,990
If you want to make a request to this API,

42
00:02:43,320 --> 00:02:47,880
you can of course use your browser to do it. Just paste the end point,

43
00:02:47,880 --> 00:02:51,180
which is a URL into the address bar.

44
00:02:51,510 --> 00:02:54,660
And once you hit enter, you can see the result

45
00:02:54,690 --> 00:02:57,750
come back in the format of a JSON.

46
00:02:58,380 --> 00:03:03,380
And this describes the current ISS position in terms of its longitude and

47
00:03:04,570 --> 00:03:07,750
latitude. If I refresh this,

48
00:03:07,870 --> 00:03:10,840
then I will make a new request to this end point

49
00:03:11,230 --> 00:03:16,210
and I will get a slightly different piece of data back because the ISS is moving

50
00:03:16,300 --> 00:03:20,040
actually quite fast through the air. So every time you can see that

51
00:03:20,130 --> 00:03:23,000
it's moved by a little bit. Now, if

52
00:03:23,160 --> 00:03:25,500
reading the data like this is a bit difficult,

53
00:03:25,650 --> 00:03:30,270
I recommend installing a free Chrome browser plugin quote, JSON Viewer

54
00:03:30,300 --> 00:03:31,133
Awesome

55
00:03:31,410 --> 00:03:36,090
and what it will do is whenever it sees JSON data being rendered in the

56
00:03:36,090 --> 00:03:36,923
browser,

57
00:03:36,960 --> 00:03:41,960
it will display it in a nice tree structure and it will look something like

58
00:03:42,240 --> 00:03:42,990
this.

59
00:03:42,990 --> 00:03:47,370
So you can collapse each of these parts and you can see that this is a

60
00:03:47,400 --> 00:03:52,020
dictionary effectively with three items, timestamp, message, ISS position.

61
00:03:52,380 --> 00:03:54,570
And then when you tap into the ISS position,

62
00:03:54,780 --> 00:03:56,640
you can get the longitude and latitude.

63
00:03:57,570 --> 00:04:00,240
Now we've already seen the JSON data format,

64
00:04:01,020 --> 00:04:04,020
and we know that it was originally created for JavaScript,

65
00:04:04,320 --> 00:04:09,320
but later became almost the standard way of transferring data across the

66
00:04:09,330 --> 00:04:12,120
internet. And the reason for it is really simple.

67
00:04:12,420 --> 00:04:15,930
If you take something like a Python dictionary, let's say you have a wardrobe,

68
00:04:15,960 --> 00:04:19,160
right? It's kind of like you're going to Ikea. You spotted a wardrobe

69
00:04:19,170 --> 00:04:23,460
you would like, but you don't want to carry this home in its entirety.

70
00:04:23,820 --> 00:04:28,260
It might not even fit in your car. Well, what do you do? Well in Ikea,

71
00:04:28,320 --> 00:04:31,980
at least the way they've solved it, is they sell you a flat pack.

72
00:04:32,250 --> 00:04:36,630
So all the pieces flat against each other and you go home and you build it

73
00:04:36,630 --> 00:04:41,430
yourself. This is the equivalent of a JSON. It's very minimalist,

74
00:04:41,730 --> 00:04:44,550
it doesn't have a lot of spaces and a lot of indents.

75
00:04:44,850 --> 00:04:48,240
It just has a couple of symbols to denote which are the keys,

76
00:04:48,270 --> 00:04:51,840
which are the values, which are the separate key-value pairs.

77
00:04:52,230 --> 00:04:55,650
And this can be transported across the internet very,

78
00:04:55,650 --> 00:04:59,640
very quickly because of how small and flat it is. Now,

79
00:04:59,670 --> 00:05:01,620
once you receive this JSON,

80
00:05:01,710 --> 00:05:06,710
then you can reconstitute it back to your original Python dictionary or a

81
00:05:07,200 --> 00:05:11,010
JavaScript object depending on what programming language you were working with.

82
00:05:11,370 --> 00:05:15,510
And this is the equivalent of taking a screwdriver and putting it together that

83
00:05:15,510 --> 00:05:16,620
Ikea furniture.

84
00:05:17,130 --> 00:05:22,130
But luckily for you, this process is a lot less painful than putting together

85
00:05:22,470 --> 00:05:25,440
Ikea furniture. It's actually super easy.

86
00:05:26,010 --> 00:05:27,750
Let me demonstrate. Here

87
00:05:27,780 --> 00:05:32,780
I've created a brand new project from scratch day-33 and inside my main.py

88
00:05:34,740 --> 00:05:39,740
I'm going to try and make a request to the ISS location API.

89
00:05:41,430 --> 00:05:45,840
So the first thing I need to do is I need to import a library to help me to do

90
00:05:45,840 --> 00:05:46,590
that.

91
00:05:46,590 --> 00:05:51,590
And the one that I'm going to import is called requests and note that there's an

92
00:05:51,720 --> 00:05:52,680
's' at the end.

93
00:05:53,310 --> 00:05:58,310
And this has to be installed because it does not come prebuilt with Python.

94
00:05:58,880 --> 00:06:01,940
So you can click on it, and then once you see the red light bulb,

95
00:06:01,970 --> 00:06:06,140
click on it and get it to install the package called requests.

96
00:06:06,710 --> 00:06:09,800
Once that's done, then you'll be able to use it. Now,

97
00:06:09,800 --> 00:06:13,820
what we're going to do using this requests package is we're going to call one of

98
00:06:13,820 --> 00:06:15,740
its methods called get.

99
00:06:16,130 --> 00:06:20,450
So this is going to help us get the data that we want from the endpoint.

100
00:06:21,110 --> 00:06:24,710
The endpoint goes in in an argument called the URL,

101
00:06:25,280 --> 00:06:29,390
and we have the check the documentation to know what the endpoint is.

102
00:06:29,990 --> 00:06:33,890
So in this case, if we want to get hold of the current location,

103
00:06:34,190 --> 00:06:36,770
then this is the endpoint URL.

104
00:06:37,130 --> 00:06:41,690
So we can simply copy it and paste it in here as a string.

105
00:06:43,100 --> 00:06:46,340
Now, once we've gotten the data from this,

106
00:06:46,520 --> 00:06:51,050
then it's going to be returned and we can capture it inside a new variable

107
00:06:51,050 --> 00:06:52,850
which we'll call response.

108
00:06:53,540 --> 00:06:57,440
So this is going to be equal to the response that we get back from this

109
00:06:57,440 --> 00:06:59,120
particular website's API.

110
00:06:59,900 --> 00:07:02,630
Now let's go ahead and print this response.

111
00:07:02,900 --> 00:07:07,310
So let's run our project and making sure that you are connected to the internet

112
00:07:07,580 --> 00:07:11,630
because this line of code is going to go across the internet and fetch this data

113
00:07:11,630 --> 00:07:14,450
for us. We get back a response.

114
00:07:15,830 --> 00:07:20,630
Now we have successfully made our very first API request from within our Python

115
00:07:20,630 --> 00:07:23,600
code. However, when we print the response,

116
00:07:23,660 --> 00:07:28,660
we don't see the JSON data like we did when we made the request directly inside

117
00:07:28,670 --> 00:07:33,230
our browser. Instead, we see this number 200 printed here.

118
00:07:33,950 --> 00:07:37,250
That is our response code. And in the next lesson,

119
00:07:37,340 --> 00:07:41,570
we'll dive deeper into how to work with these responses and what exactly these

120
00:07:41,570 --> 00:07:42,800
response codes mean.

