Full transcript of “APIs for Beginners - How to use an API (Full Course / Tutorial)”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 This course is an excellent introduction to APIs for beginners. This is an updated version of what is already one of the most popular API courses on the Internet. Craig Dennis is back as the instructor and in my opinion he is one of the best developer educators out there. - Hello, I'm Craig and I'm a developer. In this course, we are going to be exploring a term that you've probably heard quite a bit. That term is API or application programming interface. In addition to being able to recite what it stands for, when you complete this course, you'll be able to discuss what an API does. You'll understand why they exist and you'll be able to list the many benefits that they provide. You'll even get some hands-on experience using a few popular web APIs, which will give you a taste of what all this excitement is about. We'll be starting from the very beginning,
0:46 so there really isn't too much I'm gonna be expecting you to know before starting this course. I would like to request that you have a touch of experience with coding and a wee bit of knowledge about the web. If you've written any program at all, even the typical first program, you know the one where you write the words, "Hello, world." If you've done that in any programming language, you'll pretty much be ready for this course. It will be helpful if you have a basic understanding of how the web works with web servers and browsers, but don't sweat it, we'll touch on that too. Now, if you haven't met these requirements, please check the notes attached to this video for where to get started with some beginning web and programming concepts. That reminds me, you should take some time to get used to your learning environment. The notes that I just discussed are attached to this video
1:31 and I'll also drop more information into that section to keep you updated on what might have changed from my right now to your right now and I'll do my best to remind you to look in there when there's something I think you just have to see, but you really should get in the habit of checking that area out. There'll be tons of juicy information there. You might be getting a real bad case of deja vu right now. This is a refresh of a course from 2019, which these days seems like about 30 years ago or something. If you saw that version, welcome back. Technology is constantly evolving and just updating the notes wasn't cutting it, so this is version two. Thanks for all the feedback, by the way, a lot of yours and your fellow learner's suggestions are implemented in this new updated version. All that to say, we got you. These videos have captions, so if you feel like
2:17 you might benefit from having those on, please use them. This space is all yours. If you want me to speak slower, please use the speed controllers to slow me down, and if I'm taking a long time, speed me up. I make a pretty good chipmunk. And last but certainly not least, take advantage of the fact that you are in a video-based course. If you need me to repeat myself, use the video controls to rewind me. If you feel like you need to take a break to let something sink in, by all means, please pause me. I'll be right here waiting for you. I know that I've gotten into some pretty bad viewing habits with all this streaming media. Now, while binging might be a great way to consume an entire season of your favorite show, it's not the best way to learn something new. Education isn't meant to be binged and because of this,
3:03 I'll probably remind you quite a bit to take breaks and encourage you to let what you just learned marinate. All right, let's get to it. API, those three little letters generate quite the buzz, don't they? First, I'd like to take some time upfront here and clearly break down what is meant by application programming interface. By understanding its purpose and a touch of its history, I think you'll start to see its importance and begin to understand why this term keeps popping up in your life. After we get a good grasp on the what and the why of APIs, we'll go and learn how to explore two existing ones. We'll get familiar with several handy tools that you can use to interact with web-based APIs. We'll use those tools to hit an external web API to send actual text messages and we'll get some
3:49 inteRESTing data back from a streaming music service API. Once we get some practice using the APIs, we'll use them in a web application that you'll actually publish. The application that we're gonna be building is going to receive user submitted photos via a text message, and then we'll use an API to display a collection of all these photos and you get to come up with the prompt. Send me the latest photo on your phone, dog pics, please, what's for dinner, whatever you want. I can't wait to see what you build and what your family and friends submit to it. We are going to be strictly using APIs in this course, we won't be writing our own. While you most likely are gonna end up learning some best practices in API design, we won't be covering that specifically. There are many great courses available that show the creation thought process, and I've linked to some of my faves in the notes attached to this video.
4:36 In my opinion, the best way to start learning about any acronym is to focus on each individual letter. For API, I think we should start on that last character, the I, interface. Let's talk a bit about the interfaces that you've seen already. Interfaces are all around us. For instance, this radio here, it has a familiar interface. The interface allows me to do things like change the volume, change the station, I can turn it off and on. I literally have no idea how anything inside this thing works, but still the interface allows me to control the way that it works through the options that are exposed and that's pretty great, isn't it? The magic that is happening is completely abstracted away from me. I'm still in control of what has been determined that I, a user can handle. Interfaces usually evolve over time,
5:23 like an alarm clock radio for example. It has my familiar volume and station controls and it also adds some new features like changing the time and setting the alarm. I still have no idea how this works implementation wise, but the interface is something that I can understand. Usually, and as technology evolves and new options come into existence, they make their way into the new versions of the interface. Still abstracting away things for us, but enabling us to be in control of the new features. Sometimes these physical interfaces get so recognizable, they even make it into GUIs. Hey, did you hear that? There it is again, GUI, G-U-I, graphical user interface. This is my streaming music app on my phone. There's my familiar Play button in the user interface or UI,
6:11 and I know what the Play button does when I press it, but I have no idea how it does it and I'm okay with that level of abstraction. What would I do in public transit without music? Like, talk to people? Hi, how's it going? Now, the developer who wrote this streaming music application, of course, understands how the Play button works. She coded the button so that when it gets clicked, the music starts playing. Here's the thing though, our developer is also working with quite a few interfaces that are abstracting the way some of the details for her. For instance, she wrote code that happens when the button is clicked. She probably did that using an event handler on the button, like onClick. What she didn't have to do, however, was write the code that makes the button interact and change with the press.
6:57 She didn't have to write the code that makes that familiar clicking noise. It was already there for her. Buttons are, in fact, an interface. They provide the developer the means of controlling its interactions while abstracting way how that actually works, implementation wise. This button is part of the API or application programming interface and it's available from the application framework that was used to create the app. Because most apps need buttons, this interface has been created and provided to our developer or application programmer to use. By implementing the button interface, our developer was able to make her code run when the button was clicked. It's worth noting that the code probably also uses a media player API that is provided with the operating system of the device like Android or iOS.
7:42 This media player API abstracts away needing to understand details of how to send audio data to hardware. The developer knows they just want to call the play method on the media player API. I suppose too, the song isn't stored on the device, it's being streamed over the network, so a web-based API call has been made over the internet to get that data. It's actually APIs all the way down to the ones and zeros providing abstractions at many levels, so let's take a look at what all of these interfaces have in common. They all define ways for us to interact or communicate with an object, whether that object be physical or software, and as a user of the interface, we don't need to understand the implementation, we don't need to know how it works. We just need to know what we've been allowed to change
8:29 or see, interfaces abstract away the implementation. Now, while the UI or user interface is made for the user of the application, the API is made for the application programmer to use and extend in their applications. With the I covered in our API, let's take a deeper look at the REST of the API acronym. An API is a contract of sorts. It defines how it is expected to be used and it defines what you can expect to receive by using it. Think of it as a set of tools designed for software developers like yourself to use. An API is created to help make your life easier by giving you access to data and by providing you with an abstraction of the implementation, you know, not needing to know how it's doing, whatever it's doing.
9:14 They save you from needing to create everything yourself, like when designed well, they make nearly impossible things happen with just a few lines of code. Now you're probably starting to see why it's important to get familiar with them, right? I want to do a quick word of warning here about some naming issues that you'll probably run into. These days, the term API is almost always used as a way to explain web-based APIs. In fact, it's probably safe to assume that when you hear API in the wild, it's most likely that people are talking about a web-based API. We'll explore those here in a few. However, if we focus on all types of APIs instead of just the web-based subset, we'll get a better look into why web-based APIs are so popular, so hang tight, we're getting there. APIs are everywhere in your software journey just about everywhere you look, your programming language,
10:00 whichever one that might be, provides many APIs. Let's take a look at one that is common in all of them. They all provide some way of working with strings, you know, like a series of characters. Wanna make your text all uppercase? Just call the API method and voila. You could probably create the uppercase string all by yourself by doing some sort of low level bit math on each character, but you don't need to because your programming language has abstracted away that for you and provided a handy string API. It exists because this is a common enough problem that developers run into. Because these programming language specifications are just interfaces, different operating systems can supply their own implementation so that the code works properly in their environment. I think an easy way of picturing this is to think about how different operating systems
10:47 like Windows or Mac OS maintain their files. They're totally different, yet your programming language provides a single API for working with the file system. You can write one script and it works both in Mac and Windows. It's the implementation that takes care of all the details for you, pretty cool, right? Definitely something to be thankful for. It makes our lives so much easier not having to worry about what operating system is being used and the importance of an API tool language cannot be understated. There have been some fascinating legal cases where it's been questioned if the language API itself, again, not the implementation can be copyrighted, check out the notes for more on that. Along similar lines, there are different web browsers, you know, like Google Chrome, Microsoft Edge, Safari, Firefox, Brave.
11:32 Now, you might not have pondered this before, but isn't it amazing that you can write code for the web and it will work in any of 'em? This works because there's a set of web APIs that web browsers all must implement in order to make that magic happen for you. We are standing on the shoulders of giants, folks, and we really should thank those giants more often. There are authors and maintainers of libraries that package up code to solve common problems so that you don't need to do it yourself. For instance, do you know how to programmatically make a photo black and white? Me neither, but I can download and import an image package into my application and suddenly I can. I just call a method and boom, I wish real life skills worked this way. I'd import dancing so I could be more fun at weddings, cha cha cha. Frameworks provide an API that allow you to extend
12:18 what has been provided to make it your own. Like for example, most web frameworks provide you with the ability to add a new route to your web application. It allows you to write out dynamic data to a template that you specified, and you do all of this with just a handful of lines of code. You don't need to understand how it's doing things, but you do need to know how to write an implementation to what the framework is expecting. In this case, you're using the API, the contract, to write a valid implementation that meets the requirements. Now, all these APIs that we just talked about, they're local to our code base, they exist within our code. We use them to avoid recreating the wheel. That is to say they assist us in accomplishing tasks that we need to perform by abstracting away a ton of the work for us, the stuff that we developers shouldn't be worrying about.
13:04 We should be focusing on the business problem at hand, and that means a big part of your job is figuring out how to leverage these APIs to your advantage. Now, I think this is an excellent time for you to take a break. I just gave you a lot to think about and I'd like you to take some time and breathe. Why don't you jot down some APIs that you've used today? Even if you weren't coding, I know that you've seen some in action. If you're having a hard time thinking of some, I'll give you a hint. You're watching a video right now inside of a web browser. When we return, we'll explore the concept of remote APIs, ones that aren't executing locally in our code base and why that is such a powerful concept. (upbeat music)
13:59 It took me a while to find this remote control. It was in the couch. It's always on the couch. Anyways, this remote has an interface on it, right? There's a Power button and when I press the Channel button, it changes the channel, when I press the Volume button, it sends the request to my radio and the volume changes as if I turned the volume up using the interface on the device itself, and we know why that's important. I want to remotely control my device. I don't want to get up from my chair. There are of course, more or less lazy reasons as to why we'd want to remotely control objects. This cute little robot, for instance, has a remote API. You can send commands to it and it responds and it even sends back sensor information. These traffic signs can change based on an API call. This drone here is remotely controlled by an app on my phone, see?
14:45 (drone whirring and crashing) And it's not just physical objects that benefit from remote APIs. Oftentimes we don't have space on our local machines for all the data that's available, like for example, have you ever used one of those apps that helps you figure out what song is playing? You send up a snippet of a song that you heard playing at a grocery store using your microphone, and it sends back exactly what song it is. It's usually Phil Collins. It turns out that both grocery stores and yours truly love Phil Collins. You don't have room for all the songs in the world on your phone and with this remote API, you don't need to have all of them. It does all the searching and processing and it's surprisingly quick. How can it possibly do that? I don't know, but I love the interface. It's so fast. Ooh, that's another benefit
15:31 of remote APIs, computational power. Because an API removes the limitations of your local device, you can gain access to seemingly infinite amounts of power. A good example of this is the AR app of Google Translate. You can use the camera on your phone and in near real time it's able to translate directly to your screen, like instantaneously. It's using a ton of processing power to make that magic happen and that processing is happening elsewhere, "en la Nube." It took quite a bit of experimentation and some evolution of ideas, but over time, we finally turned this remote API dream into a reality. Now, I'm gonna drop some history into the teacher's notes, but I just want you to know that the struggle to achieve this concept of remote APIs was very real. Now, most of the problem was that
16:17 there wasn't a standard that everyone loved. Things were pretty clunky for a while as these ideas were sussed out, but then it happened. A new architectural style appeared that was clear and allowed us developers to interact with specific resources over the web and it quite literally changed everything. This style rapidly emerged as a winner and just about everyone began offering and consuming external APIs over the internet. Now, what I'm talking about here is REST, or representational state transfer. It was the approach that we needed and honestly, it's the abstraction that we developers deserved. Like we talked about already, the popularity of REST spread so rapidly that it nearly completely overtook the term API. It's like how we use the word Xerox to mean photocopy, even though Xerox is just a brand of photocopier.
17:05 Xerox does other stuff too, I think. That's how pervasive REST is. REST is for sure not the end all, be all of remote APIs. There are definitely some limitations which we'll discuss here in a bit. Technology's ever evolving and of course there's movements that are still advancing the concept of the best remote APIs. Some terms you might even have heard, like GraphQL, more on this in the teacher's notes. Now, for the rest of this course, we are going to do a deep dive into APIs that are written using the REST style. When APIs embrace the style and constraints of REST, they are said to be RESTful, cute, right? If you feel like you need a little rest, go ahead and pause me before we do a deeper dive into what it means to be RESTful. Sorry, that was a bad and obvious joke,
17:50 but as a developer you kind of have to make it. You don't really have an option. Because REST sits on top of web technologies like a really nice hat, we should probably do a whirlwind refresher of how the web works. (upbeat music) It might have been a while since you thought about how the web works. There are a smattering of terms that I'd like to reload into your working memory before we talk more about how REST uses them. So let's use the web, I've got my computer here and on it is an internet browser, the browser is a web client
18:36 and I'm going to use it to connect to a server. I'm going to do this by putting a location in the address bar, a universal resource locator or URL to be exact. You might also hear this referred to by its superset term URI, universal Resource Identifier. Now our URL here has a scheme portion and this one happens to be HTTP. HTTP stands for hypertext transfer protocol. Let's break that down a bit. Let's grab that last part, protocol. A protocol is also often likened to a contract. It's a little lower level than an API, but it's similar in concept. You can think of it as defining the expectations of how to communicate. Like an example that I think really sticks with me is if you've ever been to a rally
19:22 and someone does one of those chants where they're like, "When I say 'Fu,' you say "Bar." Fu, bar, one of those. Here, let's do one. When I say HTT, you say P, HTT, HTT. Oh, I just realized I might have made you yell "P" out loud. Hope you aren't in a coffee shop. Awkward. Anyways, we, you and I just set up a real quick protocol. I told you how to respond to my specific request and you would've been breaking or agreed upon protocol if you yelled anything else but P, so the browser creates an HTTP request for you, along with the URI, it also specifies a particular HTTP verb. In this case, it is using the get verb, which is used to clarify that this request will only receive data.
20:08 That is, it won't be making any changes. The server receives the request that was transferred from the client. There's that second T. The server then does the work that it needs to do. Finally, it generates the resulting webpage and sends back a response to the client, my browser, in this case. The most important part of the response is the body. For a webpage, that contains the HTML, hypertext markup language, which gives us the remaining parts, the HT of our acronym. Hypertext is text that can link to other texts. In HTML, we perform that linking with anchor tags, so with that response, the browser renders the page. When one of those links are clicked, they kick off a brand new HTTP get request and the process repeats itself over and over. In between those clicks though, the protocol's done,
20:54 we've made the request, we got the response. HTTP, as it was originally designed, is a stateless protocol. Once that request receives a response, it's all done. If you wanna keep some sort of state, your client will have to manage that itself and send it up with each new request. Check out the notes for more on this. There are more http verbs, also known as request methods besides get, most common scenario you see is when you encounter a form that is used to take information from you. Typically you'll see the verb post used to explain that you are submitting or posting data to the server. Okay, so our protocol that's used to transfer this hypertext is admittedly a little more complex than our rally chant was. There's quite a bit of important information in there that is included in the request and the response.
21:41 You've probably seen query string parameters in URLs and you know the stuff after the question mark? This is one way to pass information about the request. Another common way this information is passed around is through specific key and value pairs called headers. Both requests and responses have headers and they're used to help further communicate what is wanted as well as what has been sent back. Some of the popular request headers allow you to specify information about the content that you expect back, like the language or the type of content. You can also state that you want the page if it has changed since a certain time. This technique allows for caching, which is a powerful concept. Why get the whole page if you already have it in your local cache? You can embed authentication information in each request using a header. In this way, you're essentially keeping your client logged in across requests.
22:28 This response also contains headers. A super important one is the status code. This lets you know what happened on the server side and can convey things like missing documents. I'm sure you've seen the old 404 or maybe the page has moved, but hopefully everything is a 200 OK. It can also explain the content type, how large the data returned is, and many more things. Okay, got it? Are you feeling good about that little whirlwind tour? Remember, feel free to make me repeat myself if that was too much, I totally get it. That was a lot of information. It's important to have a basic understanding of these things because REST sits on top of them. It uses the concepts that were introduced with the web to expose resources that can be accessed and manipulated. Let's take a look at how the RESTful approach
23:15 takes advantage of the web and its tooling for your benefit, profit. REST stands for representational state transfer. APIs that meet the REST architectural style constraints are considered to be RESTful. Now, a typical way to teach about RESTful APIs is to cover the constraints that define it all upfront. Now, I don't know about you, but personally I find approaching learning that way pretty overwhelming, especially from the beginning. I mean, we haven't even looked at an API yet. On top of that, most of the REST APIs that you'll use will meet all of these constraints. So here's the thing. I promise that we'll go over all the constraints, but let's do it on our own time, like don't learn 'em all right this instant. We could even make it a fun journey, like a RESTful constraint scavenger hunt,
24:02 complete with prizes, avoid where prohibited. Here are the guiding architectural constraints required for an API to be considered RESTful. We'll check them off when we discuss them and then we'll loop back once more at the end to review. Gotta collect them all, sound fun? Good. Let's get started talking about how REST API sits on top of web technology, like a little hat, much like the web, the client makes a request to a server. The client in this case is typically going to be your program and you'll probably be using some sort of library to create the request to the server. The protocol used, you guessed it, it's HTTP and it's stateless. The server won't remember anything about the particular client. If you wanna maintain state like your login details,
24:49 you must send it with each and every request. You'll do this using headers and just like that, we talked about two of our REST constraints, client server architecture and stateless. You got two knocked off already. You're on a roll. Now, we should probably talk about what that request was requesting, right? Well, it most certainly was requesting information about a resource. Now a resource is a little abstract and that's intentional. It's the R in the URL or URI. We use the term resource to reference an object. Also happens to be pretty abstract, right? And this is because we can use resources to describe just about anything that we build. For instance, let's imagine an e-book store website. On the homepage, there's a list or collection of new books.
25:38 Each of these books is a resource. We click in to take a closer look at that book resource and we see a link to the author, which is also a resource. We click into the author resource and we see a collection of all the other books they've written. Maybe we even see a group of fans, again, another collection, and each of those fans are a resource. Clicking into a fan resource, we see a collection of their favorite authors and books. Everything is a resource. Let's change gears. How about an image sharing application on your phone? So we have user resources and those users post photos, which are resources, and those photos can be loved. So there's a lover resource, maybe there's an album that could be created. There's an album resource that contains photos and albums have a collection of contributor resources.
26:25 Turns out almost everything we ever want to build can be expressed in terms of resources and collections. Most of what we want our applications to be able to do to the resources can be expressed through the unfortunate sounding acronym CRUD, and that stands for creating, reading, updating and deleting. Now, as a user of the e-book site, all we did was a bunch of reading, no pun intended. I actually did intend that one. We were reading, right? Just getting information to display it like we retrieved the author page, we made a read request. The web browser used a get request to the specific author page. The RESTful API follows the same logic, your program sends a get request to a URI.
27:10 Then the server responds with data and of course all the HTTP headers you've come to know and love. The body is typically represented these days as JSON or JavaScript Object Notation. JSON provides a great way to structure and nest your data. Every programming language that I can think of provides some means of turning a JSON string into a native object. The content type is typically something that you can specify in your request if JSON isn't your thing. If you recall, there are other HTTP verbs available. These verbs are used in REST APIs to clearly state the intention of your request. Wanna add an author? Host to the /authors endpoint and send the data that you want to update in the body of your request and these fields will be defined for you somewhere. Wanna remove an author?
27:56 Send a delete to that specific author resource. Updates typically support the patch verb, and you'll see we're able to represent our CRUD needs, the create, request, update and delete using HTTP methods. Pretty cool, right? What this means is that you can interact with any application out there that's exposed their REST API. You could grab data, remotely automate tasks, and even smoosh together different services. This is called a mashup, and here's the thing, there are a ton of APIs available, just about anything that you can imagine, and if they're truly RESTful, they'll all feel pretty much the same. You can build anything, there are services that allow you to do incredible things with just a few simple API calls. So now the question becomes what are you gonna build?
28:41 So why don't you take a quick break and when you come back, let's explore a RESTful API together. We'll take a peek at how to use the music streaming service, Spotify and their excellent RESTful API. (upbeat music) You have some newfound understanding of how web-based APIs work. I walked you quickly through some imaginary REST APIs and now I'm thinking it's time that we get a little more specific. Why don't we go exploring together? Some of these REST APIs allow you to give it a try from the comfort of your very own web browser. One of my personal favorites that allows for exploration is the streaming music service, Spotify.
29:27 So here I am at spotify.com. Now, a quick word of warning, it is going to look different. I guarantee you this site is gonna look different because they're always making things better for us, for us Spotify users, us lovers of music, and as well us developers. So I want you to try this. When you go to a site, any site that you want to, you'll see here that this is really nice, Spotify's letting us kind of test out what it feels like in the web, on the webpage. But if I scroll down to the bottom, that's typically where they hide nerdy stuff. Now, I knew this was here, but if I didn't, I would look around for something like this, this Developers, and I'm going to click into here. Oh, I cannot tell you how good it feels as a developer to end up in a place where it feels
30:13 like you're being taken care of, right? This is so nice. The Spotify for Developers, it looks like it was thoughtful and I can tell you that it is very nice. It's a very nice tool. You can scroll down, you can see all sorts of stuff. They give you some examples of what you might want to do, right? What could you build with this? So you could find out like different things, like you could use the API to see the beats and the sections and the bars. I think that there's a way to figure out if a song is danceable or not. So maybe I could import dancing. We'll see. And if I scroll back up to the top here, you'll see that there's these different headings. We've got this Discover, I think this is great if you're going to explore things, we have this Docs, you'll see that there's all types of different, actually,
30:59 APIs and SDKs, software development kits, which are APIs, which allow you to do different things. You'll see that there's ones for iOS, there's ones for Android. What we're gonna be focusing on is the one for the web APIs. So if I click into that, we'll get a nice overview of what this is. I'm gonna blow this up just a little bit. So you'll see based on some simple REST, representational state transfer principles, that's what we're doing, that's what we're going through. We're going through our scavenger hunt. We're going through that. There's a Spotify web API that returns JSON, JavaScript Object Notation about music, artist, albums, and tracks, right? So that's awesome. That's great. So we can see all sorts of different stuff. It opens up at this api.spotify.com is the specific URL and then it has a set
31:46 of endpoints with each unique path, right? Each of those URLs that we were looking at before, there is a way to get specific data. So you could do this thing where you authorize. So, I could be a user, you could see what I recently listened to. I could have a playlist, I could add to it. But in order to do that, you need to have authorization to be able to do that. You don't wanna be able to just add to anybody's playlist, right? So there is some authorization that's available and we are not gonna do anything that requires that here. So if you keep on looking down here, you'll see that there's, it's based on those REST principles and you'll see that it does get to retrieve resources, right? And there's the post to create resources, and again, resources here are songs and artists and tracks. Maybe the resource you're creating here is one
32:33 of those playlists that we talked about. You can change using a put and a delete will delete the resource. So awesome, right? So it's using REST like we saw. You'll see that Spotify uses this URI style. So it's a Spotify track and then it has this unique ID, this Spotify ID. This will make more sense as we dig into it a little bit more and then you'll see that there's always this Spotify URL that allows you to see what it was. And it will show you this as well. As we said, the responses come back and they are a JSON object and we're gonna be looking here at a tool in the future called Curl, which you can use from your command line. That's what this dollar sign here is talking about. I'll show you all this, but basically it's saying
33:19 that there's a lot of artists that come back. You don't want all the artists in it, it will come back paginated and of course, you can use caching. We'll take a look at that here, you can see if the thing has changed or not. So we'll take a look at that and basically the response that comes back, if everything's good, it's gonna say a 200 or these, these 200 level accounts. And then there's bad requests. So if you ask wrong or maybe it wasn't found, that's the 404 or unauthorized, if we try to do one of those playlists, we get an unauthorized and there's also if you're a developer, you never wanna see this one, it's the 500. It means that you made an error on the server side. We won't be doing any of those 'cause we're just gonna be hitting somebody else's code. Cool, all right, so Spotify offers the ability to,
34:06 you can look at this and you can build your own curl request, but one of the really nice things that I love about their API, and you'll find this happens often, if we scroll back up here to the top, sorry if I'm making you sick there, we're gonna jump in here to what's known as the console. Over on the left are the things that we can do with their API and one of the things I wanna do first is I want to search and you can see it's making a get to the search endpoint. So I'm gonna go into there and what's really nice is it's set it up so that we can kind of build the query that we're looking for. So now we need to decide what we're gonna do. I think probably the first person that I would ever search for, the first artist I would search for. And you can see that here it's showing the different values of what that is. I would probably search for Beyonce.
34:52 I mean, right, wouldn't you? And I could choose, is that a track or an artist? So the type there is important, and if I type artist, you'll see that what's happening is you'll see the curl here is changing, it's changing the, it says the cue is the Beyonce. So that's one of the parameters that we're passing into the API and artist is here. And you can see there's different things that we could do here. What's really important is that it needs to have this OAuth token. Now what's nice is we can build one of those even if we don't have a Spotify account. So I'm gonna do that. I'm gonna click this Get Token, I am not currently logged in and I actually don't have a Spotify account yet in this user. I do have a Spotify account. I love Spotify. And this is asking, basically what you would do is you would create a specific OAuth token. If you were ever to use this API outside
35:38 of where we're doing this right now, you would create one of these tokens so that you could log in later and you say what it is that you want. So here's the playlist, right? We don't, we're not gonna do any of the playlist stuff, we're just gonna do a search. Maybe we'll look up some of Beyonce's tunes, but, so I'm gonna go and I'm gonna click this Request Token. So now it's asking me to either log in with my Spotify account, which I can, or I can create a new one, and let's walk through that just in case you don't have a Spotify account already. Let's create a new one. And this will be at my developer account here. So I'm gonna put in my email. So my email, I don't know if you know about this, but if you use Gmail you can do a little plus sign and it will still send to the left side of your Gmail. So I'm gonna do at Spotify sign up and that way
36:27 any emails I get from here, it will have that. And it's kind of a way to let you have multiple accounts. And I think, so again, my Gmail is [email protected], but I'm gonna do [email protected] and it will go to my Gmail. It'll just kind of ignore this, Gmail does that for you. It's pretty nice, right? So I'm gonna put in the same password as my luggage, which is 1, 2, 3, 4, 5, 6, just kidding. And what should we call you? Oh, you know what? I love it when you call me Big Poppa, that will appear on my profile, I am ready for it. And let's see, it is my birthday on November 19th, 19.
37:16 Let's see, what do I, I think, wouldn't it be nice if I was born in 1999? What is your gender? Oh, this is nice. How nice. This is like super inclusive. I love that this is so inclusive. It's like somebody at Spotify went and talked to their people and listened to what people wanted and they included people who identify as non-binary or other, or prefer not to say. So like wherever you sit in that I love and hats off to you Spotify for being inclusive, 'cause inclusivity matters. Share my registration data. Sure, why not? I am not a robot. I don't think I am. Oh, oh no. The images with bicycles. That one's got a bike. That one's got a bike. That one has a bike.
38:03 Oh, what is that garbage there? Is that, this is a bike lane. This is a bike. Okay, here we go, please. Okay, I am not a robot. Thank you. All right, so I'm gonna click Sign Up. Oh, I am male. I am male. So here we go. That's how I identify. All right, I'm gonna save that log in and I am going to log in with it. So here we go. All right. So it's asking me if I have access to this developer console and it'll see my logged in as Big Poppa, here we go. And now you'll notice here that there is this token here, right? This is this OAuth token that got created for me by Spotify and it's putting it into
38:48 this console for me so it can share it. Now you never wanna let your token out. If you get a hold of, somebody gets a hold of your token, they can do stuff on your behalf. And I realize that this is on a screen being recorded and I'm going to get rid of this token here in a bit. So you will not be able to have it. So I've got this token, it's filled out here. I'm gonna click Try It and we're gonna do a search for Beyonce. Let's see what happens. So you will see that we got some information back. So we got a list of artists, 'cause that's the type that I searched for. And this is the search that we did. We'll see some external URLs and we think that this is probably her. I'm gonna see, I'm gonna grab this really quick, but you can kind of see how things are laid out here, right? So this is my Spotify URL for that. Let's see if this is her, this looks like her.
39:37 Yeah, yeah, definitely. So, great. "Crazy in Love," "Cuff It." I haven't heard that one yet. Awesome. So this is Beyonce, great. So we got her, we found her, we used this search. It has, shows how many followers she has. Wow. 32 million. Good job. And it tells the the genre. So we can do all sorts of really cool stuff with this. And it's here, you'll see that I'm getting a URL, I'm getting a link that will open up her specific artist page, right? So, here is her ID. So if I take her ID, I'm gonna grab this ID and I'm gonna come over here to Artists and I can come in here and I can show all of the artist's albums. So you'll see it'll be artists/ID/albums. Let's see all of the albums that she did. But let's look at that in JSON format.
40:23 So the artist that I want, I'm just gonna pipe that ID in there from before, right, now it still has my bearer token. The console has done that for me and I can go ahead and do Try It and we should see that now, it's gonna return back albums, right? So we've made a get request to the albums and I can see all of the albums that she's had here and we can see where they're available in different markets as well. Looks like she's available in lots of markets all over the world. Who runs the world? All right, so here we go. We are going to look, let's see, these are albums. That's the artist there. That's the type of the artist. Let's get one of the albums here, let's make sure that we got it. So you see that it's got different things like I could get access to maybe the image of what this album is.
41:10 So let's do that, I'm gonna post that here, open that up and let's see, there she is on her horse, her magical horse there. So that's pretty cool, right? This is awesome. I can get access to all sorts of information about an artist I can get by their ID. I can go and find all sorts of different things that I wanted to do, right? So, if I knew an album too, once I have the ID, I could get the ID, I could get the tracks, all sorts of amazing things. And then there's these playlists, right? So you can add a playlist, if I wanted to, I could post to create a playlist. Now of course I need to authenticate to do that and that's a little bit of what we're doing here, but I could add, put together a playlist for everybody to do using this API.
41:55 And again, when we are looking at the artists, when when we're looking at this get artist, it's showing this curl command and we're gonna take a look at that here in a bit. But I just want to take a little bit of time. I want you to go and take a look, go back to this Discover section and kind of get inspired by what's available, right? So you can look at, there's these different things where you can, I was right, you can check and see if there's danceability of a song. You can go and find a track and is it danceable and you know that Beyonce's is, the songs are gonna come back very high in the danceability thing there. But lots of powerful things and there's lots of cool examples that you can do that people have already gone and built. And you can take a look at how they built it and how they use the API recommendations. This is awesome, really this is like super wide open
42:43 to you and you can do all of this because of the APIs that they provide. Pretty cool, right? You can get access to all of Spotify's music library and you can build tools that extend their platform. I highly recommend that you go and take a look at what other people have built using these APIs. It's super impressive. Did you notice how fast those results came back? How could it do that search so quick? Oh wait a second, does it even need to? Those results don't change all the time. It's not like Beyonce is dropping a new song every minute, though I wish she would. Those results are definitely cacheable, ding, ding ding. Cacheability is also available on the client and we'll get to that here in a bit. But this is a great example of a layered system. I'm almost positive that Spotify has set up things so that it returns the same JSON
43:29 from its own server side caching mechanism. It's not rendered that on every single request. As a user of the API, I don't know how it's doing that and frankly, I don't care. I want it to be available and fast when I hit that specific URL. Beyonce definitely appreciates how fast the results will return. She might even take that API dev to Red Lobster. Let's go ahead and take a quick break, during this break, I'd love for you to move around and think about what you might be able to build with that Spotify API. For instance, while I was using this API, I had this idea pop up. When I'm at a music festival, I often have a hard time deciding which band I should go see, there's so many. Now I would love to have an app suggest to me which of the bands that are currently playing I would most likely enjoy. You know, based on my listening habits,
44:14 I'm pretty sure that you could build that just with the Spotify API and using a list of events of the bands that are performing. And if you do end up building that, share it with me 'cause I need it. What other interests do you have that provide an API that you can use? Check the notes for more ideas. Now, after you come back from your break, let's dive a little deeper and make use of an API using our own tools, see you soon. No really, take a quick break. Seriously, it'll do you good. No binging. (upbeat music)
45:01 While I realized that exploring an API on a website is informative, it might not feel like you're actually using that API. I think the next logical step in exploring is actually making that request yourself from your computer, not from a website. Now as I pointed out in the Spotify docs, there were examples that were using a command called Curl. Curl is an open source tool that is used to send data back and forth and it runs locally on your computer and it's awesome for poking around at APIs. What do you say we explore it a bit? Okay, so we are back here on our Spotify page and we see that there is a command here that says Curl. Curl is a command that we can run from our terminal. Now if both of those terms are new to you, command and terminal, this is a great time to head over
45:48 to the notes and learn about what the terminal is. So my operating system is a Mac and it has an application on it called Terminal. So I'm gonna open that up, Terminal, and in fact, I'm gonna make this a little bit bigger. Let's go ahead, let's let it take up the whole screen, we're gonna be in here for a little bit. So I'm gonna type Clear and clear my window. All right, so this terminal allows me to perform actions on my computer. Like I can make it do text to speech. I can do Say and I can say, "Hi mom." - Hi mom. - And it says, "Hi mom." And the Windows operating system has a terminal as well. And while it's a little bit different, they're actually pretty quite similar. But check the notes for more on this. So what happens is I write the command and then I pass in values for the parameter.
46:35 In this case, it's "Hi mom." Now, note that I used quotes there because I wanna pass one value, even though there is a space, if there was a space, that's kinda how you separate things. But with the quotes it's saying this is one value that I'm passing in. We developers spend a lot of time in our terminals, so if this is new to you, I want you to remember that it was new to everyone at one time. So please take your time and don't let this get you too frustrated, you've got this. So I already have Curl installed. I'm gonna just go ahead and make sure that I do by typing the word "curl," and if I see this, I could see that I could type curl and then the command "--help." Now if I didn't have this installed, if this came back with like, I don't know what you're talking about when you type curl, the word curl, that's okay.
47:21 So we're gonna head over to the notes and you'll get sorted there. But the starting point, where I want you to go is curl.se. So you'll come to this page, you'll see this as a command line tool in library for transferring data since 1998. And that logo sure feels like that, doesn't it? It does all sorts of things. What we're really concerned about is this HTTP, right? We're going to use this from the command line to get access to a URL. So if you don't have curl on your command line, go ahead and pause me and check out the notes for the best way to get this installed on your computer. There's a lot of different ways to do it. If you come here under Download, you'll see that there's different operating systems. So you've got your Linux and you've got your Mac OS. And if you come down here, you've got your Windows.
48:07 So there's different ways of getting things installed. So go ahead, take your time, get that installed. And when you come back, I want you to try this. I want you to do this thing with me. So I'm gonna go back to my terminal and I'm gonna make sure that it works. The way that I always like to test is I like to go to icanhazdadjoke.com. How do you get a baby alien to sleep? You rocket. Oh, so good. Anyway, that's how you do that. That'll let you go and it's gonna hit that website and it's gonna give you back a joke when you're ready. So now that we've got our curl up and running, we've got our successful dad joke, let's go ahead and run that command from Spotify. So I'm gonna come back over to my Spotify thing
48:53 where I was running the search. I'm gonna make sure that it's still working actually before I get started, 'cause these tokens can expire and depending how long your break was, it might have expired, but mine is still working. So I got this back here and I should be able to get this from my command line. And what I'm gonna do is I'm going to copy this, I'm gonna highlight this. This is a development practice known by US developers known as copy pasta. So I'm going to copy this and I am going to go back to my terminal and I'm going to paste and this long list of things here, what this is doing is just saying, hey, I wanna make a get request to the api.spotify.com/v1/search. Remember, that's where we were at. We were searching and we're passing the query string of Beyonce and the type of artist and we're telling it, "Hey, I would like
49:38 to get back JSON and I am sending you JSON," even though we're not actually sending JSON. The default has that there and this is an important one. This is sending you a header, right? The "-H" is a header and authorization. And this is that long token that we had there. Now if all is working, when I press Enter, I should get here locally on my machine that same list of data that we did and I did, check that out. So what I'm gonna do is I'm gonna come back up here and I am going to grab, wow, there's a lot of information here, right? When this comes back, I'm gonna grab her ID. And so I remember that when we were looking at it, it was down in here, this ID, is this the right one? Is that the first one? So we got items, no, this is her ID here, this is her ID.
50:24 It starts with a six, VW. So I'm gonna copy that and I'm gonna use that. I'm gonna go back and remember I'm gonna go and I'm gonna look, I wanna see what albums she has. So I'm gonna come into here and I'm gonna remember, I'm gonna paste this URL here, and I'm gonna run this, make sure that that's still working. I'd like you to go ahead and pause me. I think I'd like to see can you get her albums locally on your machine? Can you go ahead and make sure that you can do the same Get Artist, see all of Beyonce's albums on your local machine? So here's what we'll do, you give it a try and if you, once you get it printing locally on your machine, you can let me know and then unpause me and I'll show you how I did it, okay?
51:10 Are you ready? You got this. Pause me. (upbeat music) Cool. So how'd you do? So here's probably one way that you could have done it is just copied that, right? Copy this whole thing and pasted it. But what I wanna show you is that we have access to, we know what these URLs are, right? So we know that there's this v1 artist and in fact, we can see that there is a get of /v1/artist. And then you can actually pass their ID in the URL there, right? That's what this ID is here. So, let's just go back. I'm gonna show you a little trick too. So, I've got her ID copied still, so I could have just, just ran that query and copied the curl and pasted it here. And if you did that, you did it right. I just wanna show you a little bit more of what else, how else we could explore.
51:57 So I'm gonna press the up arrow and you'll see my cursor's moving here. I'm gonna press the Option key on Mac. I think you're gonna press the Alt key on Windows. You can jump around like this and I'm going to get rid of what's here, and I'm gonna say v1 artists, right? So that's the collection of artists. And I have that specific ID, right? So there's the ID and I'm gonna press Enter here and you'll see that I got back Beyonce, which is awesome. That's what I did and then remember, the artists have, I'm gonna go to the front of the line, it's Control+A on Mac, and I'm gonna switch this so artists have, this is specific artists, so Beyonce, and we wanna see Beyonce's slash albums, which is what the console would've generated for you. But I just wanted to show you that you can kind of navigate and move through and you'll see that there's
52:43 a bunch of different information that comes through here. It's kind of hard to see, right? So, kind of hard to parse. And this is something optionally that you can do. So I press the up arrow and I'm going to press, pipe that into a tool called JQ and you'll see that it comes back very nice and pretty, right? So this is something that is very nice to have and I piped that. That's this, the symbol above the Enter key is this pipe symbol and it allows you to take whatever happened in this first part of the command and take its results and put it in this part of the command, which is super cool. And again, this is in the notes. You don't need to, you don't need to worry about this, but I know that I can query things. So I can say that of the items, I'm only interested in the name,
53:29 the .name from each one of the items. And this is a little query language that's attached to this JQ program. You don't need to do this, this is just something, if you're doing it from the command line, it's something that we do that we could kind of hack around with a little bit and you'll see I can get all the lists of her albums and that's pretty cool, right? That's powerful, and if you are gonna just start poking around and moving around with the URLs, one thing I wanted to make sure that you're cautious of is if we come back and we take a look at the console page here, they do a nice trick for you. If we're back here in search, like one of the things that I wanna make sure I know that Beyonce has this song that's like something like, who runs the world, right? Who runs the world? Girls. But I don't remember where, what album it's on. So I'm gonna search for a type track. Now, note that my query has spaces here.
54:16 Now what's happened here is it's done this URL encoding. So like if you were trying to mess around and work with the search, you'd wanna make sure that whatever you did, you passed that with those plus signs there. So I'm gonna run that curl command and we'll see who runs the world, girls. And I am going to, I see there's this open link here. So I'm gonna go ahead, I'm gonna open that. Let's take a look and make sure this is the right track. I'm pretty sure it is, it is not the right track. What is this? Femme phan? Maybe it's some other, some others. So let's take a look. Well, that's interesting. So, oh, I just went to the bottom instead of going to the top. So I should have went to the very top of these results
55:04 or I should have pulled, probably pulled the name out. Rock-a-bye, baby. There's a Rock-a-bye baby version of this. Whoop, okay, so when the color stops, hope you're not getting sick of all this scrolling. So Beyonce is the artist that we want. So this is the one, we want to hear this song. Let's see if I got the right one this time. This is just something, you know, there's a lot of data coming back here. It's kind of hard to do, to use on the command line. Sometimes I'm a little cutsy like you just saw. Here we go. Let's see what happens. Let's make sure this is it. There it is. There's Beyonce. Not the song I was looking for. Oh yeah, it is, "Who Runs the World? Girls." And now I can listen to that. So it's like, who runs the world? Girls. It's kinda like who runs the terminal? Curl. Boy, that was a big setup just to make that joke.
55:51 I hope that you enjoyed it. You can has dad joke. Curl is an awesome way to explore APIs and you'll find that often, just like we saw with Spotify, APIs will use Curl in their docs to show you how they work. Spotify is just one API and there are so many companies and services out there. In fact, some APIs are the product. Like that's what the company's selling to us developers. So now that we've been building up those muscles, why don't we take a swing at another API? Let's use a different API, this time let's use Twilio. Twilio is a company that loves APIs. In fact, that's what the company started as more than a decade ago, just a set of APIs. It started as abstracting away communication channels. And by that I mean any way you'd ever possibly want to communicate with your users.
56:36 Wanna send a text message? There's an API call for that. Wanna make a phone call? There's an API. Send an email, a WhatsApp message, literally whichever way you want to communicate, there's an API for all of them. Developers embed these APIs into their apps and I'm pretty sure that you've probably used it without even knowing you were. Now, nowadays there's a whole suite of products that Twilio offers that they saw their customers needing. But under the covers it's still these APIs that are powering everything. I need to be a little upfront here with you. I love Twilio, I mean, I love it so much, I even applied for a job with them and I got it. But even if they didn't hire me, I know that I'd still show off how powerful their APIs are. I mean, with a single command, you can send a text message. I remember when I first did that, I was like, whoa.
57:22 Like that level of abstraction is amazing. I mean if you've never thought about it before, what even is a text message? I mean, I know I have an app on my phone and I can use it to communicate with literally anyone else. But how does that work? Like there's all those different cell carriers and standards and phone numbers. I mean, I have no idea. And with Twilio, I don't need to know. That's the beauty of this API. It just works here, let me show you. All right, so here we are at twilio.com and again, word of warning, this is going to look completely different from my right now to your right now. So please make sure that you check the notes for anything that might have changed and is specifically about this flow that we're about ready to do 'cause we're going to sign up for a trial account. So I'm gonna click the Sign Up button here and this is going to look completely different.
58:08 I know because our team is constantly making this better. But the one thing that I wanna make sure that you know right now is that you see that there is no credit card required and it is a free Twilio account. So I'm gonna put my information in here and I'm gonna use the same trick that I used before where I do plus and I'm gonna say, "[email protected]." So, the plus sign there again, the little Gmail trick, we're gonna go ahead, I'm gonna put in my password again. 1, 2, 3, 4, 5, 6, my luggage. How many times can I do that joke? And then I gonna do this accept, must be 16 or more characters. Okay, there's some password requirements over there.
58:58 I think that's 16. All right, here we go. Starting my free trial. I'm ready. Let's do this. I'm definitely gonna save that long password 'cause I need that for later. All right, so I have now should have been sent an email. So let me make sure that I got that email and I did, I am going to click the link to verify it and here we go. I'm gonna verify my phone number to verify I'm a human 'cause all humans have phones. So we're gonna do (432)-527-4274. Please don't call too late. I'm gonna try that. I'm gonna click Verify. All right, so I got my verification code. Hey that came very quickly.
59:44 Well that's wonderful, my verification code is 017869. Hopefully your code's different than that. Wouldn't they be funny if they were all the same? All right, so here we go. So which product are we here to use? We're gonna use SMS. We're gonna use the SMS messaging product and what do we plan to build? Let's see the, of course, these questions are gonna be different. What are we gonna do?
…