1

I am trying to get the top ten tracks for an artist, but I don't know how to get Spotify id for that particular artist? this is the end point.

GET /v1/artists/{id}/top-tracks

The premise is this, i should be able to type an artist name, and I should get the JSON data for the top ten tracks.

function test() {
  var query = $('#searchArtist').val();
  console.log(query);
  $.ajax({
    url: 'https://api.spotify.com/v1/artists/' + query + '/top-tracks?country=IN',
    success: function(response) {
      console.log(response)
    }
  })
}
$('#search').on('click', test);

Am I supposed to first authorize? Because I searched the document and fetching the top ten tracks data doesn't require authorization.

Also, to let you know. There is no backend environment here, as mentioned in the docs, it is not required for simple data fetch.

jooon
  • 1,931
  • 14
  • 24
relentless-coder
  • 1,478
  • 3
  • 20
  • 39
  • 3
    I guess you need to make two separate requests, one to get the artist id and then one to get the top tracks. – TZHX Jan 10 '17 at 10:52

1 Answers1

4

What TZHX said. You need two seperate requests. Javascript in the browser is not my main environment. I will explain with curl and jq in bash. Hopefully it is clear enough.

$ API_ARTIST_URL=$(curl -s 'https://api.spotify.com/v1/search?q=Daft+Punk&type=artist' | jq -r '.artists.items[0].href')

$ echo $API_ARTIST_URL
https://api.spotify.com/v1/artists/4tZwfgrHOc3mvqYlEYSvVi

$ curl -s "$API_ARTIST_URL/top-tracks?country=US" | jq -r '.tracks[].name'
Get Lucky - Radio Edit
One More Time
Instant Crush
Get Lucky
Lose Yourself to Dance
Around The World
Harder Better Faster Stronger
Doin' it Right
Something About Us
Give Life Back to Music

Another detail. ?country=IN will not yield any results, because Spotify has not launched in India yet.

jooon
  • 1,931
  • 14
  • 24
  • Hey, thanks for posting. I had figured it out, and I am marking it as the best answer so that anybody who is looking for a solution can refer to it. – relentless-coder Jan 13 '17 at 04:34