Announcements

Help Wizard

Step 1

NEXT STEP

Move Specific Track to Top of Playlist by URI

Solved!

Move Specific Track to Top of Playlist by URI

Hello. 

It looks like the PUT playlists/{playlistid}/tracks endpoint (https://developer.spotify.com/documentation/web-api/reference/#/operations/reorder-or-replace-playli...) only lets you move a track if you know its position already, is that right?

If so, does that mean in order to move only knowing a URI, I have to get every track in the playlist (this will take a while my playlist has over 1k songs), find the track with the matching URI, then determine playlist position based on the track's position in the resulting array? Cause as far as I know the GET /playlists/{playlistid}/tracks endpoint doesn't let you filter by URI, and even if it did it doesn't tell you the position in playlist, so I would still need to get every track to determine position.

Thanks. 

Reply

Accepted Solutions
Marked as solution

How about removing the track and readding it at your desired position?


https://developer.spotify.com/console/delete-playlist-tracks/


I have a followup-questin since I have a similar problem. How do you even add tracks to an existing playlist? It seems like I am overwriting my playlist instead of adding new tracks to an existing list.


I sadly cant make sense of how to structure these parameters:
range_start, insert_before, range_length

View solution in original post

4 Replies

bump

Marked as solution

How about removing the track and readding it at your desired position?


https://developer.spotify.com/console/delete-playlist-tracks/


I have a followup-questin since I have a similar problem. How do you even add tracks to an existing playlist? It seems like I am overwriting my playlist instead of adding new tracks to an existing list.


I sadly cant make sense of how to structure these parameters:
range_start, insert_before, range_length

Thanks for the idea, it ended up working. Who even knows if PUT playlists/{playlistid}/tracks would even work with URI, and who cares. This works. You said you were struggling with a similar problem, so here's a snippet of working code to move a URI to top of playlist: 

import asyncRequest from "request-promise";

let spotifyAccessToken = "your spotify access token";
let spotifyPlaylistID = "your spotify playlist id";

async function moveTrackToTop(uriToMove) {

    await asyncRequest.delete({
        url: `https://api.spotify.com/v1/playlists/${spotifyPlaylistID}/tracks`,
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": `Bearer ${spotifyAccessToken}`
        },
        body: JSON.stringify({
            tracks: [{ uri: uriToMove }]
        })
    })

    await asyncRequest.post({
        url: `https://api.spotify.com/v1/playlists/${spotifyPlaylistID}/tracks?position=0&uris=${uriToMove}`,
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": `Bearer ${spotifyAccessToken}`
        },
        body: JSON.stringify({
            tracks: [{ uri: uriToMove }]
        })
    })

}

moveTrackToTop("spotify:track:3mxMrdo3fJjDbb64nagoXR");

Thanks man, I will try it out!

Suggested posts