1
00:00:02,050 --> 00:00:05,920
Now, that is where REST APIs come into play.

2
00:00:05,920 --> 00:00:10,920
This is a specific pattern of building URL-based APIs.

3
00:00:10,920 --> 00:00:13,510
So when you say that you build a REST API,

4
00:00:13,510 --> 00:00:18,020
you build such a URL-based API by following certain rules,

5
00:00:18,020 --> 00:00:21,220
which are not officially standardized,

6
00:00:21,220 --> 00:00:23,303
but common ground, you could say.

7
00:00:24,520 --> 00:00:28,600
REST stands for representational state transfer,

8
00:00:28,600 --> 00:00:30,640
but it's not too important actually,

9
00:00:30,640 --> 00:00:33,120
what's more important is how it actually works

10
00:00:33,120 --> 00:00:35,740
and how you build REST APIs.

11
00:00:35,740 --> 00:00:39,030
And whilst there are more details to it,

12
00:00:39,030 --> 00:00:40,660
which you can dive into,

13
00:00:40,660 --> 00:00:43,910
there is one really important core rule,

14
00:00:43,910 --> 00:00:46,730
which you will see in all REST APIs

15
00:00:46,730 --> 00:00:50,560
or APIs that call themselves REST APIs out there.

16
00:00:50,560 --> 00:00:53,330
Because REST indeed is the most popular

17
00:00:53,330 --> 00:00:55,410
and common form of building APIs,

18
00:00:55,410 --> 00:00:58,180
and you will therefore for see it a lot.

19
00:00:58,180 --> 00:01:01,240
Now, that one rule which I mentioned is in the end

20
00:01:01,240 --> 00:01:03,760
that you have these endpoints,

21
00:01:03,760 --> 00:01:07,460
these URL, HTTP method combinations,

22
00:01:07,460 --> 00:01:11,520
and every such endpoint maps to a certain action.

23
00:01:11,520 --> 00:01:14,810
For example, we could have an endpoint, a route,

24
00:01:14,810 --> 00:01:18,670
in our backend code, where we expect a GET request

25
00:01:18,670 --> 00:01:23,670
to a path named /cars, so our domain /cars.

26
00:01:24,581 --> 00:01:27,100
And for incoming GET requests to this path,

27
00:01:27,100 --> 00:01:30,730
we might then return a list of available cars.

28
00:01:30,730 --> 00:01:34,330
For example, if we're building a car rental service,

29
00:01:34,330 --> 00:01:37,260
then this could be one of our endpoints.

30
00:01:37,260 --> 00:01:39,580
And of course, we could have more endpoints,

31
00:01:39,580 --> 00:01:43,470
and we could have a bunch of HTTP method, URL

32
00:01:43,470 --> 00:01:48,210
or path combinations that then trigger different actions.

33
00:01:48,210 --> 00:01:51,520
Besides the GET /cars, example I already showed you,

34
00:01:51,520 --> 00:01:54,140
we could, for example, except POST requests

35
00:01:54,140 --> 00:01:58,150
to our domain.com/rental,

36
00:01:58,150 --> 00:02:01,640
which could create a new rental request,

37
00:02:01,640 --> 00:02:04,020
if we are building a car rental service,

38
00:02:04,020 --> 00:02:07,020
and then of course, return a response that contains

39
00:02:07,020 --> 00:02:11,810
whether the request was confirmed or declined, for example.

40
00:02:11,810 --> 00:02:15,560
We could accept PUT requests to a rental process

41
00:02:15,560 --> 00:02:17,950
that has a certain ID assigned to it,

42
00:02:17,950 --> 00:02:22,160
which would allow us to update that rental data piece

43
00:02:22,160 --> 00:02:25,820
or a PATCH request to update some user data

44
00:02:25,820 --> 00:02:30,110
or a DELETE request to cancel the rental of a car.

45
00:02:30,110 --> 00:02:32,440
And these are just some dummy examples,

46
00:02:32,440 --> 00:02:35,490
but the idea hopefully is clear.

