# Using the API to add to a playlist

**URL:** <https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306>\
**Category:** General Discussions\
**Created:** [November 13, 2024, 3:09am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306 "2024-11-13T03:09:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![drstev](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@drstev](https://forums.plex.tv/u/drstev)\
**Post date:** [November 13, 2024, 3:09am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/1 "2024-11-13T03:09:04Z")

</div>

Hello, hoping to find an answer.

I have a Java application that works very well with the API provided at [Plex API Documentation - Plexopedia](https://www.plexopedia.com/plex-media-server/api/).

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://](http://localhost:32400/playlists/31785&uri=server://)[Edited of course]6cf3d2a3e/com.plexapp.plugins.library/library/metadata/29071?X-Plex-Token=[TheToken]

Thanks!

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 13, 2024, 11:31am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/2 "2024-11-13T11:31:20Z")

</div>

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`

 ![image](https://global.discourse-cdn.com/plex/original/4X/d/8/d/d8d79016357c7ecc6fb2bf0633fedd21ad85264b.png)

---

<div class="post-metadata">

**Author:** ![drstev](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@drstev](https://forums.plex.tv/u/drstev)\
**Post date:** [November 13, 2024, 7:22pm UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/3 "2024-11-13T19:22:23Z")

</div>

Yes I am using PUT.

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

…  
String theURL = “[http://localhost:32400/playlists/](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/](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://](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.

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 13, 2024, 7:48pm UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/4 "2024-11-13T19:48:51Z")

</div>

> [@drstev](#):
>
> [http://localhost:32400/playlists/31785&uri=server:](http://localhost:32400/playlists/31785&uri=server://)

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

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 13, 2024, 7:52pm UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/5 "2024-11-13T19:52:20Z")

</div>

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");`

---

<div class="post-metadata">

**Author:** ![drstev](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@drstev](https://forums.plex.tv/u/drstev)\
**Post date:** [November 14, 2024, 1:43am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/6 "2024-11-14T01:43:17Z")

</div>

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](https://www.plexopedia.com/plex-media-server/api/playlists/add-item/) 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!

---

<div class="post-metadata">

**Author:** ![Flanders](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/flanders/32/81906_2.png) [@Flanders](https://forums.plex.tv/u/Flanders)\
**Post date:** [November 14, 2024, 7:30am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/7 "2024-11-14T07:30:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drstev](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@drstev](https://forums.plex.tv/u/drstev)\
**Post date:** [November 14, 2024, 7:44am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/8 "2024-11-14T07:44:17Z")

</div>

It does have [plexopedia.com](http://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…

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [November 14, 2024, 10:38am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/9 "2024-11-14T10:38:38Z")

</div>

> [@drstev](#):
>
> 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.

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`

---

<div class="post-metadata">

**Author:** ![drstev](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@drstev](https://forums.plex.tv/u/drstev)\
**Post date:** [November 15, 2024, 1:34am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/10 "2024-11-15T01:34:31Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pshanew](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pshanew/32/160555_2.png) [@pshanew](https://forums.plex.tv/u/pshanew)\
**Post date:** [November 15, 2024, 1:46am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/11 "2024-11-15T01:46:28Z")

</div>

> [@drstev](#):
>
> …my application is only looking at what is playing at the current time.

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/](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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [February 13, 2025, 1:46am UTC](https://forums.plex.tv/t/using-the-api-to-add-to-a-playlist/895306/12 "2025-02-13T01:46:37Z")

</div>

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