1 00:00:00,000 --> 00:00:02,480 [Seminar] [A Programmer's Introduction to APIs] 2 00:00:02,480 --> 00:00:04,059 [Billy Janitsch] [Harvard University] [Tommy MacWilliam] 3 00:00:04,059 --> 00:00:08,220 [This is CS50.] [CS50.TV] 4 00:00:08,220 --> 00:00:12,100 >> Hi, everyone, I'm Billy, and today I'm going to be talking about APIs, 5 00:00:12,100 --> 00:00:15,220 or application programming interfaces, 6 00:00:15,220 --> 00:00:20,040 specifically in the context of CS50 final projects and that sort of thing. 7 00:00:20,040 --> 00:00:22,490 In general what is an API? 8 00:00:22,490 --> 00:00:25,530 In very broad terms, it's sort of a middle man that allows 2 pieces 9 00:00:25,530 --> 00:00:28,610 of software to communicate with each other. 10 00:00:28,610 --> 00:00:32,530 That's a kind of very broad definition and isn't that relevant for what we're looking at. 11 00:00:32,530 --> 00:00:35,450 What we really want is some sort of useful middle ground 12 00:00:35,450 --> 00:00:40,570 to communicate with some sort of database somewhere. 13 00:00:40,570 --> 00:00:43,310 >> Here's a chart, and basically the idea is that 14 00:00:43,310 --> 00:00:46,790 we are an application, and we want to get data from a database, 15 00:00:46,790 --> 00:00:49,570 but we don't want to query the database directly. 16 00:00:49,570 --> 00:00:52,710 Instead we want to go through this sort of middle man, the API. 17 00:00:52,710 --> 00:00:55,440 The idea behind that is numbers 2 and 3 on the chart 18 00:00:55,440 --> 00:00:57,750 are both going to be very complicated and messy. 19 00:00:57,750 --> 00:00:59,960 In other words, when the API is querying the database, 20 00:00:59,960 --> 00:01:03,300 it's probably going to be using SQL tables and all of that sort of stuff, 21 00:01:03,300 --> 00:01:05,489 and we've learned a bit about it in CS50, but overall, 22 00:01:05,489 --> 00:01:07,590 you've noticed that it's a bit of a pain. 23 00:01:07,590 --> 00:01:10,590 It gets very, very complicated and messy, especially when you're making 24 00:01:10,590 --> 00:01:12,530 complex queries and that sort of thing. 25 00:01:12,530 --> 00:01:15,960 >> What we really want is some sort of useful and simple way 26 00:01:15,960 --> 00:01:19,780 to get that data, and that's the idea behind numbers 1 and 4 on the chart. 27 00:01:19,780 --> 00:01:23,600 In other words, we want a really simple way to tell the API what to get for us 28 00:01:23,600 --> 00:01:27,760 and a really simple way to get that data back. 29 00:01:27,760 --> 00:01:33,020 There is one main way that that data is usually sent and received, 30 00:01:33,020 --> 00:01:36,490 which is JSON, or JavaScript Object Notation. 31 00:01:36,490 --> 00:01:40,370 That can vary a little bit as far as how you send the request to the API. 32 00:01:40,370 --> 00:01:43,210 In other words, if you want some certain amount of data, 33 00:01:43,210 --> 00:01:46,670 how you tell the API to get that data can vary a little bit. 34 00:01:46,670 --> 00:01:49,210 Usually it involves making some sort of network request. 35 00:01:49,210 --> 00:01:53,130 In other words, accessing some sort of URL that's going to tell the API 36 00:01:53,130 --> 00:01:56,190 exactly what you want, but the data is almost always sent back, 37 00:01:56,190 --> 00:01:59,530 in other words, number 4 in JSON. 38 00:01:59,530 --> 00:02:01,030 >> What is JSON exactly? 39 00:02:01,030 --> 00:02:03,030 As I said, JavaScript Object Notation. 40 00:02:03,030 --> 00:02:07,090 It's basically the universal standard for transmitting and receiving data. 41 00:02:07,090 --> 00:02:11,410 The idea is that you have these 3 categories of things. 42 00:02:11,410 --> 00:02:13,540 You have arrays, hashmaps, and primitives. 43 00:02:13,540 --> 00:02:16,580 Arrays and hashmaps you've looked at a little bit in CS50, 44 00:02:16,580 --> 00:02:19,870 but you've sort of gotten a very strict sense of what they are. 45 00:02:19,870 --> 00:02:22,780 In other words, with arrays you know that they're type bound, 46 00:02:22,780 --> 00:02:26,560 so you only have one sort of type that goes throughout the entire array. 47 00:02:26,560 --> 00:02:29,310 JSON is a lot more lenient with that sort of thing. 48 00:02:29,310 --> 00:02:33,590 Basically the idea is you construct this object, 49 00:02:33,590 --> 00:02:36,270 which can be composed of any of these 3 things 50 00:02:36,270 --> 00:02:39,470 and can be composed of multiple ones of them, and they can be nested. 51 00:02:39,470 --> 00:02:42,110 >> Here's sort of an example of JSON, 52 00:02:42,110 --> 00:02:47,910 which is these curly brackets here represent your hashmap, 53 00:02:47,910 --> 00:02:51,400 and a hashmap is basically a mapping from some sort of key 54 00:02:51,400 --> 00:02:53,340 to some sort of value. 55 00:02:53,340 --> 00:02:56,440 You'll see here that we have the properties key, 56 00:02:56,440 --> 00:02:59,600 and that's mapping onto an array, which is this whole thing. 57 00:02:59,600 --> 00:03:04,120 We see another element of the hashmap, which is this key isAwesome, 58 00:03:04,120 --> 00:03:07,370 which maps to a primitive value of true, in other words, a boolean. 59 00:03:07,370 --> 00:03:09,420 Primitives can be strings. They can be integers. 60 00:03:09,420 --> 00:03:11,960 They can be bools, anything like that. 61 00:03:11,960 --> 00:03:18,410 And you see the contents of this array that properties points to has 2 strings in it, 62 00:03:18,410 --> 00:03:20,050 self-similar and wonderful. 63 00:03:20,050 --> 00:03:27,410 Those are 2 properties of JSON, and we see that JSON is awesome. 64 00:03:27,410 --> 00:03:30,060 To look at that a little more closely I'm going to construct 65 00:03:30,060 --> 00:03:32,870 a more complex example of JSON here. 66 00:03:32,870 --> 00:03:37,000 >> Let's start with an array, for example, just an empty array. 67 00:03:37,000 --> 00:03:39,180 But that's sort of boring, so we're going to fill it up a bit, 68 00:03:39,180 --> 00:03:43,420 and as I said, arrays in JSON are type bound, 69 00:03:43,420 --> 00:03:46,400 so we could also have a string here, which is hi, 70 00:03:46,400 --> 00:03:49,330 and that's another element of that array. 71 00:03:49,330 --> 00:03:53,450 And likewise, we could add a hashmapping here, which is going to have a few mappings. 72 00:03:53,450 --> 00:04:00,470 It's going to have a mapping from name to the string Billy. 73 00:04:00,470 --> 00:04:04,590 We have a mapping from name to Billy, and we have a mapping of 74 00:04:04,590 --> 00:04:10,860 favorite color to blue. 75 00:04:10,860 --> 00:04:12,700 That's basically a good example of JSON. 76 00:04:12,700 --> 00:04:18,160 It kind of gets into—whoops, need a comma there—all of the different parts of it. 77 00:04:18,160 --> 00:04:21,140 Again, it's not type bound at all, so you can have any kind of types 78 00:04:21,140 --> 00:04:24,710 inside anything you want, and the idea is it's self-similar. 79 00:04:24,710 --> 00:04:28,830 In other words, this right here is a JSON object, as is this whole thing, 80 00:04:28,830 --> 00:04:33,200 as is just this, so you can have a primitive be an object, 81 00:04:33,200 --> 00:04:35,680 an array be an object or a hashmap be an object. 82 00:04:35,680 --> 00:04:40,270 >> As you can kind of see, JSON is really, really useful in that it's so versatile. 83 00:04:40,270 --> 00:04:45,860 You can have any possible data that you can conceive stored in JSON. 84 00:04:45,860 --> 00:04:47,900 That makes it a really nice language to use with APIs 85 00:04:47,900 --> 00:04:50,770 because it pretty much means that no matter what data that you want 86 00:04:50,770 --> 00:04:54,270 there's going to be some way to get it back in JSON. 87 00:04:54,270 --> 00:04:58,600 A few properties that make JSON particularly good for this sort of thing. 88 00:04:58,600 --> 00:05:02,270 As you can see, compared to a lot of things that you've been working with in CS50 89 00:05:02,270 --> 00:05:06,040 it's comparatively very easy to read and also very easy to write. 90 00:05:06,040 --> 00:05:09,700 You can indent it out if you want, like I was doing in that example, 91 00:05:09,700 --> 00:05:12,990 which gives you a nice, pretty version that you can see really well. 92 00:05:12,990 --> 00:05:17,150 But moreover, it's also easy to read and write for a computer. 93 00:05:17,150 --> 00:05:19,870 In other words, it's easy to parse and easy to encode, 94 00:05:19,870 --> 00:05:23,820 which means that it's pretty fast as far as reading the data is concerned, 95 00:05:23,820 --> 00:05:26,460 and JSON can be generated really quickly. 96 00:05:26,460 --> 00:05:30,300 >> It's also very easy to access different parts of JSON and that sort of thing. 97 00:05:30,300 --> 00:05:33,320 That's nice, and furthermore, the fact that it's self-similar, 98 00:05:33,320 --> 00:05:36,090 in other words, the fact that you can have JSON within JSON within JSON 99 00:05:36,090 --> 00:05:40,040 is really nice for storing data. 100 00:05:40,040 --> 00:05:45,490 Another part that is generally really useful in working with APIs is jQuery. 101 00:05:45,490 --> 00:05:49,290 You've learned a little bit of JavaScript, which is a nice way 102 00:05:49,290 --> 00:05:53,710 to manipulate HTML and CSS within a website. 103 00:05:53,710 --> 00:05:57,190 But it can kind of be a pain to code in plain JavaScript, 104 00:05:57,190 --> 00:05:59,810 largely because JavaScript is a really verbose language. 105 00:05:59,810 --> 00:06:03,020 You have to learn a lot of syntax, and just to do very simple things 106 00:06:03,020 --> 00:06:07,590 it takes a lot of code, so jQuery is a library for JavaScript. 107 00:06:07,590 --> 00:06:09,800 In other words, it's a JavaScript file that you can load 108 00:06:09,800 --> 00:06:12,730 and then use jQuery functions to do certain things. 109 00:06:12,730 --> 00:06:15,670 And jQuery basically makes your life a whole lot easier. 110 00:06:15,670 --> 00:06:20,390 It simplifies what would take hundreds of lines in JavaScript down to a few lines in jQuery. 111 00:06:20,390 --> 00:06:24,430 >> It's particularly useful if you're using APIs because generally 112 00:06:24,430 --> 00:06:27,600 how you'll be accessing APIs is by making AJAX requests, 113 00:06:27,600 --> 00:06:30,130 and I believe David has mentioned in lecture that AJAX requests 114 00:06:30,130 --> 00:06:33,120 are generally when you're making a network request to some sort of server 115 00:06:33,120 --> 00:06:37,760 and getting back some sort of data and updating a page instantaneously. 116 00:06:37,760 --> 00:06:41,840 Whereas in plain JavaScript that would take crazy numbers of lines 117 00:06:41,840 --> 00:06:44,620 to validate all of the headers and do all of that sort of stuff, 118 00:06:44,620 --> 00:06:46,810 jQuery has a really simple function called AJAX, 119 00:06:46,810 --> 00:06:51,760 and all you have to do in AJAX is give the parameters that you want to give the API, 120 00:06:51,760 --> 00:06:56,830 the location of the API and any additional sort of options that you want to configure. 121 00:06:56,830 --> 00:07:02,480 It's really, really nice and very useful for this kind of thing. 122 00:07:02,480 --> 00:07:06,970 That's all we need to start getting our hands dirty in APIs. 123 00:07:06,970 --> 00:07:10,220 >> I'm going to bring up a few examples and explore their different properties 124 00:07:10,220 --> 00:07:13,150 and why they're useful for different kinds of things. 125 00:07:13,150 --> 00:07:15,570 The first thing I'll actually show you is something that I'm working on 126 00:07:15,570 --> 00:07:18,310 at my research lab, which is an Ngram Viewer, 127 00:07:18,310 --> 00:07:23,270 and basically the idea of an Ngram Viewer is you can search for some kind of word 128 00:07:23,270 --> 00:07:28,840 or phrase and see how often it's appeared in a certain set of text over time. 129 00:07:28,840 --> 00:07:33,160 This example here is this data set of babies 130 00:07:33,160 --> 00:07:36,480 that were born in New York between 1920 and 2000. 131 00:07:36,480 --> 00:07:40,090 We can search, for example, for the name Jennifer, 132 00:07:40,090 --> 00:07:44,400 and we see that pre-1960s it really wasn't used all that much, 133 00:07:44,400 --> 00:07:48,900 and then as we get into later years it's becoming used more and more. 134 00:07:48,900 --> 00:07:53,680 We can also do comparisons, so if we compare Jennifer to, for example, Thomas, 135 00:07:53,680 --> 00:07:56,520 we can see Thomas has been pretty prevalent throughout history, 136 00:07:56,520 --> 00:07:58,780 whereas Jennifer is a more recent name. 137 00:07:58,780 --> 00:08:00,590 We can do that kind of thing. 138 00:08:00,590 --> 00:08:02,460 >> How does this application work? 139 00:08:02,460 --> 00:08:06,030 Basically, it works via an API. 140 00:08:06,030 --> 00:08:08,660 In other words, we have certain parameters here. 141 00:08:08,660 --> 00:08:11,360 We have the parameters of what we're actually searching for, 142 00:08:11,360 --> 00:08:13,720 which are these names, and then we have a few other properties, 143 00:08:13,720 --> 00:08:16,570 like the Y axis and the X axis. 144 00:08:16,570 --> 00:08:18,440 You can see we have a few different options as far as the 145 00:08:18,440 --> 00:08:20,860 time resolution to use and that sort of thing. 146 00:08:20,860 --> 00:08:26,700 We have these options as far as what data we actually want from the database, 147 00:08:26,700 --> 00:08:29,400 and we want to get that data back in some useful way. 148 00:08:29,400 --> 00:08:34,020 Ordinarily, if we were querying the database directly it would sort of be a pain to do 149 00:08:34,020 --> 00:08:38,970 because presumably this data about baby names lives in some database somewhere, 150 00:08:38,970 --> 00:08:42,789 and it would be really complicated to have to query it manually 151 00:08:42,789 --> 00:08:45,830 and decide exactly what data to return. 152 00:08:45,830 --> 00:08:49,300 In other words, we only care about Jennifer and Thomas in this case, 153 00:08:49,300 --> 00:08:53,410 and we only care about on a certain axis and all of that sort of stuff. 154 00:08:53,410 --> 00:08:55,720 >> How do we get around this? 155 00:08:55,720 --> 00:09:01,200 To dig into this API a little more I'll show you another example of this platform 156 00:09:01,200 --> 00:09:04,490 which uses a slightly different data set. 157 00:09:04,490 --> 00:09:09,950 This data set, instead of being baby names, is actually just the entire 158 00:09:09,950 --> 00:09:12,460 print publication database of Open Library, 159 00:09:12,460 --> 00:09:18,410 which is a giant source of texts published throughout the last 100 or so years. 160 00:09:18,410 --> 00:09:23,540 The idea is we have this compository of millions and millions of text, 161 00:09:23,540 --> 00:09:27,420 which we can now search for different words and phrases in. 162 00:09:27,420 --> 00:09:30,840 Here's an example that varies a little differently from the previous example 163 00:09:30,840 --> 00:09:33,350 I showed you, which is we have these 3 search queries, 164 00:09:33,350 --> 00:09:36,290 war, war, and the French word for war, which is guerre. 165 00:09:36,290 --> 00:09:40,380 And we're searching within 3 different sections of the total database. 166 00:09:40,380 --> 00:09:45,080 In other words, in this first query we're only searching in the USA, 167 00:09:45,080 --> 00:09:51,150 in the second one only in the UK, and the third only from works published in France. 168 00:09:51,150 --> 00:09:53,120 We see some interesting patterns emerge. 169 00:09:53,120 --> 00:09:58,180 For example, we see right around here which— 170 00:09:58,180 --> 00:10:02,410 oops, I messed up the axis a little bit, but you can see right in this range here 171 00:10:02,410 --> 00:10:05,730 around the Civil War there's a big spike in the American edition 172 00:10:05,730 --> 00:10:08,340 but not such a big spike in the other two, and that's obviously because 173 00:10:08,340 --> 00:10:10,880 the American Civil War was happening at that point. 174 00:10:10,880 --> 00:10:13,890 >> We can see some cool stuff there, 175 00:10:13,890 --> 00:10:17,070 but what we really care about is how we got this data. 176 00:10:17,070 --> 00:10:21,320 I'll take you behind the scenes in this app in a little bit. 177 00:10:21,320 --> 00:10:24,540 A neat trick is if you're working with the site and kind of want to know 178 00:10:24,540 --> 00:10:27,430 what's going on behind the scenes, you can open up the developer tools. 179 00:10:27,430 --> 00:10:30,200 I'm going to be using Chrome's developer tools, and to get to those 180 00:10:30,200 --> 00:10:35,160 you can do control, shift, J, and that takes you to the JavaScript console. 181 00:10:35,160 --> 00:10:37,420 There are a few tabs here. 182 00:10:37,420 --> 00:10:39,680 They can all be pretty useful under different circumstances, but I care about the network 183 00:10:39,680 --> 00:10:44,150 tab right now, and I actually have to refresh to get that working. 184 00:10:44,150 --> 00:10:50,180 Oh, sorry. 185 00:10:50,180 --> 00:10:52,320 It likes to give a random example. 186 00:10:52,320 --> 00:10:54,700 Okay, we'll use this example instead then. 187 00:10:54,700 --> 00:11:01,330 >> The idea is there's this API here, 188 00:11:01,330 --> 00:11:05,330 and you can see exactly what the API is returning. 189 00:11:05,330 --> 00:11:10,220 This is what the application is getting back from the API having sent that request. 190 00:11:10,220 --> 00:11:13,680 Let me zoom in a little bit, 191 00:11:13,680 --> 00:11:18,340 and we can basically see it's just a series of key value pairs in JSON. 192 00:11:18,340 --> 00:11:23,220 In other words, we have this hashmap here that's mapping values. 193 00:11:23,220 --> 00:11:26,440 In other words, it's mapping years to values. 194 00:11:26,440 --> 00:11:32,600 In 1765 whatever word we initially searched for is used 90 times 195 00:11:32,600 --> 00:11:35,810 out of 1 million, so we're getting back this result. 196 00:11:35,810 --> 00:11:40,280 It's not exactly JSON since we have this little result header here, 197 00:11:40,280 --> 00:11:45,630 but notice that this whole object here is just a great big JSON blob. 198 00:11:45,630 --> 00:11:51,070 We have an array here which contains this whole element, 199 00:11:51,070 --> 00:11:55,590 and you can see that whole element ends there, and then we have another big element 200 00:11:55,590 --> 00:11:59,430 that goes all the way down to the end, and that ends here. 201 00:11:59,430 --> 00:12:02,200 We have a really big array with 2 objects in it, 202 00:12:02,200 --> 00:12:04,630 and each of those objects is a hashmap. 203 00:12:04,630 --> 00:12:07,340 You can see within each of those hashmaps we have a mapping 204 00:12:07,340 --> 00:12:12,700 of this index value to 0 and this value's value to another hashmap, 205 00:12:12,700 --> 00:12:18,360 which again is mapping X axis values to Y axis values. 206 00:12:18,360 --> 00:12:20,970 >> You can see JSON gets a little bit complicated, but overall, 207 00:12:20,970 --> 00:12:24,190 it's actually very useful, and it's very easy to access compared to 208 00:12:24,190 --> 00:12:27,390 other different forms of notation. 209 00:12:27,390 --> 00:12:30,550 As far as what we're actually sending data to the API to get, 210 00:12:30,550 --> 00:12:34,690 I'm going to go into the back end a little bit here. 211 00:12:34,690 --> 00:12:39,850 This is the big JavaScript file that's handling all of the interactions of the web app, 212 00:12:39,850 --> 00:12:44,810 and so we don't care about most of this, but we do care about some of it. 213 00:12:44,810 --> 00:12:47,410 For example, we care about this buildQuery function, 214 00:12:47,410 --> 00:12:50,670 and the idea of this function is basically it's looking around the page, 215 00:12:50,670 --> 00:12:53,750 figuring out what the user wants to query, in other words, 216 00:12:53,750 --> 00:12:57,090 checking those boxes where they've input their search terms, 217 00:12:57,090 --> 00:13:01,380 checking the different Y and X axis values that they've chosen and all of that sort of thing, 218 00:13:01,380 --> 00:13:06,650 and it's going to spit out this query value, which I can then send off to the API. 219 00:13:06,650 --> 00:13:09,180 >> This looks complicated, and it is pretty complicated 220 00:13:09,180 --> 00:13:18,090 but what I'm going to do—in fact, I'm already doing this, which is great— 221 00:13:18,090 --> 00:13:21,640 is that I'm going to get the console to print out exactly that query value 222 00:13:21,640 --> 00:13:28,110 that it's sending off to the API. 223 00:13:28,110 --> 00:13:30,870 That's actually right here. Sorry, it outputs a lot of things. 224 00:13:30,870 --> 00:13:33,690 But this is what we care about, this object right here. 225 00:13:33,690 --> 00:13:35,300 This is the query object. 226 00:13:35,300 --> 00:13:40,670 In other words, this is exactly what the web application is sending to the API, 227 00:13:40,670 --> 00:13:45,730 and so let's look inside a little bit, and we see we have a few values here. 228 00:13:45,730 --> 00:13:48,710 We see we have this count type, which is occurrences per million words, 229 00:13:48,710 --> 00:13:51,460 which is exactly what we've chosen in the Y axis over here. 230 00:13:51,460 --> 00:13:53,740 That's where that's coming from. 231 00:13:53,740 --> 00:13:58,010 We have a database value, which means that there's some certain database 232 00:13:58,010 --> 00:14:01,610 that this data is living in, and we want to access that data specifically 233 00:14:01,610 --> 00:14:04,950 as opposed to the baby names data, for example. 234 00:14:04,950 --> 00:14:08,320 Then we have this groups value, 235 00:14:08,320 --> 00:14:12,090 which is saying that we want to search by year as opposed to 236 00:14:12,090 --> 00:14:16,030 any other X axis value. 237 00:14:16,030 --> 00:14:19,040 Then we have a method, which some APIs will do multiple things. 238 00:14:19,040 --> 00:14:22,360 In other words, this API can also return other kinds of data, 239 00:14:22,360 --> 00:14:27,740 but in this case, we want that mapping of X axis values to Y axis values. 240 00:14:27,740 --> 00:14:30,730 That's what that is telling it to do there, 241 00:14:30,730 --> 00:14:35,020 and we have this search limits array, which contains 2 values. 242 00:14:35,020 --> 00:14:40,720 The first one is what we see here, which is all of the values 243 00:14:40,720 --> 00:14:43,020 contained within that first little box at the top. 244 00:14:43,020 --> 00:14:47,570 >> In other words, we want to look for the word battle, and we want to filter it 245 00:14:47,570 --> 00:14:51,920 by English texts within American literature. 246 00:14:51,920 --> 00:14:54,590 We have this country, which is USA. 247 00:14:54,590 --> 00:14:59,130 We have a language, which is English, so we have all of these different parts 248 00:14:59,130 --> 00:15:02,690 that are all telling the API exactly what we want. 249 00:15:02,690 --> 00:15:04,940 We don't know what the data that we get back is yet, 250 00:15:04,940 --> 00:15:10,970 but we know that the data is going to take a certain form. 251 00:15:10,970 --> 00:15:13,650 This example is sort of on the complicated side, 252 00:15:13,650 --> 00:15:16,180 and you wouldn't necessarily be using an API this complex, 253 00:15:16,180 --> 00:15:20,600 but this is to show you the range and power of what APIs can do. 254 00:15:20,600 --> 00:15:24,980 In other words, using a relatively simple query system we basically have an input box 255 00:15:24,980 --> 00:15:29,490 with a few other selectors in different places. 256 00:15:29,490 --> 00:15:32,010 >> Let me zoom back out here. 257 00:15:32,010 --> 00:15:37,720 We have an input box with a few different metadata selections, 258 00:15:37,720 --> 00:15:40,610 and we have Y axis and X axis selections. 259 00:15:40,610 --> 00:15:42,830 We don't actually have that many fields, 260 00:15:42,830 --> 00:15:46,210 and we can see very easily we're able to query some sort of API 261 00:15:46,210 --> 00:15:48,510 and get data back and then put it into this chart, 262 00:15:48,510 --> 00:15:52,080 which is then going to display it in a useful way. 263 00:15:52,080 --> 00:15:54,970 To look at another example that might be a bit more familiar to you guys 264 00:15:54,970 --> 00:15:56,510 we're going to turn to Facebook. 265 00:15:56,510 --> 00:15:59,440 Facebook's API is called the Facebook Graph, 266 00:15:59,440 --> 00:16:04,390 and basically what that means is Facebook sees itself as this massive database 267 00:16:04,390 --> 00:16:08,000 of lots of different parts that all have certain relationships to each other. 268 00:16:08,000 --> 00:16:11,070 In other words, I'm a user on Facebook, so I have a profile, 269 00:16:11,070 --> 00:16:14,310 and I also have certain friends, and each of them has a profile, 270 00:16:14,310 --> 00:16:17,580 and each of my friends has a wall, which has different comments on it, 271 00:16:17,580 --> 00:16:20,800 and each of those comments has likes and all of that sort of thing. 272 00:16:20,800 --> 00:16:23,100 >> There's lots of different parts to Facebook. 273 00:16:23,100 --> 00:16:26,670 It's a hugely complex API, and there's tons you can do with it, 274 00:16:26,670 --> 00:16:28,450 but it's actually pretty simple to use. 275 00:16:28,450 --> 00:16:33,680 I'm going to start out by going to graph.facebook.com/billyjanitsch, 276 00:16:33,680 --> 00:16:38,430 which is my unique account name, and your account name will either be 277 00:16:38,430 --> 00:16:43,710 some kind of word if you've chosen it, or it might just be a string of numbers. 278 00:16:43,710 --> 00:16:46,360 What we get back is pretty basic information. 279 00:16:46,360 --> 00:16:50,460 We see that I have a first name, which is Billy, a last name, which is Janitsch. 280 00:16:50,460 --> 00:16:53,370 There's a unique Facebook ID which I have. 281 00:16:53,370 --> 00:16:57,920 You can see that I'm male and that I have my language setting 282 00:16:57,920 --> 00:17:01,290 to British English. 283 00:17:01,290 --> 00:17:03,490 In other words, we're seeing very basic information here. 284 00:17:03,490 --> 00:17:08,670 It's not too much, but it does give us an idea of what's there. 285 00:17:08,670 --> 00:17:10,849 >> We can do the same thing to David Malan, for example. 286 00:17:10,849 --> 00:17:13,599 I think his name is dmalan. 287 00:17:13,599 --> 00:17:16,369 We see David Malan has a unique ID. 288 00:17:16,369 --> 00:17:19,300 He has a name, first name, middle name, last name. 289 00:17:19,300 --> 00:17:24,210 We also see that he's male and has his language set to US English. 290 00:17:24,210 --> 00:17:26,869 In other words, we're seeing pretty basic information here. 291 00:17:26,869 --> 00:17:28,860 Now, what happens if we try to check out something else? 292 00:17:28,860 --> 00:17:33,060 Let's say I'm interested in what David Malan has liked on Facebook. 293 00:17:33,060 --> 00:17:36,860 I can do /likes. Now we've run into a problem. 294 00:17:36,860 --> 00:17:39,280 We've got some sort of error that says an access token 295 00:17:39,280 --> 00:17:41,660 is required to request this resource. 296 00:17:41,660 --> 00:17:44,730 But if you think about it, that actually makes sense because it would be weird 297 00:17:44,730 --> 00:17:47,830 if you could access every single part of Facebook's database 298 00:17:47,830 --> 00:17:50,170 just from some sort of simple API, right? 299 00:17:50,170 --> 00:17:56,040 In other words, presumably your information can't be accessed by anyone who wants it. 300 00:17:56,040 --> 00:17:58,330 >> This error is precisely what that means. 301 00:17:58,330 --> 00:18:03,630 Some APIs require certain permissions in order to access their data. 302 00:18:03,630 --> 00:18:06,940 And even more advanced APIs, like the Facebook one, 303 00:18:06,940 --> 00:18:09,840 will require certain permissions to do certain things. 304 00:18:09,840 --> 00:18:12,650 I can see this basic information about David Malan. 305 00:18:12,650 --> 00:18:15,950 I can see that he's male and that he lives in the US, 306 00:18:15,950 --> 00:18:19,270 but I can't really see anything past that. 307 00:18:19,270 --> 00:18:23,050 To get around this for now, Facebook has this nice tool 308 00:18:23,050 --> 00:18:27,690 which is the graph API explorer, and the idea of that is you can sort of 309 00:18:27,690 --> 00:18:31,880 make up permissions for yourself based on your own account 310 00:18:31,880 --> 00:18:35,680 and then view things that specifically your account can view. 311 00:18:35,680 --> 00:18:45,120 For example, if I do graph.facebook.com/billyjanitsch/likes— 312 00:18:45,120 --> 00:18:53,510 whoops, I guess I have to revalidate my token here. 313 00:18:53,510 --> 00:18:55,950 Okay. 314 00:18:55,950 --> 00:19:01,740 If I do that again, great, now I see that I get this object back 315 00:19:01,740 --> 00:19:06,300 which says that I like pool noodles, which are in the category Games and Toys. 316 00:19:06,300 --> 00:19:08,620 I like walruses, which are in the category Animal. 317 00:19:08,620 --> 00:19:10,180 These are my actual Facebook likes. 318 00:19:10,180 --> 00:19:13,280 They're kind of embarrassing. 319 00:19:13,280 --> 00:19:16,090 >> But we can see this data is all returned in JSON. 320 00:19:16,090 --> 00:19:18,160 It's pretty readable. 321 00:19:18,160 --> 00:19:20,970 In other words, we have this mapping of data to some sort of an array, 322 00:19:20,970 --> 00:19:25,220 and each element of this array is a hashmap which maps 323 00:19:25,220 --> 00:19:28,530 the name of a like and the category of a like. 324 00:19:28,530 --> 00:19:31,240 Each like has a unique ID. 325 00:19:31,240 --> 00:19:34,510 There are all sorts of different things of data that we can get, 326 00:19:34,510 --> 00:19:37,980 and if you're interested in using the Facebook API for a CS50 final project 327 00:19:37,980 --> 00:19:40,720 or for anything like that it's actually quite doable. 328 00:19:40,720 --> 00:19:44,260 Basically how you get around the authentication thing is Facebook 329 00:19:44,260 --> 00:19:48,030 uses a system called OAuth, or Open Authentication, 330 00:19:48,030 --> 00:19:52,870 and I don't want to get into it now because OAuth or the different type 331 00:19:52,870 --> 00:19:56,060 of authentication tends to vary a lot between different APIs, 332 00:19:56,060 --> 00:19:58,320 so I could spend a long time going over each one, 333 00:19:58,320 --> 00:20:01,170 but they're actually pretty self-explanatory. 334 00:20:01,170 --> 00:20:04,050 >> If you Google Facebook API it's very readable. 335 00:20:04,050 --> 00:20:06,670 There's a whole spec. 336 00:20:06,670 --> 00:20:10,210 For example, this is the documentation for the Facebook API, 337 00:20:10,210 --> 00:20:14,170 and you can see I'm on the User page, so I can learn all about the different kinds of things 338 00:20:14,170 --> 00:20:17,170 that are available to get as far as data 339 00:20:17,170 --> 00:20:21,550 and also the different permissions that I need in order to access them. 340 00:20:21,550 --> 00:20:25,470 As we saw, we don't need permissions to access the name or the gender, 341 00:20:25,470 --> 00:20:29,380 but beyond that we do need permissions for most things. 342 00:20:29,380 --> 00:20:33,040 This page, or rather, this website will also tell you how to get 343 00:20:33,040 --> 00:20:35,640 a token to be able to authenticate yourself. 344 00:20:35,640 --> 00:20:39,290 Most authentication systems use some sort of token 345 00:20:39,290 --> 00:20:42,880 where you get this unique value, which is a really long and random string, 346 00:20:42,880 --> 00:20:46,240 and that way they can associate the request that you're making with you. 347 00:20:46,240 --> 00:20:50,560 In other words, they know that you're not doing anything suspicious with their data. 348 00:20:50,560 --> 00:20:53,340 They know exactly what you're getting. 349 00:20:53,340 --> 00:20:56,180 They also know that you have permission to view that information. 350 00:20:56,180 --> 00:20:59,110 >> If you've made a Facebook app and your app has certain users, 351 00:20:59,110 --> 00:21:03,380 and those users have allowed that app to access certain parts of their profile, 352 00:21:03,380 --> 00:21:07,790 then whatever API key or token that that app is using 353 00:21:07,790 --> 00:21:11,090 will be able to access the data for those users. 354 00:21:11,090 --> 00:21:13,780 This might sound complicated, but it's not too bad, 355 00:21:13,780 --> 00:21:16,810 and if you want to use Facebook I would highly recommend that you 356 00:21:16,810 --> 00:21:18,990 consider playing around with their API. 357 00:21:18,990 --> 00:21:21,610 It's very cool, and you can do a lot of different things with it. 358 00:21:21,610 --> 00:21:24,880 If the user grants you these permissions you can even go back to the API 359 00:21:24,880 --> 00:21:28,820 and say I want to actually post to this user's wall, or I want to have them post a photo, 360 00:21:28,820 --> 00:21:32,390 and that's why on your news feed you'll sometimes get those annoying things 361 00:21:32,390 --> 00:21:37,840 saying your friend has watched this video on some sort of weird site or something like that. 362 00:21:37,840 --> 00:21:43,120 That's because that app has been granted access to post on that person's wall. 363 00:21:43,120 --> 00:21:48,350 The idea overall, the Facebook API is pretty complicated but also really useful. 364 00:21:48,350 --> 00:21:53,220 Definitely worth checking out if you're still looking for a final project. 365 00:21:53,220 --> 00:21:57,930 >> Another suite of APIs that I'm going to go over is CS50 APIs. 366 00:21:57,930 --> 00:22:00,070 Let me zoom in here. 367 00:22:00,070 --> 00:22:03,390 CS50 has actually put together a whole series of APIs 368 00:22:03,390 --> 00:22:07,080 that you can use for a final project or just for anything that you're making. 369 00:22:07,080 --> 00:22:12,830 And they're mostly Harvard related, and they vary from the HUDS menu, 370 00:22:12,830 --> 00:22:17,780 for example, to this Harvard Events API, which will let you access a list of 371 00:22:17,780 --> 00:22:21,290 different events that are going on at Harvard and that sort of thing. 372 00:22:21,290 --> 00:22:24,510 And so we can click on any one of these and get a spec for it, 373 00:22:24,510 --> 00:22:28,090 which you'll be able to find for any API, and the idea is 374 00:22:28,090 --> 00:22:33,920 it lets you know, A, specifically what to request from the API and how to request it. 375 00:22:33,920 --> 00:22:37,370 In other words, if I want all events that are happening tomorrow 376 00:22:37,370 --> 00:22:42,550 then I've got to obviously give it that date that I want in a certain format, 377 00:22:42,550 --> 00:22:46,030 and B, it will tell me exactly what it's going to give back to me. 378 00:22:46,030 --> 00:22:48,590 It will say I'm going to return you this JSON object, 379 00:22:48,590 --> 00:22:50,960 or like you can see, there are different formats. 380 00:22:50,960 --> 00:22:54,050 >> You can also return the data as a CSV, for example. 381 00:22:54,050 --> 00:22:57,620 But you know exactly how that data is going to look when you get it back 382 00:22:57,620 --> 00:23:00,610 so you can expect to do certain things with it. 383 00:23:00,610 --> 00:23:07,240 We can scroll down and see, for example, if we want to query the API 384 00:23:07,240 --> 00:23:11,500 to get a calendar, then we can use this particular URL 385 00:23:11,500 --> 00:23:16,480 and give it certain parameters which are going to be the data that we want exactly. 386 00:23:16,480 --> 00:23:19,540 And likewise, if we want the data back in a certain format, 387 00:23:19,540 --> 00:23:23,790 then we can ask it to output the data in a CSV, 388 00:23:23,790 --> 00:23:27,700 and that's just another parameter that we're passing to the API. 389 00:23:27,700 --> 00:23:29,210 Lots of cool things to do there. 390 00:23:29,210 --> 00:23:32,550 I would definitely recommend checking out the CS50 APIs. 391 00:23:32,550 --> 00:23:36,000 >> I'm going to look at this Harvard Food API in particular for a little bit. 392 00:23:36,000 --> 00:23:39,870 One thing I've actually designed is this Harvard Noms website, 393 00:23:39,870 --> 00:23:44,930 which uses the CS50 Food API to retrieve the HUDS menu for the day. 394 00:23:44,930 --> 00:23:50,400 And for extension school people, HUDS is the dining service at Harvard. 395 00:23:50,400 --> 00:23:55,130 What you get is this page which contains all of the meals for the day, so we see lunch. 396 00:23:55,130 --> 00:23:58,130 We have a few different categories. We have the bean and whole grain station. 397 00:23:58,130 --> 00:24:00,340 We have the brown rice station. 398 00:24:00,340 --> 00:24:03,360 We can see for brunch we have these few food items. 399 00:24:03,360 --> 00:24:07,030 If we click on them, then we get the nutrition information. 400 00:24:07,030 --> 00:24:12,240 You see this is the nutrition information for grapefruit, in case you were wondering. 401 00:24:12,240 --> 00:24:14,870 And so again, we're going to peer into the back end here a little bit 402 00:24:14,870 --> 00:24:18,530 and see what exactly this is doing to get this data. 403 00:24:18,530 --> 00:24:21,710 And it turns out to not actually be very complex at all. 404 00:24:21,710 --> 00:24:28,720 This file looks a little messy, but keep in mind that this is handling the entire website, 405 00:24:28,720 --> 00:24:34,130 and if I scroll down we see this change data function. 406 00:24:34,130 --> 00:24:36,630 >> Now, just to be clear, this is written in CoffeeScript, 407 00:24:36,630 --> 00:24:39,570 which is a language that you probably haven't seen before. 408 00:24:39,570 --> 00:24:44,810 But it's pretty readable, so I'll walk through it as though it were pseudocode. 409 00:24:44,810 --> 00:24:49,080 Change date is a function that's going to take in this date value, 410 00:24:49,080 --> 00:24:51,740 and it's also going to take in a first, which we don't care about as much. 411 00:24:51,740 --> 00:24:54,110 But the important thing is that it has this date, 412 00:24:54,110 --> 00:25:00,080 and that date is the day that we want to request all of the food items for. 413 00:25:00,080 --> 00:25:04,030 And then you see we have a little bit of syntax here, 414 00:25:04,030 --> 00:25:09,000 which is basically parsing that date into a readable format. 415 00:25:09,000 --> 00:25:11,920 In other words, the API requires the date in a certain format. 416 00:25:11,920 --> 00:25:17,390 You can't just say November 16th, 2012 AD. 417 00:25:17,390 --> 00:25:20,320 It won't know what to do with that. It wants the date in a specific format. 418 00:25:20,320 --> 00:25:23,230 All we're doing here is giving it exactly that format, 419 00:25:23,230 --> 00:25:26,520 which is a year value and then a hyphen, a month value, 420 00:25:26,520 --> 00:25:29,420 another hyphen and the date value. 421 00:25:29,420 --> 00:25:34,910 And we also say we want the data to be output in JSON. 422 00:25:34,910 --> 00:25:37,560 >> Now we're making this AJAX request, and as I mentioned earlier, 423 00:25:37,560 --> 00:25:41,680 jQuery has this super useful AJAX function which all you need to do is specify 424 00:25:41,680 --> 00:25:45,780 a few parameters down here, and it will give you back exactly what you want. 425 00:25:45,780 --> 00:25:50,490 We're telling it that the URL we want it to go to is this CS50 Food API, 426 00:25:50,490 --> 00:25:52,270 which we got from the spec. 427 00:25:52,270 --> 00:25:56,730 We say that we want the data in JSON and that 428 00:25:56,730 --> 00:25:59,490 we're going to give it this data which we've defined up here. 429 00:25:59,490 --> 00:26:02,670 This is the day we want the food items for. 430 00:26:02,670 --> 00:26:07,790 And then all we have to do is define some sort of success function, 431 00:26:07,790 --> 00:26:11,980 which is basically what happens when the API returns that data. 432 00:26:11,980 --> 00:26:15,490 In other words, we've packaged up all of the parameters that we want, 433 00:26:15,490 --> 00:26:20,530 which in this case is the day that we want it and the fact that we want it in JSON, 434 00:26:20,530 --> 00:26:23,840 and we sent it off to the API, so now the API is saying, okay, 435 00:26:23,840 --> 00:26:26,350 here is your data, I got it back for you. 436 00:26:26,350 --> 00:26:29,930 We have the success function, which means given that the API 437 00:26:29,930 --> 00:26:32,230 successfully returns some data, what do we do with it? 438 00:26:32,230 --> 00:26:35,980 >> And it turns out that all we do is call this update menu function 439 00:26:35,980 --> 00:26:42,680 with whatever the API has returned, so we can search for that 440 00:26:42,680 --> 00:26:47,970 and see that all we're doing is using a bunch of new syntax here 441 00:26:47,970 --> 00:26:52,220 to update the HTML and insert this new data. 442 00:26:52,220 --> 00:26:56,580 What this allows is we have these arrows on either side, and we can click, 443 00:26:56,580 --> 00:27:01,060 and now we're looking at the data for the next day and again for the next day, 444 00:27:01,060 --> 00:27:04,820 and each time it's updating that date value and querying the API, 445 00:27:04,820 --> 00:27:07,510 getting back some data and putting it into the site. 446 00:27:07,510 --> 00:27:10,590 Again, you can see, super, super useful. 447 00:27:10,590 --> 00:27:14,410 This app took me a few hours to hack together, 448 00:27:14,410 --> 00:27:20,140 and I have a bit more experience, obviously, but your CS50 final project 449 00:27:20,140 --> 00:27:22,870 can look something very much like this. 450 00:27:22,870 --> 00:27:29,540 >> APIs are super powerful for the amount of effort that they take. 451 00:27:29,540 --> 00:27:32,800 The last thing I'm going to go over is a few more APIs broadly. 452 00:27:32,800 --> 00:27:35,480 I won't get as far into them as far as what they do specifically, 453 00:27:35,480 --> 00:27:38,740 but I'll give you an idea of what's out there. 454 00:27:38,740 --> 00:27:42,700 2 really useful ones, if you're interested in data analysis or visualization 455 00:27:42,700 --> 00:27:45,960 or anything like that, are Freebase and Wikipedia. 456 00:27:45,960 --> 00:27:49,800 Wikipedia—presumably you all know—is a free online encyclopedia, 457 00:27:49,800 --> 00:27:53,230 and it actually has an API, so if you want to, for example, 458 00:27:53,230 --> 00:27:56,250 get all of the texts and the articles for octopus 459 00:27:56,250 --> 00:27:58,030 you can very easily do that. 460 00:27:58,030 --> 00:28:02,300 Just say hey, Wikipedia API, I'd like the data returned as this, 461 00:28:02,300 --> 00:28:07,010 and I'd like it in this format, and the article I'd like is octopus, 462 00:28:07,010 --> 00:28:09,820 and very quickly it will give you back that information. 463 00:28:09,820 --> 00:28:12,230 That can be really useful if you want to make some sort of site 464 00:28:12,230 --> 00:28:16,200 that's a better viewer for Wikipedia or something like that. 465 00:28:16,200 --> 00:28:21,350 >> Freebase is sort of similar, although it's a little bit harder as far as API. 466 00:28:21,350 --> 00:28:24,390 Freebase is like Wikipedia in that it's an online encyclopedia 467 00:28:24,390 --> 00:28:29,050 which contains lots and lots of different data about all sorts of different topics, 468 00:28:29,050 --> 00:28:33,150 but it's stored in a relational database, which is slightly different from Wikipedia. 469 00:28:33,150 --> 00:28:36,410 Wikipedia has its articles and articles linked to other articles, 470 00:28:36,410 --> 00:28:38,860 but for the most part, if you want the data for octopus, 471 00:28:38,860 --> 00:28:41,990 you go to the octopus article, get that data, and you have a bunch of text 472 00:28:41,990 --> 00:28:43,830 about octopuses, so that's great. 473 00:28:43,830 --> 00:28:46,870 Freebase works in a slightly more complicated manner in that 474 00:28:46,870 --> 00:28:48,930 everything is related to one another. 475 00:28:48,930 --> 00:28:52,620 In other words, if we're searching for octopus 476 00:28:52,620 --> 00:28:54,940 then it has a bunch of categories associated with it. 477 00:28:54,940 --> 00:28:57,920 >> For example, it's an animal, it lives underwater, 478 00:28:57,920 --> 00:28:59,710 it has a certain body temperature. 479 00:28:59,710 --> 00:29:01,210 I don't know. 480 00:29:01,210 --> 00:29:04,230 And all of these categories are links to other places where you can go 481 00:29:04,230 --> 00:29:06,640 to see things with that same category. 482 00:29:06,640 --> 00:29:13,450 In other words, the octopus data set would contain a link to the data set for all animals, 483 00:29:13,450 --> 00:29:16,790 and that would let me move around in the database really quickly. 484 00:29:16,790 --> 00:29:21,740 This can be very useful if you're doing something like comparisons. 485 00:29:21,740 --> 00:29:24,490 In other words, given a certain thing, you want to see 486 00:29:24,490 --> 00:29:27,890 what else it's related to and see what else it's not related to. 487 00:29:27,890 --> 00:29:30,700 That sort of thing. It can be useful in a number of ways. 488 00:29:30,700 --> 00:29:34,250 If you're looking for more of a challenge and to be able to do some more complex things 489 00:29:34,250 --> 00:29:38,740 I would consider taking a look at the Freebase API. 490 00:29:38,740 --> 00:29:44,670 But largely, Wikipedia is a very simple place to go as far as getting information. 491 00:29:44,670 --> 00:29:48,340 Another place that I'll look at is Last.fm, and I'm actually going to go to the site 492 00:29:48,340 --> 00:29:53,800 in case some people aren't familiar, but Last.fm is basically a music 493 00:29:53,800 --> 00:29:57,220 tastes and recommendations website. 494 00:29:57,220 --> 00:29:59,000 You can make an account. 495 00:29:59,000 --> 00:30:04,250 You can start uploading music from your music player 496 00:30:04,250 --> 00:30:08,020 to the website, and basically it will start giving you music recommendations 497 00:30:08,020 --> 00:30:10,030 based on what you listen to. 498 00:30:10,030 --> 00:30:14,270 >> For example, if you go to your profile page—this is mine— 499 00:30:14,270 --> 00:30:18,180 you can see you have a list of recently listened to tracks. 500 00:30:18,180 --> 00:30:22,550 You can see overall favorite artists, all of that sort of thing, 501 00:30:22,550 --> 00:30:25,280 and again, there's a big API behind Last.fm, 502 00:30:25,280 --> 00:30:29,360 and you can use it to do lots and lots of really cool things. 503 00:30:29,360 --> 00:30:38,870 For example, I'll go to a friend's page who has this Last.fm Tools website. 504 00:30:38,870 --> 00:30:42,380 This is actually another platform that's built on the Last.fm API, 505 00:30:42,380 --> 00:30:45,420 and it does a number of pretty interesting things. 506 00:30:45,420 --> 00:30:50,260 If I log in with my user name, for example, 507 00:30:50,260 --> 00:30:53,110 I can ask it to generate a tag cloud, for example, 508 00:30:53,110 --> 00:30:56,480 and what that's going to do is give me back an image of 509 00:30:56,480 --> 00:30:59,850 all the different genres and that sort of thing that I like to listen to. 510 00:30:59,850 --> 00:31:01,410 How is it doing this? 511 00:31:01,410 --> 00:31:05,670 Very basically it's saying to the Last.fm API here's this user. 512 00:31:05,670 --> 00:31:10,710 I'd like to know the genre of every song that they've ever listened to, 513 00:31:10,710 --> 00:31:15,130 and you can do that by making a pretty simple AJAX call to the Last.fm API. 514 00:31:15,130 --> 00:31:18,990 You'll get back a big list, and then obviously some other stuff is being done 515 00:31:18,990 --> 00:31:22,280 to turn it into a word cloud, but you can see overall 516 00:31:22,280 --> 00:31:25,850 it's very easy to access and very easy to use. 517 00:31:25,850 --> 00:31:30,750 Really nice for a number of things. 518 00:31:30,750 --> 00:31:35,940 >> I think that's about all I'll say overall. 519 00:31:35,940 --> 00:31:39,040 One last thing I'll mention about APIs in general is that 520 00:31:39,040 --> 00:31:41,840 you'll sometimes run into something called rate limiting, 521 00:31:41,840 --> 00:31:44,940 and the idea of rate limiting is you don't want to abuse APIs. 522 00:31:44,940 --> 00:31:48,130 In other words, it's really nice that a lot of these websites have APIs 523 00:31:48,130 --> 00:31:51,070 that you can go to and use for free. 524 00:31:51,070 --> 00:31:54,460 However, if you're making millions or billions of requests per day, 525 00:31:54,460 --> 00:31:57,610 for example, if you're stuck in an infinite loop that's infinitely querying 526 00:31:57,610 --> 00:32:00,680 some sort of API and getting back a huge amount of data, 527 00:32:00,680 --> 00:32:04,570 obviously that's not good, so what a lot of APIs do is have this rate limiting feature 528 00:32:04,570 --> 00:32:09,970 that says you can only make 1,000 requests per day per IP address or something like that. 529 00:32:09,970 --> 00:32:12,540 And if you're doing a lot of testing and that sort of thing, 530 00:32:12,540 --> 00:32:14,890 you'll sometimes run into that, and suddenly it will shut you off 531 00:32:14,890 --> 00:32:18,280 and say no, I'm not giving you any more data. 532 00:32:18,280 --> 00:32:20,000 >> What you want to do is play by the rules. 533 00:32:20,000 --> 00:32:22,950 You want to make sure that you read the API spec carefully. 534 00:32:22,950 --> 00:32:26,330 If it has certain rules attached to it, like you can only make X queries per day 535 00:32:26,330 --> 00:32:30,000 or you can only access a part of the database a certain number of times 536 00:32:30,000 --> 00:32:32,900 or something like that you want to make sure you stick to that. 537 00:32:32,900 --> 00:32:38,360 As long as you play within those rules you'll probably have a really nice time using APIs. 538 00:32:38,360 --> 00:32:42,030 Your overall takeaway is APIs are really, really useful. 539 00:32:42,030 --> 00:32:45,610 >> There's an API for almost any big web service out there. 540 00:32:45,610 --> 00:32:50,700 Pretty much any part of the Google Tools Suite, Google Maps, Google Earth, 541 00:32:50,700 --> 00:32:54,390 GMail, Google Calendar, all of those things have APIs. 542 00:32:54,390 --> 00:32:58,280 You can use them to both get data from the server and send data to the server. 543 00:32:58,280 --> 00:33:00,870 In other words, if you wanted to make a calendar app that can update 544 00:33:00,870 --> 00:33:04,190 someone's Google Calendar, there's an API for that. 545 00:33:04,190 --> 00:33:07,810 If you want to make something that's going to tell you where 546 00:33:07,810 --> 00:33:12,530 the location of a certain address is you can use the Google Maps API for that. 547 00:33:12,530 --> 00:33:15,860 APIs are fantastically useful, and they're everywhere. 548 00:33:15,860 --> 00:33:18,700 If you're interested in some sort of idea, 549 00:33:18,700 --> 00:33:22,170 there's probably a related API that you can use to get a lot of data 550 00:33:22,170 --> 00:33:25,060 very quickly and very simply. 551 00:33:25,060 --> 00:33:28,140 >> If you're still looking for a project or if you just want to play around 552 00:33:28,140 --> 00:33:31,820 with something in general, APIs are definitely worth doing. 553 00:33:31,820 --> 00:33:37,200 Thanks, and I'm happy to answer any questions that you guys may have. 554 00:33:37,200 --> 00:33:44,900 Okay, thanks a lot. 555 00:33:44,900 --> 00:33:48,000 [CS50.TV]