Introduction: Spotify
Spotify is one of the largest music streaming services with over 400 million listeners. Users on Spotify have access to different artists and albums from all over the world. Spotify hosts a plethora of data, and on top of that, graciously has a publicly available API that can be connected to RStudio and used to explore Spotify’s data. This will allow you to retrieve information from artists, albums, and users’ data from Spotify’s Web API. If you’re curious as to what your taste in music looks like compared to your friends’ tastes, or if you want to look at the top songs of the year on Spotify, then this API will make all your dreams come true!
Installation
First step is to install spotifyr and read it in r:
install.packages('spotifyr')
library(spotifyr)
Step up a Dev account
The next step is to log into Spotify or to create a Spotify account if you do not already have one, click here to get started.
create a Dev account with Spotify to access the Web API (click here).
Once you are logged in or have created an account, the next step is to create an app. To do this, enter an App Name and App Description to anything you want (that is appropriate). Next add a web domain or URL in the Website box. The next step is to enter a Redirect URLs which allows Spotify to callback your app every time the user logs in. This URL can be whatever you want, but the default option is ‘http://localhost:1410/’.
Once you have created an app, it will give you your Client ID and Client Secret ID. This information is required for API authorization to obtain an access token.
Authentication
An Access Token is what is used to access a given resource (such as artists, albums, and tracks) or a user’s data (such as your profile and playlists).
Now that you have created a Dev account and have a Client ID and Client Secret ID, you can pull your access token into R. To gain access, the code in r is below:
Sys.setenv(spotify_client_id ='xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(spotify_client_secret = 'xxxxxxxxxxxxxxxxxxxxx')
access_token <- get_spotify_access_token()
After you run this code, it will prompt you to log into your own Spotify account. This will allow you to access your own Spotify account and look up things such as your top artists, albums, and most listened to songs.
Demonstration
Once your authentication is complete you can start exploring the data. Here I’ll demonstrate how to show your top artists. Below is the code I used to gather my Spotify data:
#get_my_top_artists_or_tracks(type = 'artists', time_range = 'long_term', limit = 10) %>%
#select(name, genres) %>%
#kable() This code would output a table shows my top 10 artists over the past year along with the genres associated with the artist. To limit the output to only 10 artists, specify limit = 10 and to show your top artists of all time, specify time_range = ‘long term.’ In order to only see the artist’s name and their associated genre(s), you enter ‘name’ and ‘genre’ into select and it will output only the name and genres of your top 10 artists.
Congratulations!
Congratulations! You have successfully learned how to use the Spotify API! The world is now your oyster and you are free to explore Spotify’s data until you’re sick of it. Have fun and good luck!