Learn R Programming

tinyspotifyr

Overview

tinyspotifyr is an R wrapper for the Spotify’s Web API. It is a fork of spotifyr by Charlie Thompson, since revived at its current location, with minimal dependencies inspired by the tinyverse. The focus of this package is to mirror the spotify api in R.

Installation

R version 3.2.0 (recommended)

install.packages('tinyspotifyr')

Development version

devtools::install_github('troyhernandez/tinyspotifyr')

Authentication

First, set up a Dev account with Spotify to access their Web API here. This will give you your Client ID and Client Secret. Once you have those, you can pull your access token into R with get_spotify_access_token().

The easiest way to authenticate is to set your credentials to the System Environment variables SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET. The default arguments to get_spotify_access_token() (and all other functions in this package) will refer to those. Alternatively, you can set them manually and make sure to explicitly refer to your access token in each subsequent function call.

Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')

access_token <- get_spotify_access_token()

Authorization code flow

For certain functions and applications, you’ll need to log in as a Spotify user. To do this, your Spotify Developer application needs to have a callback url. You can set this to whatever you want that will work with your application, but a good default option is http://localhost:1410/ (see image below). For more information on authorization, visit the offical Spotify Developer Guide.

Usage

Create a Daily Radio playlist

library(tinyspotifyr)
playlist_name <- "Daily Radio"

Get your playlists

my_playlists <- get_my_playlists(limit = 50)
Create a new playlist

Find yesterday’s Daily Radio playlist or create a new, empty playlist.

playlist_logical <- (my_playlists$name == playlist_name)
if(sum(playlist_logical) > 0){
  ind <- which(playlist_logical)
  dr <- my_playlists[ind, ]
} else {
  dr <- create_playlist("TroyHernandez", playlist_name, public = FALSE)
}

Add songs to playlist

I use my Discover Weekly playlist as a base and overwrite my existing “Daily Radio” tracks. Using reorder_replace_playlist_items is more robust for playlists than other options.

discover_weekly <- my_playlists[which(my_playlists$name == "Discover Weekly"),]
dw_tracks <- get_playlist_tracks(discover_weekly$id)
dw_uri <- dw_tracks$track.uri
reorder_replace_playlist_items(playlist_id = dr$id, uris = dw_uri)

Add podcasts to your playlist

I listen to 4 songs between each podcast. NPR updates every hour, but sometimes it’s empty and returns an error. Notice the zero indexing.

# Add NPR
try(add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:6BRSvIBNQnB68GuoXJRCnQ", position = 0), silent = TRUE)
# Add WBEZ
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:1x1n9iWJLYNXYdDgLk5yQu", position = 1)
# CBS
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:2pLChHUBuwElfAplwVGTdF", position = 6)
# JRE Clips
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:1LMmQF9PH8LjYrktU0Oq5Y", position = 7)
# Chicago Tribune
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:3K1ffPI9ynW3mO24A5rfbF", position = 12)
# Marketplace
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:6zYlX5UGEPmNCWacYUJQGD", position = 13)
# Crains
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:20Ut1ENH9nTy4LqWF9p8vq", position = 18)
# Planet Money
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:4FYpq3lSeQMAhqNI81O0Cn", position = 23)
# WSJ Tech
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:51MrXc7hJQBE2WJf2g4aWN", position = 28)
# Useful idiots
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:5BpYXlVorOw5FZ9pfpu7ff", position = 33)
# Chicago Tonight to the end of the podcast
add_latest_to_playlist(playlist_id = dr$id, uri = "spotify:show:2WuB3zkmXGo7sJUZ6GQIx3")

I run this as a cron job every morning with some extra tweaks.

Copy Link

Version

Install

install.packages('tinyspotifyr')

Monthly Downloads

169

Version

0.2.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Troy Hernandez

Last Published

June 7th, 2026

Functions in tinyspotifyr (0.2.3)

get_artists

Get Spotify catalog information for multiple artists identified by their Spotify IDs.
get_artist_albums

Get Spotify catalog information for multiple artists identified by their Spotify IDs.
get_my_devices

Get information about a user’s available devices.
get_albums

Get Spotify catalog information for multiple albums identified by their Spotify IDs.
get_album_tracks

Get Spotify catalog information about an album’s tracks. Optional parameters can be used to limit the number of tracks returned.
get_my_currently_playing

Get the object currently being played on the user’s Spotify account.
get_my_recently_played

Get Current User's Recently Played Tracks
get_my_profile

Get detailed profile information about the current user (including the current user’s username).
get_category

Get a single category used to tag items in Spotify (on, for example, the Spotify player’s “Browse” tab).
get_my_followed_artists

Get the current user’s followed artists.
get_my_playlists

Get a list of the playlists owned or followed by the current Spotify user.
get_playlist_cover_image

Get the current image associated with a specific playlist.
get_playlist

Get a playlist owned by a Spotify user.
get_my_current_playback

Get information about the user’s current playback state, including track, track progress, and active device.
get_new_releases

Get a list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab).
get_my_top_artists_or_tracks

