Nick Knauer Nick Knauer - 1 year ago 106
R Question

Accessing Spotify API for Multiple Artists in R

I have created a Client ID and Secret Key in Spotify's developer app section. I am referencing this document and want to extend this by choosing multiple artists.

In this example, they are only choosing 1 artist ID but there are some holes to this,

1) How do you obtain the artist name rather than the artist ID because I can't find a glossary key anywhere for the artist ID?

2) How do you choose multiple artists rather than just one.

So for example, how do I choose Frank Sinatra and Elvis Presley?

Code is below:


clientID = '#############################'
secret = '########################'

response = POST(
authenticate(clientID, secret),
body = list(grant_type = 'client_credentials'),
encode = 'form',

mytoken = content(response)$access_token
## Frank Sinatra spotify artist ID
artistID = '1Mxqyy3pSjf8kZZL4QVxS0'

HeaderValue = paste0('Bearer ', mytoken)

URI = paste0('', artistID)
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)

My goal here is to get all of the attributes about each of the artists songs such as danceability, etc.

Any help would be great, thanks!

Answer Source

If you're asking how get get the artist name from the response object you created in your question, you can do so like this:

> Artist$name

"Frank Sinatra"

If you're asking how to query the API by artist name, the relevant endpoint is the artist search by name, which will allow you do find the ID (as the API is based on ID other than the search feature, so you'll need that):


The endpoint to get several artists is:


They actually provide a nice jsFiddle example of a (single) artist search based on name:

This search documentation page shows how they build the search URI:

Putting it all together

Here's a fully working example of searching based on artist name in R. Let's say we want to search for YTCracker, the nerd rap king:

artistName = 'ytcracker'

HeaderValue = paste0('Bearer ', mytoken)

URI = paste0('', artistName,'&offset=0&limit=20&type=artist')
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)

The Artist object has the ID, URI, genre, number of followers, and other information sufficient to explore the rest of the Spotify API for this artist, their albums, and related artists, etc, etc:

[1] ""

[1] ""


[1] 2386

[1] "nerdcore"

[1] ""

[1] "1x82Mu3wakMkldMW5kEiP4"

[1] 640

[1] ""

[1] 640

[1] 300

[1] ""

[1] 300

[1] 64

[1] ""

[1] 64

[1] "YTCracker"

[1] 31

[1] "artist"

[1] "spotify:artist:1x82Mu3wakMkldMW5kEiP4"

[1] ""


[1] 36


[1] ""

[1] "2RnAr4QItmKW4vFKip3m8v"


[1] "YTCracker & Hairetsu"

[1] 9

[1] "artist"

[1] "spotify:artist:2RnAr4QItmKW4vFKip3m8v"

[1] 20


[1] 0


[1] 2

Note that I set an optional limit on the size of the search results. Also note that you can use the same search results to query things like album art images, as in the jsFiddle example, if so inclined.

Multiple Arists

OK, so now I have the ID for YTCracker based on my search and the ID for Sinatra from your question, and I want to simultaneously query the API for both artists. I can do that as follows. As long as you have the ID's you can to this for more than 2 artists, as well.

URI = paste0('', Artist$artists$items[[2]]$id,",", '1Mxqyy3pSjf8kZZL4QVxS0')
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artists = content(response2)
[1] ""


[1] 36


[1] ""

[1] "2RnAr4QItmKW4vFKip3m8v"


[1] "YTCracker & Hairetsu"

[1] 9

[1] "artist"

[1] "spotify:artist:2RnAr4QItmKW4vFKip3m8v"

[1] ""


[1] 1204440

[1] "adult standards"

[1] "brill building pop"

[1] "christmas"

[1] "easy listening"

[1] "jazz christmas"

[1] "lounge"

[1] "mellow gold"

[1] "soft rock"

[1] "soul"

[1] "swing"

[1] "vocal jazz"

[1] ""

[1] "1Mxqyy3pSjf8kZZL4QVxS0"

[1] 650

[1] ""

[1] 999

[1] 416

[1] ""

[1] 640

[1] 130

[1] ""

[1] 200

[1] 42

[1] ""

[1] 64

[1] "Frank Sinatra"

[1] 77

[1] "artist"

[1] "spotify:artist:1Mxqyy3pSjf8kZZL4QVxS0"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download