What is Spotify API and how to quickly set it up

Spotify has a pretty extensive API and its website has a ton of data to analyze and play around with. It’s very unique and allows you to quantify music in a way for you to learn more about your own listening behaviors or analyze certain variables such as the “danceability” of a song.

First and foremost, we will need to install the devtools package to get spotify to work. This is because spotify was removed from CRAN for compliance issues. Just paste and run this code into R as such to quickly get started:

install.packages("devtools")
devtools::install_github('charlie86/spotifyr')
install.packages('spotifyr')    
library(spotifyr)

You will then need to set up a developer account to use the Spotify API. Setting this up allows you to access your Client ID and Client Secret and then get your access token into R. You can click here to set up your developer account and get the required id’s.

After obtaining your Client ID and Client Secret, add the following lines to RStudio as follows:

id <- ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxx’
secret <- ‘xxxxxxxxxxxxxxxxxxxxxxxx’
Sys.setenv(SPOTIFY_CLIENT_ID = id)
Sys.setenv(SPOTIFY_CLIENT_SECRET = secret)
access_token <- get_spotify_access_token()

Now you are ready to begin using spotifyr and start analyzing!

Examples of the Different Types of Functions within spotifyr

Name Description
get_artist_audio_features Get features and popularity for an artist’s entire discography on Spotify
parse_playlist_list_to_df Parse Spotify playlist list to a dataframe
get_user_playlists Get user playlists
get_user_playlist_count Get count of Spotify playlists for a given user
get_user_audio_features Get features and popularity for all of a user’s playlists on Spotify
get_artists Get Artists
get_artist_audio_features Get features and popularity for an artist’s entire discography on Spotify
get_album_popularity Get Album Popularity
get_albums Get Albums

What is the most words Frank Ocean uses in a song?

“Speechiness” refers to the presence of spoken words in a song according to Spotify. This is one of many unique variables that can be used to analyze from your favorite artist or band. Here is a quick example of an analysis of Frank Ocean’s speechiness across all of his songs:

track_name speechiness
Facebook Story 0.685
Be Yourself 0.541
Futura Free 0.508
Good Guy 0.467
Not Just Money 0.444
Not Just Money 0.427
Pilot Jones 0.374
Pilot Jones 0.248

How to get your spotify playlist in a dataframe

Here is a way for you to put any given spotify playlist into it’s own dataframe. The only thing you need is the “Spotify URI” which is found on any public playlist. For this example, I used my own driving playlist and put it in it’s own dataframe like so:

driving_playlist <- get_playlist_audio_features(playlist_uris = '5tOE6jaKCZrlVbf4A5aQ6s')

Then spotify spits this into a dataframe with so many different metrics that could be played around with as shown below! You can definitely run some visualizations to see correlations between variables or even compare playlists with each other.

danceability energy key loudness mode speechiness acousticness instrumentalness liveness valence tempo
0.700 0.4600 4 -8.111 0 0.0661 0.1420 4.81e-05 0.111 0.2860 151.901
0.272 0.2380 2 -12.148 0 0.0349 0.8900 9.25e-03 0.275 0.1190 83.507
0.577 0.3870 7 -8.607 1 0.2740 0.9640 7.05e-01 0.208 0.4590 146.565
0.665 0.5350 4 -7.056 0 0.0572 0.0786 1.68e-05 0.154 0.3700 138.842
0.505 0.2670 9 -13.763 0 0.0500 0.2790 0.00e+00 0.096 0.4390 143.929
0.391 0.4150 0 -11.665 1 0.0398 0.5730 1.88e-03 0.242 0.0789 99.035
0.593 0.3700 11 -11.674 0 0.3970 0.8320 0.00e+00 0.108 0.3120 135.391
0.466 0.5480 5 -9.362 0 0.1180 0.4200 1.00e-06 0.113 0.4230 89.815
0.402 0.5260 9 -6.741 0 0.0904 0.1130 8.30e-06 0.105 0.3120 81.404
0.585 0.5970 0 -7.499 1 0.0370 0.3390 4.04e-02 0.195 0.2270 100.051
0.421 0.0952 6 -12.561 1 0.0479 0.9310 2.01e-04 0.126 0.0773 109.698

This was just a quick introduction for Spotify API and I hope you can see the potential for what you can do with music data!