Get the current user’s top artists or tracks based on calculated affinity.
get_latest_episode

Get Spotify uri information for a show's latest episodes identified by their unique Spotify ID.
get_featured_playlists

Get a list of Spotify featured playlists (shown, for example, on a Spotify player’s ‘Browse’ tab).
get_my_saved_albums

Get Current User's Saved Albums
get_category_playlists

Get a list of Spotify playlists tagged with a particular category.
get_related_artists

Get Spotify catalog information about artists similar to a given artist. Similarity is based on analysis of the Spotify community’s listening history.
get_track_audio_analysis

Get a detailed audio analysis for a single track identified by its unique Spotify ID.
get_track_audio_features

Get audio feature information for a single track identified by its unique Spotify ID.
is_uri

Check if a string matches the pattern of a Spotify URI
get_user_profile

Get public profile information about a Spotify user.
get_recommendations

Create a playlist-style listening experience based on seed artists, tracks and genres.
get_playlist_items

Get full details of the items of a playlist owned by a Spotify user.
get_my_saved_tracks

Get a User's Saved Tracks
set_my_volume

Set the volume for the user’s current playback device.
skip_my_playback

Skips to next track in the user’s queue.
get_spotify_authorization_code

Get Spotify authorization Code
get_playlist_tracks

Get full details of the tracks of a playlist owned by a Spotify user.
get_track

Get Spotify catalog information for a single track identified by its unique Spotify ID.
get_show

Get Spotify catalog information for a single show identified by their unique Spotify ID.
seek_to_position

Seeks to the given position in the user’s currently playing track.
toggle_my_shuffle

Toggle shuffle on or off for user’s playback.
tinyspotifyr-package

tools:::Rd_package_title("tinyspotifyr")
set_my_repeat_mode

Set the repeat mode for the user’s playback. Options are repeat-track, repeat-context, and off.
get_shows

Get Spotify catalog information for a single show identified by their unique Spotify ID.
get_spotify_access_token

Get Spotify Access Token
pitch_class_lookup

Pitch class notation lookup
unfollow_playlist

Remove the current user as a follower of a playlist.
search_spotify

Search for an item
get_shows_episodes

Get Spotify catalog information for a show's episodes identified by their unique Spotify ID.
verify_result

Verify API result
scopes

Valid scopes
transfer_my_playback

Transfer playback to a new device and determine if it should start playing.
pause_my_playback

Pause playback on the user’s account.
get_tracks

Get Spotify catalog information for a single track identified by its unique Spotify ID.
reorder_replace_playlist_items

Reorder or replace one or more items from a user’s playlist.
get_user_playlists

Get a list of the playlists owned or followed by a Spotify user.
remove_tracks_from_playlist

Remove one or more tracks from a user’s playlist.
skip_my_playback_previous

Skips to previous track in the user’s queue.
start_my_playback

Skips to previous track in the user’s queue.
add_tracks_to_playlist

Add one or more tracks to a user’s playlist.
follow_artists_or_users

Add the current user as a follower of one or more artists or other Spotify users.
check_me_following

Check if Current User Follows Artists or Users
add_latest_to_playlist

Add the latest episode of a podcast to a user’s playlist.
check_users_following

Check if Users Follow a Playlist
create_playlist

Create a playlist for a Spotify user. (The playlist will be empty until you add tracks.)
add_items_to_playlist

Add one or more tracks to a user’s playlist.
change_playlist_details

Change a playlist’s name and public/private state. (The user must, of course, own the playlist.)
follow_playlist

Add the current user as a follower of a playlist.
get_album

Get Spotify catalog information for a single album.
get_artist_top_tracks

Get Spotify catalog information about an artist’s top tracks by country.
get_categories

Get a list of Spotify categories
get_artist

Get Spotify catalog information for a single artist identified by their unique Spotify ID.