47
00:02:35,490 --> 00:02:39,470
We build such a Web service as a REST API

48
00:02:39,470 --> 00:02:41,670
by defining a couple of routes

49
00:02:41,670 --> 00:02:45,960
where the HTTP method path combination is very important,

50
00:02:45,960 --> 00:02:50,780
and then our users, our customers, of our service,

51
00:02:50,780 --> 00:02:54,410
can send requests to these specific endpoints

52
00:02:54,410 --> 00:02:59,260
to trigger certain actions or get back certain responses.

53
00:02:59,260 --> 00:03:02,240
That's the idea behind REST APIs in the end,

54
00:03:02,240 --> 00:03:06,510
and that, as mentioned, is the most common form of APIs,

55
00:03:06,510 --> 00:03:10,650
the most common approach of building Web APIs.

56
00:03:10,650 --> 00:03:13,660
Nonetheless, there is something which I have highlighted

57
00:03:13,660 --> 00:03:16,853
before in the course, which I wanna highlight again.

58
00:03:17,870 --> 00:03:20,970
Even though we have all these nice combinations here,

59
00:03:20,970 --> 00:03:24,750
it's still the backend code dead really matters in the end.

60
00:03:24,750 --> 00:03:28,940
It's the backend code that decides what actually happens.

61
00:03:28,940 --> 00:03:31,390
Of course, we have these endpoints

62
00:03:31,390 --> 00:03:34,890
and we set them up following certain conventions.

63
00:03:34,890 --> 00:03:37,110
For example, we would create a endpoint

64
00:03:37,110 --> 00:03:39,070
in our route configuration

65
00:03:39,070 --> 00:03:43,470
that wants the DELETE request method for deleting some data,

66
00:03:43,470 --> 00:03:45,300
but these are just conventions.

67
00:03:45,300 --> 00:03:48,170
What actually happens on our server

68
00:03:48,170 --> 00:03:51,710
is only decided by our backend code.

69
00:03:51,710 --> 00:03:54,270
We could theoretically set up a route

70
00:03:54,270 --> 00:03:57,590
that wants a GET request to a certain path

71
00:03:57,590 --> 00:04:00,640
where we then delete some data on the backend,

72
00:04:00,640 --> 00:04:02,800
this just wouldn't make sense.

73
00:04:02,800 --> 00:04:06,310
So as the developer creating such a Web service,

74
00:04:06,310 --> 00:04:08,950
we typically come up with endpoints,

75
00:04:08,950 --> 00:04:13,540
so with request method, URL or path combinations

76
00:04:13,540 --> 00:04:16,470
that also give the sender of the request

77
00:04:16,470 --> 00:04:19,523
a clear idea of what is likely to happen.

78
00:04:22,150 --> 00:04:23,250
Of course, in addition,

79
00:04:23,250 --> 00:04:25,950
we should also provide clear documentation,

80
00:04:25,950 --> 00:04:29,440
written documentation, if we plan on selling a service

81
00:04:29,440 --> 00:04:32,620
to others, but the endpoints should already give

82
00:04:32,620 --> 00:04:35,963
a first clue of what's probably going to happen.

83
00:04:37,040 --> 00:04:38,660
Now, in this course section,

84
00:04:38,660 --> 00:04:42,150
we're going to learn how to build such a REST API,

85
00:04:42,150 --> 00:04:43,280
because as mentioned,

86
00:04:43,280 --> 00:04:46,460
that is the most common form of an API.

87
00:04:46,460 --> 00:04:50,160
I still also wanna mention that there are alternatives

88
00:04:50,160 --> 00:04:53,300
because the REST is just a pattern for building Web APIs,

89
00:04:53,300 --> 00:04:56,090
and of course, there are also different patterns.

90
00:04:56,090 --> 00:05:00,020
Alternatives would, for example, be the SOAP API,

91
00:05:00,020 --> 00:05:03,800
though, to be very honest, that's not too important anymore,

92
00:05:03,800 --> 00:05:06,170
you don't really need to learn that

