library(spotifyr)
library(httpuv)Spotify API
Spotify API and its Uses
Spotify is an extremely large database that holds millions of songs, albums, and artists. Holding all this data in a singular dataset would not only be extremely difficult, but immpractical as well. In order to make this data more accessible to the public Spotify uses an API that allows people to call for certain pieces of information that they are looking for. It can be used for looking at one’s personal music listening habits, finding all the songs an artist has put on spotify, checking to see how a certain song has preformed, and much more.
Setting Up the Spotify API
To set up a Spotfiy API the first step is to look up Spotify API. Go to the first link that pops up and hit login. Log in to your Spotify account and then go to the dashboard. In the dashboard, click the create app option. Name the app whatever you want it to be called and give it a brief description. Check the option for Web API when asked for which you are planning on using. From there, find the settings tab and click on it to access your ID number. There you will find your Client ID and Client secret.
Setting Up the API in R
Load in the spotifyr package to give you the tools necessary fro useing the API.
Put both your Client ID and Client secret into R to get your access token, which will allow you to access the API.
auth <- get_spotify_authorization_code(client_id = Sys.getenv("SPOTIFY_CLIENT_ID"),
client_secret = Sys.getenv("SPOTIFY_CLIENT_SECRET"),
scope = scopes()[c(7,8,14,15)])Now you’re ready to make calls from the API. For example, you can pull all the albums an artist has put on Spotify. In this example I pulled all of Drake’s albums. To do this you must find the Artist ID. This can be found by going to the artist’s Spotify page and clicking the copy link to artist button. This will give you a url for that spotify page. The artist ID can be found in url between the /artist/ and ?. With this command you can look at all the albums they have put out with information on what they’re called, when they were released, and how many tracks are on it.
drake <- get_artist_albums("3TVXtAsR1Inumwj472S9r4")