Introduction

Spotify is a digital media company that specializes in bringing music, podcast, and videos to its millions of users across the world. Spotify offers access to millions of songs from millions of artists for free. Spotify also has its premium service that allows users to enjoy songs without the interruption of ads. It it my intention to demonstrate how to use the Spotify API so users can use their data in a more meaningful way by viewing their most played songs, artists, or genres.

Setup

To setup the Spotify api you must first download these packages:

install.packages(‘spotifyr’)

library(spotifyr)

Step 1

The first step requires the user to create a Spotify dev account the following link: https://developer.spotify.com/

Step 2

The second step requires the user to create an app. Using the previous link the user will click on “create app” on the top right of the page. The user will enter app name, app description, website url, redirect url.

Step 3

After the app is created, the third step involves finding the client id and secret client id. The user will navigate the settings section and will find both ids under the basic information tab.

Final Step

The final step involves retrieving the access token, which allows the user to gain access to Spotify data.To gain access to this data run the following code:

Sys.setenv(spotify_client_id = ‘xxxxxxxxxxxxxxxxxxxxx’)

Sys.setenv(spotify_client_secret = ‘xxxxxxxxxxxxxxxxxxxx’)

access_token <- get_spotify_access_token()

Demonstration

Once the user has retrieved their access token they can writes calls such as the following to retrieve data from their Spotify account:

get_my_top_artists_or_tracks(type = ‘tracks’, time_range = ‘short_term’, limit = 5) %>% mutate(artist.name = map_chr(artists, function(x) x$name[1])) %>% select(name, artist.name, album.name) %>% kable()

This call returns information about the users favorite tracks at the moment. Using these calls avaliable within the Spotify API users can use their data in more meaningful ways.