14

I'm using Javascript to create a web app with Soundcloud's API for my portfolio. At my current stage I need to be able to create a new set (aka playlist). I was using the sample code from Soundcloud's docs:

SC.connect(function() {
  var tracks = [22448500, 21928809].map(function(id) { return { id: id } });
  SC.post('/playlists', {
    playlist: { title: 'My Playlist', tracks: tracks }
  });
}); 

But I'm getting a 422 error:

Unprocessable Entity - The request looks alright, but one or more of the parameters looks a little screwy. It's possible that you sent data in the wrong format (e.g. an array where we expected a string).

But it does not look like anything's missing.

brooklynsweb
  • 817
  • 3
  • 12
  • 26
  • maybe because 21928809 is not a valid/public track, while 22448500 is ok ? – CapelliC Jul 13 '15 at 07:40
  • you have authed the user in advance? afaik you need to use PUT instead of POST according to the docs. you may wanna have a look in that answer, even if its php: http://stackoverflow.com/questions/29156861/how-to-create-soundcloud-playlist-using-php-wrapper – hwsw Jul 13 '15 at 08:28
  • I did authorize the the user as my first step after initialization. Other portions of my code work, but currently stuck at this point. Will continue digging. – brooklynsweb Jul 14 '15 at 04:55

1 Answers1

6

The call to the SoundCloud API requires a callback function in addition to the playlist title and tracks. Your code should look like this:

SC.connect(function() {
  var tracks = [22448500, 21928809].map(function(id) { return { id: id } });
  SC.post('/playlists', {
    playlist: { title: 'My Playlist', tracks: tracks }, function(response){
      console.log(response)
    }
  });
});

Their example is, unfortunately, wrong.

Cory Ribson
  • 126
  • 4
  • Thanks Cory that did the trick; I knew there was something off in their docs. Also I figured out the source of the 422 I was getting: I had a typo and left out the "/" from the endpoint path 'playlist,' thus it would resolve to "api.soundcloud.comme.." or "api.soundcloud.complay..." Two bugs killed in one sitting, excellent. – brooklynsweb Jul 17 '15 at 05:49