How to fetch artist genre data from Spotify Web API for playlist tracks

I’m working on a project to export Spotify playlist data to CSV format and need help with JavaScript. I want to include genre information for each track but I’m running into some issues.

From what I understand, getting the genre requires:

  • Extracting genre info from the artist data
  • The basic playlist endpoint only gives limited artist details with just the ID
  • Making additional API calls to get full artist information including genres

I can successfully get artist IDs using this approach:

playlistData.items.forEach(function(song) {
  var artistIds = song.track.artists.map(function(performer) {
    return performer.id;
  }).join(', ');
});

My current data processing looks like this:

var songList = apiResponses.map(function(resp) {
  return resp.items.map(function(song) {
    return [
      song.track.uri,
      song.track.id, 
      song.track.name,
      song.track.artists.map(function(performer) {
        return performer.name;
      }).join(', '),
      song.track.artists.map(function(performer) {
        return performer.id;
      }).join(', '),
      song.track.album.name,
      song.track.disc_number,
      song.track.track_number,
      song.track.duration_ms,
      song.added_by == null ? '' : song.added_by.uri,
      song.added_at
    ];
  });
});

How can I use the artist ID to make another API call and retrieve the genre information to add as an extra column?

You need to handle the async API calls properly to get genre info. First, grab all the unique artist IDs from your playlist data - use a Set to avoid duplicates. Then create a separate function that hits the /v1/artists endpoint in batches of 50 (it supports batch requests). Store all the genre data in a Map with artist ID as the key. When you’re building your CSV, just reference that Map to pull in each artist’s genre info.

call the /artists/{id} endpoint after u get the artist IDs. batch up to 50 IDs per request with comma separation - it’s more efficient. use fetch('https://api.spotify.com/v1/artists/' + artistIds.join(',')) then pull the genres array from each artist object. cache the results since the same artists show up multiple times in playlists.