How to Get a Plex Server's Session History

Plex stores the session history for all users in the database. Everything streamed from your Plex server for all users is retained in the database.

This also means that you can get your Plex server's session data at any time. This can be done using the Plex API.

In this post, I will show a simple Python script that can return all session history data, history for a specific account, or play history for a specific library.

How to Get a Plex Server's Session History

Getting the session history

The Python script to display the Plex logs in real time involves making a call using a single Plex API endpoint.

The API endpoint is the Get Session History endpoint, and optional parameters allow you to filter the history data that is returned.

The Python script below contains three functions, each one returning session history for all libraries and users, for a single user, and a single library.

import os
import requests
import sys

# Get the Plex URL and token from the environment variables
plex_url = os.environ.get('PLEX_URL')
plex_token = os.environ.get('PLEX_TOKEN')
library_id = {library_id}
account_id = {account_ikd}

def get_all_session_history():
    """
    Gets all session history.
    """    
    response = requests.get('{0}/status/sessions/history/all?X-Plex-Token={1}'.format(plex_url, plex_token))
    if response.ok:
        return response.content
    else:
        return None

def get_account_session_history():
    """
    Gets session history for an account.
    """    
    response = requests.get('{0}/status/sessions/history/all?X-Plex-Token={1}&accountID={2}'.format(plex_url, plex_token, account_id))
    if response.ok:
        return response.content
    else:
        return None

def get_library_session_history():
    """
    Gets session history for a library.
    """    
    response = requests.get('{0}/status/sessions/history/all?X-Plex-Token={1}&librarySectionID={2}'.format(plex_url, plex_token, library_id))
    if response.ok:
        return response.content
    else:
        return None

if __name__ == "__main__":
    history = get_all_session_history()
    if history is None:
        sys.exit('The session history was not returned.')

    print(history)

    history = get_account_session_history()
    if history is None:
        sys.exit('The session history for the account was not returned.')

    print(history)

    history = get_library_session_history()
    if history is None:
        sys.exit('The session history for the library was not returned.')

    print(history)    

How to use the script

To use the script, you will need to do the following:

  1. Install Python.
  2. After Python is installed, run the following pip command to install the dependencies:
    pip install websockets
  3. Copy the above script and save it as a Python file, for example, console_log.py
  4. Create an environment variable called PLEX_URL and set it to the URL of your Plex server. For example: http://localhost:32400.
  5. Create an environment variable called PLEX_TOKEN and set it to your Plex token.
  6. Replace {library_id} and {account_id} with valid library and account IDs.

What does the script do?

The above script contains three functions:

get_all_session_history()
Gets all session history since the Plex server was created.
get_account_session_history()
Gets all session history for a specific account.
get_library_session_history()
Gets all session history for a specific library.

Each function is called one after the other and the response contents - the session history - is then printed to the console.

The script just shows how you can get the session history from your Plex server, but you can extend the script to do so much more, such as parse the response and display the history as you would like, or the history for a specific record.

Photo of Paul Salmon
Started managing a Plex server in November 2014 and has been sharing his experience and what he has learned on Plexopedia. He is exploring and documenting the Plex API to help automate tasks for Plex to reduce the management effort of his server.
Subscribe
Display