How can I get more than 100 songs in the queue from playlist in MusicKit JS?

I'm using MusicKit JS v1, and I've noticed that I can't seem to get playlists with more than 100 songs to shuffle and play past the first 100. The limitation is that the API call made with MusicKit JS default fetch (I'm using the tracks relationship in a playlist) is capped at 100, and I get a 400 error code whenever I try to raise the limit. On the other hand, I can't figure out how to just get the next 100, because I can't seem to get the offset parameter to work correctly in MusicKit JS either. Any recommendations?

For reference, something like the below just doesn't work (it only retrieves the first 100 songs or throws an error).

await appleMusicState.musicKit.api.playlist(selectedPlaylist.id, {limit: 200})
.then(playlist => {
                console.log(playlist);
            })

nor does directly trying to setQueue with the playlist ID

            await appleMusicState.musicKit.setQueue({playlist: selectedPlaylist.id});

Replies

Thank you for the question, @Anduru

The API endpoint being called from the MusicKit on the Web V1 method musicKit.api.playlist is for the playlist itself. That endpoint does return a list of tracks as a relationship, but is limited to the first 100 tracks, which is the default limit for resources returned from the API. The reason you’re seeing an error when adding a limit query param is that the endpoint is expecting to return a single resource, the playlist for the ID. See the Get a Library Playlist documentation for more information.

The URL path for this endpoint ends up looking like: /v1/catalog/us/playlists/${playlistId}

In order to retrieve the track information via pagination, you’ll need to use the direct relationship fetch endpoint. See the Fetching Resource Relationships Directly documentation for more information.

This URL path would end up being: /v1/catalog/us/playlists/${playlistId}/tracks

This exact example was also part of our recent WWDC Session: Meet Apple Music API and MusicKit. You can find the relationship fetch example at the 13:00 mark.

Unfortunately in MusicKit on the Web v1 there is not a method for this specific tracks request, which means you would need to make the API request directly, including the applicable headers. See the Apple Music API documentation for more information.

For MusicKit on the Web v3, we no longer have specific methods for different API endpoints, but instead have what we call the 'passthrough' API method, which allows requesting any URL that is supported by Apple Music API, with the appropriate headers handled for you. See MusicKitAPI and the Passthrough API Method for more information on the passthrough API method usage.