Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Add Tracks To Playlist API Error

Add Tracks To Playlist API Error

Hey guys, I've been working on a project, and within it, I have to add tracks to a freshly created playlist. When I do it, I get a 201 response and a snapshot ID, indicating a successful request. But when I check the actual playlist, nothing has been added to the track. I can't get it to work in Javascript or Python. 

 

Does anyone know what the cause might be? I made sure my URIs are correctly formatted, and I'm authenticated with the proper scope. I have provided a piece of the code below if it helps.

 

 

function addTracksToPlaylist(playlistId, trackUris, token) {
//use a batchSize of 90 to avoid errors with too long strings
const batchSize = 90;
const url = `https://api.spotify.com/v1/playlists/${playlistId}/tracks`;

// Split trackUris into batches of batchSize
for (let i = 0; i < trackUris.length; i += batchSize) {
const batch = trackUris.slice(i, i + batchSize);

// Make the POST request using fetch for each batch
fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ uris: batch })
})
.then(response => response.json())
.then(result => {
if (result.snapshot_id) {
console.log('Batch added successfully:', result);
} else {
console.error('Error adding batch:', result);
}
})
.catch(error => console.error('Error:', error));
}
}

 

Reply
3 Replies

Same with me

Dunno why but the IDs i got were invalid, fetching the track info gives 404 not found.

https://developer.spotify.com/documentation/web-api/reference/get-track

Therefore they were not added. Unfortunate to not have feedback.

Hi,

 

Like a_fullrandom_guy identified, if you are sending correctly formatted URIs, but they do not correspond with an actual item, you will not get any errors (we only verify the format, not if they are part of the catalog). 

If you send a list of two items, but only one with an URI that corresponds to an item, only one item will be added to the playlist, eg. spotify:track:11dFghVXANMlKmJXsNCbNl,spotify:track:10dFghVXANMlKmJXsNCbNl

So most likely you are not providing URIs that correspond to items vaibhavs1. 

Suggested posts