Plex API
Plex API Reference
Section titled “Plex API Reference”Automate Plex with the REST API.
Authentication
Section titled “Authentication”Get Plex Token
Section titled “Get Plex Token”# Method 1: From Plex Web# Sign in → Browser DevTools → Network tab → Look for X-Plex-Token
# Method 2: APIcurl -u "username:password" \ "https://plex.tv/users/sign_in.xml" \ -X POST \ -H "X-Plex-Client-Identifier: MyApp"Common Endpoints
Section titled “Common Endpoints”Server Info
Section titled “Server Info”# Identitycurl "http://localhost:32400/identity?X-Plex-Token=TOKEN"
# Server capabilitiescurl "http://localhost:32400/?X-Plex-Token=TOKEN"Libraries
Section titled “Libraries”# List librariescurl "http://localhost:32400/library/sections?X-Plex-Token=TOKEN"
# Get library contentscurl "http://localhost:32400/library/sections/1/all?X-Plex-Token=TOKEN"
# Refresh librarycurl -X POST "http://localhost:32400/library/sections/1/refresh?X-Plex-Token=TOKEN"Search
Section titled “Search”# Search allcurl "http://localhost:32400/search?query=inception&X-Plex-Token=TOKEN"
# Search librarycurl "http://localhost:32400/library/sections/1/search?query=inception&X-Plex-Token=TOKEN"Playback
Section titled “Playback”# Currently playingcurl "http://localhost:32400/status/sessions?X-Plex-Token=TOKEN"Python Library
Section titled “Python Library”PlexAPI
Section titled “PlexAPI”pip install plexapifrom plexapi.server import PlexServer
plex = PlexServer('http://localhost:32400', 'YOUR_TOKEN')
# List librariesfor section in plex.library.sections(): print(f"{section.title}: {section.totalSize} items")
# Searchresults = plex.search('Inception')
# Refresh librarymovies = plex.library.section('Movies')movies.refresh()
# Get item detailsmovie = plex.library.section('Movies').get('Inception')print(f"Rating: {movie.rating}")print(f"Bitrate: {movie.media[0].bitrate}")Automation Example
Section titled “Automation Example”#!/usr/bin/env python3"""Plex library maintenance script."""
from plexapi.server import PlexServerimport os
PLEX_URL = os.getenv('PLEX_URL', 'http://localhost:32400')PLEX_TOKEN = os.getenv('PLEX_TOKEN')
def main(): plex = PlexServer(PLEX_URL, PLEX_TOKEN)
# Refresh all libraries for section in plex.library.sections(): print(f"Refreshing {section.title}...") section.refresh()
# Clean bundles plex.library.cleanBundles()
# Optimize database plex.library.optimize()
if __name__ == "__main__": main()Webhook Integration
Section titled “Webhook Integration”Configure webhooks for events:
- Settings → Webhooks
- Add webhook URL
- Events sent as POST requests
Example payload:
{ "event": "media.play", "user": true, "owner": true, "Account": { "title": "username" }, "Metadata": { "title": "Movie Title", "type": "movie" }}Rate Limits
Section titled “Rate Limits”- Be respectful of API limits
- Cache responses when possible
- Use webhooks instead of polling