PlexAPI

4.18.1 · active · verified Mon Apr 13

PlexAPI is a Python library that provides bindings for the Plex Media Server API. It allows programmatic interaction with your Plex server, enabling tasks such as managing libraries, controlling media playback, retrieving metadata, and more. The library is actively maintained, with frequent releases, typically every few weeks.

Warnings

Install

Imports

Quickstart

Connects to a Plex Media Server using provided URL and token, then lists available libraries and demonstrates how to find a specific media item.

import os
from plexapi.server import PlexServer

# Replace with your Plex URL and token, or set as environment variables
# Example: PLEX_URL='http://your_plex_ip:32400', PLEX_TOKEN='YOUR_PLEX_TOKEN'
PLEX_URL = os.environ.get('PLEX_URL', 'http://localhost:32400') # Default for local access
PLEX_TOKEN = os.environ.get('PLEX_TOKEN', 'YOUR_PLEX_TOKEN') # Get from Plex settings > Network > Show Advanced > token

try:
    plex = PlexServer(PLEX_URL, PLEX_TOKEN)
    print(f"Connected to Plex server: {plex.friendlyName}")

    # List all libraries
    print("\nLibraries:")
    for section in plex.library.sections():
        print(f"- {section.title} ({section.type})")

    # Example: Find a movie in a 'Movies' library
    # Adjust 'Movies' to the actual name of your movie library
    try:
        movies_library = plex.library.section('Movies')
        inception_movie = movies_library.get('Inception')
        print(f"\nFound movie: {inception_movie.title} ({inception_movie.year})")
    except Exception as e:
        print(f"\nCould not find 'Movies' library or 'Inception': {e}")

except Exception as e:
    print(f"Error connecting to Plex or performing operations: {e}")
    print("Please ensure PLEX_URL and PLEX_TOKEN are correctly configured in environment variables or directly in the script.")

view raw JSON →