Getting Started with Spotify API (Complete Overview)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and here is where um you pass in the authorization details so hey friends welcome to the video i just want to say a massive thank you to a whopping 4 000 subscribers i'm really chuffed about it and it's nice to know the content i'm making is helping you in your programming journey with that said i'm going to be celebrating on the weekend with a big fat takeaway i haven't decided what i'm getting yet probably not good idea in lockdown but you know go enjoy yourself and celebrate these moments with that said let's keep this community growing so make sure you smash that like button if you're learning programming i want to see more videos like this now let's get on to the video today we're going to do something slightly different in this video i'm going to give you a complete overview of the spotify api what i'm going to do is walk you through the spotify api through the lens of a programmer you're going to get a better understanding of what the api has to offer everything from libraries and interacting with tracks to understanding authorization and how you would go about doing that depending on the app that you're trying to build and the bonus at the end is we're gonna look through an end-to-end example of creating a playlist with the spotify api in python i highly recommend that you watch the entirety of this video because not only will you learn how to navigate the spotify api but you're also going to get some pro tips on how to work with other apis in the future with that said i'm sure you're going to enjoy this video so buck your seatbelts and let's get started [Applause] [Music] right the first thing you want to do is you want to go to developer.spotify.com um i'll put a link in the description below and essentially this is what the homepage looks like so it's spotified for developers and hey as you can tell you've got a few links now i'm going to break this video up into three parts the first part i think will browse through the api documentation just to give you a flavor or give you a better understanding of what spotify offer out of the box which is quite cool so essentially we'll look at the different things that you can do with the spoiler api so that's the first thing i'm going to do the second thing i'm going to do is then look at authorization and rate limiting authorization i think deserves its own entire section because i get a ton of questions about like how you go about authorizing with the spotify api because there's a couple ways to do it and you might want to adopt a specific authorization flow depending on the app you're trying to build and then we'll leave the best to last which is actually interacting with the api so we're going to write a very simple python script that actually creates a playlist using the spotify api and we're going to keep it simple we're not going to do anything too complicated i've got a project actually on my youtube channel where i automated the process of adding a music video from youtube to spotify and so if you're looking for something a bit more complicated fun challenging all that jazz i'll include a link up above so for free to click that but again the goal of this video is or in the case of the last part we just want to do something simple and i just want to show you how you can uh do that by creating a playlist with the spotify api right so let's start with hovering over the docs link and as you can tell you get a nice little menu here and you've got a couple of things right so as you can tell here you've got ios and android so essentially if you're building a mobile app you're gonna want to be referring to these links here but if you're building like a web application you know a python or javascript file that's gonna run locally um then you're gonna spend most of your time in the web api um by the way later in the video what i will do is i'll go over examples of different types of apps that you can build and in those scenarios what kind of authorization flow um you'd adopt so uh we'll keep that for later on in the video but for now again we're going to build a simple python script so what you want to look for is this link called reference and as you can tell reference also exists in every sub menu here so you've got it under the web playback sdk ios and android and the reason why is because reference essentially this is like the crux of the spotify api this is essentially where you're going to spend most of your time and this is because um this is the page where spotify tell you all the different uh bits of information that you can get from the api and they expose all the different things that you can do with the api and so um and they get quite specific with that so as you can tell on the left hand side if you've used spotify before i imagine if you're watching this video you have then you can see on the left hand side already there's uh it's already starting to ring a bit familiarity right because you've got like playlists and you've got browse and you've got follow and library and all of those things are essentially uh components of the api that you can sort of interact with so let's start by clicking onto playlists so if i click on the playlist um again there's quite a bit going on here but we'll go through it so on the left hand side first it's quite um it's quite nice it's quite intuitive you get a clear understanding of exactly the thing that you might want to do so say for example you're building an application that's going to need to get a list of a user's playlist great you've got the link right here right so you can click onto that equally if you're building an application that needs to get a playlist create a playlist reorder you know here essentially it's quite intuitive in that spotify give you a clear understanding of the different things that you can do uh with playlists using the api and the same pattern is followed with the library so you can in this case your spotify library so here you can get a users or the current user saved albums you can remove a user's saved tracks so you know essentially each section will have a link that basically tells you something that you can do with the api with respect to that category so i'm going to go back to playlists and then i'm going to go into create a playlist the way the docs are structured are such that if you click any one of these links they're all going to have the same headings which is really handy because you can get straight to it and it builds that familiarity which makes it easier for you as you build out your application with that said we have the endpoint and annoyingly actually they don't put the endpoint here they put on the right hand side not sure why they've done that but endpoint is quite straightforward it's basically the euro that you're gonna you know hit request call i use them interchangeably but essentially this is the api euro that you're gonna hit to um uh create the playlist now as you can probably tell sometimes in the euro you might need to substitute a value and what do i mean by substitute well in this case if there's braces it means you need to replace the value here so you need to provide a value for the user id and actually they make that easier for you here under the request parameters so they sort of tell you that you need to provide the user id and they say this is the value you need to provide and um it's quite good they link to exactly uh what the user id looks like so if i click onto that as you can tell you've got the spotify user id it tells you uh what it looks like oh it tells you what it is and then it tells you what it looks like and usually that's just your username but lowercase so that's quite handy so if again if you make a request this euro you just need to uh provide the user id you also have header fields and this is where authorization gets quite interesting so i'll cover this in the next section but essentially header fields usually with any api call you make not just with the spotify api uh typically this is where you pass authorization details so if you ever think about you know an api that you know require in well in spotify's case requires some sort of authentication um you're going to need to provide some sort of token secret um usually you pass it in the header fields um in the authorization field specifically so uh we'll get onto that in the next section but essentially it gives you a bit more detail about that and really the last two sections are quite straightforward the body parameters is probably the most important this is basically where you provide the data so um in this case of us creating a playlist it tells you that you know you need to give a name so you need to give a name for that playlist you need to tell spotify if it's public or private um if it's collaborative and you can give it a description too but also on the right hand side it does tell you you know if you have to do it and in some cases you don't have to do it so the name is required like you you have to give the name but there's other you know in this case public and collaborative optional so if you don't give it you know you can still create the playlist so that's worth knowing just keep an eye on that um because not all requests require that you provide you know the value for every single one of these um parameters and in some cases you're not might you might not need to provide any at all so uh that's worth bearing in mind and then the last thing is the response format and this essentially describes what you're going to get back so assuming that the um the api request went through successfully usually that's indicated by a 200 or 201 status code um it will tell you you know what kind of data comes back and you've got a few things going on here the first is it will tell you uh the object so spotify usually it works in this object notion so you know a a track is an object a playlist is an object a user with spotify is an object right and again they actually link to it so if i click on the playlist object you get a full breakdown of all the different fields in that object so you know in this case a playlist will have um all these different pieces of data like the name the owner whether it's public the tracks in that playlist so that's quite handy and um that's a playlist object really and then on the right hand side um this is quite cool because if you scroll down a bit it actually gives you a sample of what um the playlist object looks like or in other words what it's going to return so that's quite handy because you can copy this maybe and you can test it with your application maybe you just want to see if your application handles um the response that you get back when you're creating your playlist without actually you know implementing uh the api call itself this acts as good test dummy data so that's it really for just understanding what the api offers again you can take your time to explore each one of these sections because you know sometimes depending on the application you want to build and i'll get onto some examples in a second um you might want to see first of all if it's even possible because um sometimes you know i've come across the api it's not just a spotify api where i've wanted to do something but unfortunately the api doesn't expose functionality to do it and so um you know you can't do it unless you work your way around it there might be a way around it depending on again what you're trying to do but um so it's worth just browsing around and if you have an idea for the application you want to build uh just check the api reference just to see if they have what you're looking for right so let's move on to the fun stuff authorization i'm telling you this section is funny how authorization because authorization isn't just something specific to the spotify api um it's for other apis too and it throws off so many developers you know it causes a lot of confusion a lot of beginner programmers don't know you know how to navigate authorization where to start how even works because there's a lot to understand when it comes to security security it's just not um the easiest thing to understand it's actually quite complex um so anyways let's go to the section so you can find it if you click guides and then you click the authorization guide here you get a full guide provided by spotify as you can tell it's fairly long um but this details basically how you go about authorizing with the spotify api so as you can tell the authorization guide is actually quite long now you don't need to go through all of this my job is actually to highlight the important sections and hopefully give you a better understanding of the things that you want to focus on as you build out your applications so if you scroll down you'll come to the scope section and now this is where i'll probably give you an example of an app you're trying to build and how scopes are relevant to that so say for example you're building a web application where you're gonna allow users to log in with their spotify account so let's get creative maybe you build a web app that um shows a user some visualizations of the kind of tracks that they listen to and maybe categorize it by genre and you get a bit creative by artists and essentially with that kind of application it's going to live on a web web server somewhere it's going to allow spotify users to log in and it's going to need essentially the data on spotify right so with that kind of application scopes become relevant because scopes are a way for user to have control over what data they give you in your application so if you've ever used an app that gives you the option of signing in with facebook then when you sign in with facebook usually asks you do you want to give your name uh or well of course usually for the most part actually you have to give your name i believe i can't remember but it'll be your name it'll be your friends list it'll be your date of birth right and the user can option you choose or to not give that right they can sort of continue to sign up but some apps actually require that right because some apps might need access to your friends list uh depending on the application essentially that's scopes in action where facebook are allowing a user who's signing in to another app with facebook to be able to restrict the data that they give away or the permissions that they give away to the application um funnily enough the irony with facebook and them being in the news recently with respect to privacy and whatsapp but nevertheless um anyway scopes in this case especially with spotify allow a user to do a similar thing right so if your application again is going to show visualizations of you know what a user is listening to on spotify in that case you don't need to be able to you know create playlists with that spotify users account you only need the scopes to you know read their playlists or um i don't know what the read email one is but you have a list of scopes and you don't need every scope um so if i click on scopes here uh what it will do is it will hopefully should give a list of scopes somewhere authorization scopes yeah as you can tell there's quite a few here now again depending on the application you're trying to build you only need some of these scopes and a user when they sign in with the spotify 2 application they'll be able to select which ones they approve so just bear that in mind don't ask for everything at the beginning because it's unlikely a user is going to want to continue if you know you're trying to edit their playlists when you're trying to build a simple web app that's all it's meant to do again it's just meant to visual um has some visualization uh visualizations for the tracks that they listen to right so moving on to authorization flows and this is where it gets quite fun as you can see there's quite uh there's a table here and it lists out four different authorization flows that author all authorization flows that you can use right so um uh the key thing that you want to look out for is this column here which is access token refresh now very quickly when you authenticate with most apis or more secure apis and spotify of course fits into that category um what happens is you usually get a token and a token is something that lasts you know usually a couple of hours maybe in some cases just one hour or maybe a day but in any case that token doesn't last very long and your web application um in this case is going to have to handle the case where the token expires and usually you do that with this refresh token mechanism and so if you're building that web app that we talked about um you're going to need to use one of these flows because that's the one that's going to give you the capability to refresh your token when it's expired and that's essentially important otherwise your users are going to have errors um when they you know they're trying to view the graphs of you know what tracks they listen to but they get an error saying you know your tokens expired you don't want them to have that experience so you're gonna need to use one of these authorization flows uh that have access to the refresh token now if you're writing a python or javascript file that's just gonna create your playlist which we're gonna do in a second in that case you don't need this complicated authorization flow that you know has access to refresh tokens you just need um one of these which is basically quite easy to implement code wise you know there's not much to it and it doesn't have access to a refresh token that's totally fine because these tokens they don't last for long it only lasts you know an hour a couple of hours and that's fine because you're not going to be running the script uh 24 7 you're not going to be put on a web server somewhere um this is just something maybe you know you just want to uh like a hobby project or something you might share with a friend with that said it's quite easy to implement and it's usually the go-to option if you're just trying to spin up something quickly that interacts with the spotify api now with that said spotify also provide a link for each one of these authorization flow so if you click onto it it's on the same page but essentially it's a section uh that covers the uh set authorization flow in much more detail and so it's quite handy because again if you're building an application that makes use one of one of these then you're going to need to actually dive into the detail here now the last thing i want to run you through is this notion of rate limit so i'm going to do that by clicking the search button and by the way whenever you work with apis usually um you might want to just search for things it's just easier and it helps you get to where you want to quicker so um i'll probably click on the first link and then if i search rate limit on this page yeah fab so here we've got rate limiting right and rate limiting is essentially a way for apis to be able to restrict how many requests you make to them so um why would you want to do this well of course spotify has a bunch of web servers and systems running right and whenever you make a request to the uh to the to one of these uh or the spotify api it's having to um you know handle that on their end right and what might happen as you can imagine is if spotify is dealing with an incredible number of api requests in like you know in in 10 seconds maybe they're getting hundreds of thousands that can potentially take the app down right so spotify a spotify api as well as other apis need a way of protecting themselves in other words protecting themselves from a humongous amount of traffic in a short space of time that could potentially bring the app down now the way they implement that is with weight limiting and essentially this is being restrictive on the number of requests that you can make now in this case what if i don't actually tell you how many requests you're allowed to make per second and i think they've purposely left that out because they just want you to leave it up to intuition but specifically they want you to be able to handle a case where you're getting weight limited so they also want you to write good code um that handles that case and they give you a bit more details of how you can go about doing that in this case it will return a 429 status code saying you've made too much too many requests and they tell you to check the retry after header in the response and then you see a number displayed which corresponds to the number of seconds that you actually need to you know back off for a bit before you make the next request so they you know they detail other um or that out quite nicely for you funny story actually back in the day i say back in the day it's still used but ddos attacks were actually one way that you could actually bring the website down so the idea is that you know you get a bunch of people together i think what people used to do is um when they used to coordinate ddos attacks which is just basically flooding a server with requests um what they do is they get you know a thousand people all at the same time to run a particular script that just sends out requests every second or however many quests every second you get a thousand people to do that that's like hundreds of thousands of requests potentially and that would be enough to take websites down and ddos attacks are actually quite or they're still common today of course a lot of um applications like you know spotify or big organizations like spotify and facebook are much more sophisticated and they're much better at handling that traffic but smaller websites are probably not so uh yeah it's just something uh something uh worth knowing about ddos it's quite still quite popular in the uh ethical hacking world right so let's now walk through example of creating a playlist so the first thing you want to do is click onto console and the console essentially is very similar to the reference in that on the left hand side you can see uh what you saw before which was playlist you know episodes and if i click onto playlists and i click on to create a playlist you notice here you can actually you've got fields that are editable and this is why or this is the reason why the console is actually great what this allows you to do is it allows you to run a request or make a request to one of these endpoints in this case creating a playlist with parameters that you define so here you can give the user id and here you can actually give the request body in this case the name the description in public so it's pretty cool it's actually really handy um especially if you don't want to write any code and you just want to test what kind of data a particular endpoint will give back once you give the parameters now the more important thing is if you come down to this section here you have this oauth token you can see one's already been generated for me but this is key right because you know that authorization flow which gives you access to a token that's short-lived and it can't be refreshed and it only lasts for a certain period of time i think this one a couple of hours or maybe just an hour in this case you want to click get token and because we want to create a playlist with the spotify api we want to use let's just click both because it's fine it kind of tells you these are the required scopes for this endpoint anyway and then we click request token it'll give me a fresh token so in this case i can copy this and then we'll head over to our script so i'll open up sublime and now what i'll do is i'll copy into this field or this variable called access token and what we're going to do is we're going to walk through this script that creates a playlist on spotify so walking through this script the first thing is you notice we only import one library and that's a request library this is the library that we're going to use to make a request to the spotify api you notice i've got two constants here one is the playlist url or creating the playlist url and that's one i copied from the docs and remember how i mentioned that you need to replace the user id in this case i've put my user id here so that's good and i've also created another constant with um the access token which i just pasted in now the script is fairly simple if i actually scroll down to the bottom um this is where essentially the application is going to start so as soon as i run i'm going to call a method called create playlist on spotify and we'll get onto that in a second i'm going to give it a name and call it my private playlist and notice here i've set public equal to false so that it's going to be a private playlist and then we'll put the result in a variable called playlist and then we'll just print it out and then if i scroll up above to this create playlist on spotify function it again it's quite simple here i'm making a request out to this uh endpoint um and that's the spotify create playlist url and here is where um you pass in the authorization details so as you can tell we've got headers and this is how you pass the authorization header so the key is authorization and then the value is bearer with a space followed by the access token and i've used an f string in python to make that all nice or to make that look better then otherwise you'd usually have to use format and it just adds extra code so that essentially is how you pass in the authorization and you pass in the access token within that authorization header and then the last bit is the actual json so again i mentioned that this endpoint requires all the body that requires is the name that's the one that's required and the others are optional but i'm actually providing the public one because i want to make this a private playlist so public is a variable we're going to pass in again we've set it to false here so this that's going to make it a private endpoint or sorry a private playlist and so that's the json quite simple and then what you do is uh this will make out the request it's going to store in the response variable and then to actually get the response in a json format in other words in the dictionary you call the dot json method on that response and then of course i'm putting the result in here and then i'm returning that back and then of course that will return back to this and it'll be stored in the playlist variable so hopefully that makes sense i would have loved to take the time to make a tutorial on doing this step by step do let me know if that's something you want to see in the comments below and also do let me know if there's any particular uh things that you want to do with the spotify api and make maybe you want me to make a tutorial on one of those things feel free to let me know in the comments below but let's give this script a run right so i'm in my uh directory and here we have the file so i'm gonna type python and this was create unschool playlist dot py and if i tap enter as you notice it basically printed out the playlist now i've made this font bigger essentially he can see the playlist and you can see it's a dictionary and it's got a bunch of values and the most important thing is uh the name as you can tell we it's not quite clear but the names here and uh what i'll do is i'll open up spotify and we'll actually look for this playlist right so as you can tell i've got a lot of playlists on spotify but the most important thing is the most recently created playlist will be at the top and here you can see my playlist or my private playlist has been created and of course it's empty and you can check that it's actually private by clicking the more button and then here you can see the option to make it public which obviously means that it's private so it created the playlist and that looks good i'm sure you learned a ton so make sure you smash that like button as well as tap the subscribe and bell icon if you want to see more videos like this thanks again for whooping 4000 subscribers and until next time have a blast of the day and i'll see you in the next video peace
Info
Channel: Imdad Codes
Views: 14,842
Rating: 4.9705162 out of 5
Keywords: getting started with spotify api, getting started with sptfy api, spotify api, spotify api authorization, spotify api python, spotify api javascript, spotify api react, spotify api flutter, spotify api android studio, how to use spotify api, spotify api tutorial python, python spotify api tutorial, automate spotify, spotify api python example, spotify api example, spotify api oauth, spotify api access token, learn spotify api, spotify api beginner, spotify web api, spotify
Id: c5sWvP9h3s8
Channel Id: undefined
Length: 24min 2sec (1442 seconds)
Published: Sat Jan 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.