Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Automatically share and unshare libraries for Plex users
# Run this script using "share" or "unshare" as arguments:
# To share the Plex libraries:
# python share
# To unshare the Plex libraries:
# python unshare
import requests
import sys
from xml.dom import minidom
# Get the User IDs and Library IDs from
# Example:
# Enter the User IDs and Library IDs in this format below:
# {UserID1: [LibraryID1, LibraryID2],
# UserID2: [LibraryID1, LibraryID2]}
USER_LIBRARIES = {1234567: [1234567, 1234567]}
def share():
headers = {"X-Plex-Token": PLEX_TOKEN,
"Accept": "application/json"}
url = "" + SERVER_ID + "/shared_servers"
for user_id, library_ids in USER_LIBRARIES.iteritems():
payload = {"server_id": SERVER_ID,
"shared_server": {"library_section_ids": library_ids,
"invited_id": user_id}
r =, headers=headers, json=payload)
if r.status_code == 401:
print "Invalid Plex token"
elif r.status_code == 400:
print r.content
elif r.status_code == 200:
print "Shared libraries with user %s" % str(user_id)
def unshare():
headers = {"X-Plex-Token": PLEX_TOKEN,
"Accept": "application/json"}
url = "" + SERVER_ID + "/shared_servers"
r = requests.get(url, headers=headers)
if r.status_code == 401:
print "Invalid Plex token"
elif r.status_code == 400:
print r.content
elif r.status_code == 200:
response_xml = minidom.parseString(r.content)
MediaContainer = response_xml.getElementsByTagName("MediaContainer")[0]
SharedServer = MediaContainer.getElementsByTagName("SharedServer")
shared_servers = {int(s.getAttribute("userID")): int(s.getAttribute("id"))
for s in SharedServer}
for user_id, library_ids in USER_LIBRARIES.iteritems():
server_id = shared_servers.get(user_id)
if server_id:
url = "" + SERVER_ID + "/shared_servers/" + str(server_id)
r = requests.delete(url, headers=headers)
if r.status_code == 200:
print "Unshared libraries with user %s" % str(user_id)
print "No libraries shared with user %s" % str(user_id)
if __name__ == "__main__":
if len(sys.argv) != 2:
print 'You must provide "share" or "unshare" as an argument'
elif sys.argv[1] == "share":
elif sys.argv[1] == "unshare":
Copy link

Magikarplvl4 commented Jan 13, 2017

I got a error when i try this very amazing script :(

PlexPy Notifiers :: Script error: 
    File "D:\plexpy\scripts\", line 107 
        elif r.status_code == 200: 
SyntaxError: invalid syntax

Copy link

Triskelen commented Feb 20, 2017

i get a error :
PS C:\Users\Administrator> C:\Python27\python.exe C:\Plexpy\ unshare
line 7 cannot import requests

Copy link

ninthwalker commented Apr 14, 2017

Just posting that I also get errors. Specifically just trying with the POST code only to update a shared Library, but i get the returned response code:

Tried with some general python POSTS but kept getting returned with:

<Response code="400" status="You're already sharing this server with plexFriend. Please edit your existing share."/>

Was using the following POST:

headers = {"X-Plex-Token": 'Ycx2134yTyaJHGFxKtgezn9',
           "Accept": "application/json"}
url = ""

payload = {"server_id": "GUte86560eJHGa7fagde5j77dd4bjgrLGHT05",
           "shared_server": {"library_section_ids": "[35723967]",
                                 "invited_id": "2120385"}

r =, headers=headers, json=payload)

So I must have been doing the request wrong.
(Tokens in code all fake)

Copy link

tweakfour17 commented Sep 19, 2017

I've got this to work..sort of. When I do the unshare it unshares for all 4 users, however when I do the share function it will only share for 1 of the users, and if I attempt to re-run the share it stops and says I'm already sharing. Any idea why this could be?

Copy link

timespaced commented Jan 1, 2018

This script works for unsharing all users, but like tweakfour17 said, it will only enable sharing for 1 user then stops. I'm too stupid to troubleshoot this - hopefully someone can figure it out.

Copy link

someredpanda commented Mar 8, 2018

@haltdev and @tweakfour17.

If you're still curious about why the script stops after sharing with one user; there's a return statement on line 50 that interrupts the for loop. Just remove it and it'll handle multiple users fine.

Copy link

yonderbread commented Sep 17, 2020

Howdy, is there an endpoint for retrieving the ID of a user by their email?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment