@ottic Did you ever get this working? I tried this workflow you published, but it seems to just delete everything in the playlist instead of removing the duplicates. I think it’s because using the delete-track-by-uri call results in the deletion of every track with that uri in the playlist.
One approach to fix that is to completely replace the playlist with the deduplicated tracks using the PUT method (docs). AFAICT n8n doesn’t support this endpoint yet, but you can work around that by calling the Spotify API directly with an HTTP Request node. You’ll need to set up plain Oauth2 Api credentials using the info from your Spotify dev portal app.
Authorization URL: Anmelden - Spotify
Access Token URL: https://accounts.spotify.com/api/token
Scope: playlist-modify-public playlist-modify-private playlist-read-private playlist-read-collaborative
Authentication: Header
It should look something like this:
I’ve got more scopes in my setup, because I use this for other workflows too.
And then for the actual nodes, try this workflow:
{
"nodes": [
{
"parameters": {
"authentication": "oAuth2",
"requestMethod": "PUT",
"url": "https://api.spotify.com/v1/playlists/REPLACEME/tracks",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "uris",
"value": "={{$json.uris}}"
}
]
}
},
"name": "Replace playlist with deduplicated tracks",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
850,
300
],
"credentials": {
"oAuth2Api": "spotify oauth"
}
},
{
"parameters": {
"resource": "playlist",
"operation": "getTracks",
"id": "spotify:playlist:REPLACEME",
"returnAll": true
},
"name": "Get playlist to deduplicate",
"type": "n8n-nodes-base.spotify",
"position": [
450,
300
],
"typeVersion": 1,
"executeOnce": true,
"credentials": {
"spotifyOAuth2Api": "Spotify"
}
},
{
"parameters": {
"functionCode": "if (items.length == 1 && Object.keys(items[0].json).length == 0) {\n return [];\n}\n\nconst seen = new Set();\nitems.forEach(i => seen.add(i.json.track.uri));\n\nreturn [{json: {uris: [...seen]}}];\n"
},
"name": "Extract unique track URIs",
"type": "n8n-nodes-base.function",
"position": [
650,
300
],
"typeVersion": 1,
"alwaysOutputData": true
}
],
"connections": {
"Get playlist to deduplicate": {
"main": [
[
{
"node": "Extract unique track URIs",
"type": "main",
"index": 0
}
]
]
},
"Extract unique track URIs": {
"main": [
[
{
"node": "Replace playlist with deduplicated tracks",
"type": "main",
"index": 0
}
]
]
}
}
}
Just copy and paste that into a workflow. Replace “REPLACEME” with your playlist ID.
This worked for me. If it works for you, please update your published workflow with this, so others trying it don’t have their playlists wiped by accident.