Introduction
Spotify is a streaming music service founded in 2008, and is available on any and all devices. According to BusinessOfApps statistics, Spotify is the largest music application in the world as of February 2023 in terms of number of subscribers. What this means to me is that Spotify is swimming in data that can be analyzed and investigated for any number of possibilities. Spotify leverages a lot of this data, most popularly with their Spotify Wrapped, which shows users their listening statistics every year at the end of the calendar year, including a slew of information regarding listening habits, genres, favorite artists etc. What the plebeians of non RStudio users don’t know, and what I will discuss in this document, is that Spotify has a publicly available API that can be connected to through RStudio in order to analyze their host of data, and even make your own Spotify Wrapped! The content of this document will be a rudementary introduction into accessing and utilizing an API, coming from a college student in an RStudio Analytics Programming class, so bare with me! :)
Connecting to the API from the Spotify Side
Before loading into R, the first step is create a Dev account from the Spotify for Developers webpage, located here.
Spotify has their own walkthrough on setting up a Web API, which can be found here.
Log into your Spotify user account through the login button in the
top right corner (if you do not have a Spotify user account you can do
so on their main webpage open.spotify.com
using the Sign Up page in the top right). Under your profile
header, click on dashboard, and create application. Name and describe
your application however your heart desires. The website can be a domain
that you own or host, however this field is optional so feel free to
skip it for now. Spotify needs to have a callback URL for certain
features, and you can set this to whatever you want that will work with
your application, but a good default option is
http://localhost:1410/. Check the terms of service box and
create your application! After doing so, navigate to the settings of
your app in order to find you Client ID and Client Secret, both of these
will be mandatory in creating your Spotify Access Token!
Connecting to the API from the R Side
Thankfully, the RStudio side of this API connection is much simpler.
The most important step in accessing the connection is setting up your
access token, which is fairly easy. First and foremost, you’re going to
need to install the Spotifyr package using
install.packages('spotifyr'). Afterwords, use
library(spotifyr) to call this installed package into the
script. To access your unique API that you just set up, the Spotifyr
package has the aptly named get_spotify_access_token
command, which uses 2 arguments. The first being your Client
ID, and the second being your Client Secret. To establish
these two arguments in your System Environment and set up the access
token, use the following code chunk:
# Commented out to knit properly.
# In your script, delete comments and include your personal values
# Sys.setenv("SPOTIFY_CLIENT_ID" = 'XXXXXXXXXXXXXXXXXXXXXXX')
# Sys.setenv("SPOTIFY_CLIENT_SECRET" = 'XXXXXXXXXXXXXXXXXXXXXX')
# access_token <- get_spotify_access_token()Congratulations, you are now connected to the Spotify API through both the Spotify and the RStudio side.
Utilizing the API connection
There are many different utilizations of this API - for some more detailed analysis than will be conducted in this document, please visit the ‘Other Cool Stuff’ portion at the very bottom of the page.
Note: the first request that you run will redirect you to your browser and require you to allow Spotify access your date for authentication purposes - this will also allow Spotify to have access to your personal data in order to fulfill personalized requests.
The API request I’ve made is to see my top artists of all time. The following request to Spotify’s API is personalized for each user, meaning the output would be your own top artists of all time. I have commented out the code, since running this would require my own Client ID and Client Secret somewhere in the document, which I would rather not include.
# get_my_top_artists_or_tracks(type = 'artists', time_range = 'long_term', limit = 10) %>%
# select(name, genre, popularity)The raw output includes a lot of statistics that aren’t very adept to being analyzed, for example there is a unique ID which serves as a primary key for each artist or song. For this reason, I have piped the Spotifyr output into a select statement to only get the information I actually want to see: in this case, I have requested the artist name, genres, and popularity. I found the popularity variable quite interesting, and I was curious on how Spotify rates their popularity. A few Google searches later, and it turns out that popularity is calculated by a Spotify algorithm that is based largely on how many times a track (or in this case an artist’s track) plays, and how recent these plays are. The algorithm then spits out a quantitative variable from the scale of 0-100, 100 being the most popular. I utilized this variable in this visualization to conduct a subjective analysis of how basic my music taste is, based on the popularity of my top 10 artists. I have an average popularity score of 85.4 out of 100, from which I am concluding that my music taste could do for some diversity.
The Spotify dev WebAPI reference page (found here has a plethora of information on different commands and variables included within the Spotifyr package. There is a lot of helpful commands and examples that they provide to help out with figuring out their API and
Other Cool Stuff
RCharlie has a very detailed process to set up and utilize the Spotifyr package, which you can find here
Mia Smith has an incredibly well done GitHub site which runs through a project she did in college utilizing the Spotifyr package, which is a fascinating read! You can find her GitHub site here
Congratulations on making it to the end of my document! Thank you for taking the time to read this, I hope you learned a little something and enjoyed yourself along the way!!