93
00:05:06,170 --> 00:05:08,660
unless you really work on a project

94
00:05:08,660 --> 00:05:11,360
where you are forced to use that,

95
00:05:11,360 --> 00:05:14,860
but a very popular alternative to REST APIs

96
00:05:14,860 --> 00:05:17,840
are GraphQL APIs.

97
00:05:17,840 --> 00:05:20,820
This is a pattern developed by Facebook,

98
00:05:20,820 --> 00:05:24,380
and here, for example, you don't have different endpoints

99
00:05:24,380 --> 00:05:28,250
with different request method path combinations.

100
00:05:28,250 --> 00:05:31,360
Instead, there you have just one endpoint,

101
00:05:31,360 --> 00:05:34,800
which is a POST request to some path of your choice,

102
00:05:34,800 --> 00:05:37,670
and then it's the data that's part of the request

103
00:05:37,670 --> 00:05:41,310
that describes what should happen on the server.

104
00:05:41,310 --> 00:05:43,200
It's a totally different topic.

105
00:05:43,200 --> 00:05:47,230
I do dive into GraphQL APIs in my NodeJS course,

106
00:05:47,230 --> 00:05:49,510
if you're interested in that, but still,

107
00:05:49,510 --> 00:05:53,990
as I mentioned before, REST APIs or APIs that are similar

108
00:05:53,990 --> 00:05:58,450
to the concepts implied by the REST API approach

109
00:05:58,450 --> 00:06:01,760
are the most common form of APIs, and therefore,

110
00:06:01,760 --> 00:06:04,310
that's what we'll dive into in this course section.

111
00:06:05,250 --> 00:06:08,000
And with that enough of the theory,

112
00:06:08,000 --> 00:06:10,300
we're now going to get our hands dirty

113
00:06:10,300 --> 00:06:13,160
and build our first API together now,

114
00:06:13,160 --> 00:06:16,660
and for then, we're going to start with a simple example

115
00:06:16,660 --> 00:06:19,460
and we're going to build our own Web service

116
00:06:19,460 --> 00:06:24,460
with a web-based API that allows users of that service,

117
00:06:24,560 --> 00:06:28,690
no matter if that's ourselves on our decoupled frontend

118
00:06:28,690 --> 00:06:31,860
or other developers with their own sites,

119
00:06:31,860 --> 00:06:36,780
to send requests to that API to get a daily quote.

120
00:06:36,780 --> 00:06:38,940
So when our request reaches this route,

121
00:06:38,940 --> 00:06:42,413
we wanna retrieve and return a random daily quote.

122
00:06:43,440 --> 00:06:45,870
Now, we're not going to add endpoints

123
00:06:45,870 --> 00:06:49,960
for storing daily quotes because that's not a feature

124
00:06:49,960 --> 00:06:52,550
this service will provide to our customers.

125
00:06:52,550 --> 00:06:54,360
Instead, the that is something which we,

126
00:06:54,360 --> 00:06:56,770
as the service administrator,

127
00:06:56,770 --> 00:06:59,510
could do through some other API,

128
00:06:59,510 --> 00:07:01,660
which is not exposed to the public,

129
00:07:01,660 --> 00:07:05,210
or as we will do it here, by directly manipulating

130
00:07:05,210 --> 00:07:08,400
the database with database queries.

131
00:07:08,400 --> 00:07:10,150
This is what we're going to build,

132
00:07:10,150 --> 00:07:12,130
and we're going to build it from the ground up

133
00:07:12,130 --> 00:07:15,670
with Node, Express and MongoDB, though, of course,

134
00:07:15,670 --> 00:07:18,100
I wanna emphasize that you could use

135
00:07:18,100 --> 00:07:22,090
any programming language, any framework for that language,

136
00:07:22,090 --> 00:07:24,940
and of course, also any database.

137
00:07:24,940 --> 00:07:28,610
You could also build Web APIs with MySQL.

138
00:07:28,610 --> 00:07:30,773
That would work just fine as well.

