Using the API to add to a playlist

Hello, hoping to find an answer.

I have a Java application that works very well with the API provided at Plex API Documentation - Plexopedia.

However I cannot figure out how to get the add to playlist functionality to work.

I have spent several days (not all day) trying to resolve this problem.

I can delete items from a playlist and do almost anything else that I have tried but for some odd reason I cannot get the add to playlist URL to work.

The part of the API where the {library_id} might be part of the problem although I have read and re-read the bottom part of the page where it says to hover over the item in question and the “Library Key” parameter will be displayed.

I am not sure if this is the same thing but I have tried numerous variations on the API call using curl also and keep getting a 404 error.

Curiously this is also the only part of the API where it does not reference get this key valu from some other API call.

Please note I am on the local server when trying to accomplish this task.

I can build a window in Java that contains the movie name and other parameters including check boxes with a list of the playlists the movie is included or excluded from.

I can remove the movie from a playlist but cannot add it to one by checking or unchecking the box.

And yes the machine ID I am using agrees with everything I see in the Plex Web app and the API call.

Result from Java Eclipse:

Attempting to add!

The URL: http://localhost:32400/playlists/31785&uri=server://[Edited of course]6cf3d2a3e/com.plexapp.plugins.library/library/metadata/29071?X-Plex-Token=[TheToken]

Thanks!

If you look in the debugger of a browser while using the webclient to add a track to an existing music playlist, you’ll notice that the method is a PUT so are you sure you are using that, and not a GET

Yes I am using PUT.

Code snippet from “remove from playlist” which is working.

…
String theURL = “http://localhost:32400/playlists/” +
PlaylistID.elementAt(i) +
“/items/” + PlaylistItemID.elementAt(i) + //“library/metadata/” + laylistItemID.elementAt(i)
“?X-Plex-Token=” + pli.PlexUserID;
WriteLog("URL: " + theURL);
URL url = new URL(theURL);
WriteLog("URL: " + url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod(“DELETE”);
connection.connect();

InputStream stream = connection.getInputStream();
InputStream DataStream = getStringInputStream(stream);
…

Snippet from “add to playlist” which is NOT working:

…
String URL1 = “http://localhost:32400/playlists/” + PlaylistID.elementAt(i) + “&uri=server”;
String URL2 = “://” + pli.MachineIdentifier;
String URL21 = “/com.plexapp.plugins.library/” + “library/metadata/29071”;
// Note that I have hard-coded a value here while trying to get working
String URL3 = “?X-Plex-Token=” + pli.PlexUserID;
String allURL = URL1 + URL2 + URL21 + URL3;
URL url = new URL(allURL);
WriteLog("The URL: " + url.toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod(“PUT”);
connection.connect();

InputStream stream = connection.getInputStream();
InputStream DataStream = getStringInputStream(stream);
…

One note here is that trying to get the InputStream stream = … causes an exception but does not return a meaningful indication of what went wrong - e.getMessage() →

Error adding! http://localhost:32400/playlists/31787&uri=server://…

I have tried to look in the logs but I have not been able to find any useful information.

If I use curl with the generated string (escaping the &) I get

Unauthorized

401 Unauthorized

but the PlexToken works everywhere else so not sure what that means…

Thanks very much for your assistance and any suggestions on what else to try that I might be missing something.

The & should be a ?
Also note when looking at the screenshot I provided from the debugger, that the uri is url-encoded

Edit: and if you really need the token in the url, it’s &X-Plex-Token..... notice the &sign

And Nit, but since you are writing the url with the token to your log, I suggest adding the token to the header instead of the url, to avoid logging the token!

Edit:
connection.setRequestProperty("X-Plex-Token", "SomeToken");

1 Like

First thanks much for your assistance!

I have it working!

The suggestion to use connection.setRequestProperty was awesome - but I don’t know if that helped solve the problem but it did make things easier for me.

I have been using the URL Plex API: Add an Item to a Playlist - Plexopedia during my struggles.

So the & should be a ? needs to be corrected there.

Also I think then next big thing that needs to be corrected on that page is the API call says:

PUT http://{ip_address}:32400/playlists/{playlist_id}&uri=…

needs to be:

PUT http://{ip_address}:32400/playlists/{playlist_id}/items?uri=…

Note the addition of /items before the [&/?]uri=

Again thanks for your assistance!

I think I still need to make sure I get rid of the hard-coded Library_Id/Library_Key parameter and it would be great if that page could reflect how to find that directly through the API instead of the suggestion to hover in the browser.

But I can sleep easier tonight and thanks again for your assistance!

That website is not associated with Plex - you would need to reach out seperately to the author for any corrections/additions.

1 Like

It does have plexopedia.com as part of the address. I think it was very useful to me but yeah, I was never able to reach the author there…

Again, using the webclient to list playlists and the debugger, shows this:

/playlists?playlistType=audio&includeCollections=1&includeExternalMedia=1&includeAdvanced=1&X-Plex-Client-Identifier=<AN_ID_FOR_YOUR_APP>&X-Plex-Container-Start=0&X-Plex-Container-Size=50

Using a GET with above will return a list of playlists with their keys

Also note that above call use paging, so if you have more than 50 playlists, you must run the call again, but this time set X-Plex-Container-Start to 51

Yeah, everything is working well now.

I have returned to the dynamic Library_Key and that is working as expected.

Just a note - my application is only looking at what is playing at the current time. I want to be able to adjust what playlist the currently playing item belongs to, I use Plex mostly on ROKU devices and don’t want to have to remember or write down items that I want to add or remove from a playlist when I cannot/(don’t want to) use a computer to make adjustments.

One note is that I was retrieving the parameter for the ViewCount and that was causing an exception on new items that had not been completely viewed yet. But that has been handled now also.

I don’t really have many playlists so the size/(playlist length) will likely not be a problem/consideration. But I will keep that in mind.

My goal at the moment is to move the Java application into an Android app that I can use while laying in bed and adjust playlist items.

But who knows. I might add more features that I desire later.

Again thanks for your assistance!

One other note. I found the site that I used for development after using direct access to the database but was having difficulty getting the joins right.

PSS? - I also found the site that you referenced later but found it hard to interpret the documentation. Exactly what I needed to send to the server etc… But I’m an old guy and semi-retired and just kinda doing this as a hobby now so might not be as sharp as I once was…

This might be a use case where web hooks could be beneficial. You can be notified of certain events which occur on the server (playback start/stop, for example). The event payload includes a lot of (potentially) useful information, including paths to the artwork for the item being acted upon.

https://support.plex.tv/articles/115002267687-webhooks/

[Edit]
Also, if the item is a member of a collection, the collection name is also included. I’m not sure if that’s the case with playlists.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.