Novels of Scotland, software, and the space between. Author of the Wardstane Chronicles, the DI Cormac Buchan Investigations, and the Pondlife novels.
per-plexed
GitHub · C#

per-plexed

A Plex event handler, driven by webhooks and API calls.

View on GitHub

Plex Webhook Folder Toucher

Small ASP.NET Core minimal API intended to run as a Windows Service.

It exposes a single endpoint (POST /webhook) that accepts Plex Webhooks form posts and, on relevant playback events, updates the last write time of the folder that contains the media file being played.

Why: some library/file sync/indexing workflows look at folder timestamps. Touching the folder on playback can be used to trigger downstream jobs (backup, metadata refresh, external indexers, etc.).

While the behavior in this repo is intentionally specific (touching a folder timestamp, and a couple of playback events), it’s also meant as a small, practical example of how to receive Plex webhook events and trigger your own actions (run a script, call another API, enqueue a job, etc.).

What it does

Logging

The app writes a simple text log to:

Logs are retained for 7 days.

It logs:

If any field is missing/unknown, the handler returns 200 OK (so Plex doesn’t keep retrying) and does nothing.

Requirements

Plex API lookup (to get the real filename)

Some Plex webhook payloads do not include the full filesystem path (no Metadata.Media[].Part[].file). In that case, this app will optionally call back into Plex using Metadata.ratingKey to resolve the real file path.

Set these environment variables on the machine running the service:

If these are not set, the app will log that lookup was skipped and it will not be able to touch folders.

How to get a Plex token

This token is the X-Plex-Token value.

Options:

  1. Plex Web App (Network tab)
  1. Plex server Preferences file

Setting env vars on Windows

Environment variables are read from the process environment.

System-level (Windows Service)

Run PowerShell as Administrator:

setx PLEX_BASEURL "http://<plex-server>:32400" /M
setx PLEX_TOKEN "<your-x-plex-token>" /M

Restart the service after setting these.

User-level (Visual Studio / console)

Run PowerShell (non-admin is fine):

setx PLEX_BASEURL "http://<plex-server>:32400"
setx PLEX_TOKEN "<your-x-plex-token>"

Close and re-open your terminal / Visual Studio so the new environment variables are picked up.

Build

From the repository root:

dotnet build

Run (console)

dotnet run

Then test it is listening:

curl -Method Post http://localhost:5000/webhook -ContentType "application/x-www-form-urlencoded" -Body "payload={}" 

You should get a 400 for invalid payload JSON, or 200 for a valid Plex-style payload.

Install as a Windows Service

1) Publish

Publish self-contained or framework-dependent. Framework-dependent is smaller and requires the .NET runtime installed on the machine.

Framework-dependent example:

dotnet publish -c Release -o C:\Services\PlexWebhookFolderToucher

2) Create the service

Run PowerShell as Administrator.

sc.exe create PlexWebhookFolderToucher binPath= "C:\Services\PlexWebhookFolderToucher\per-plexed.exe" start= auto

Optional: set a friendly description:

sc.exe description PlexWebhookFolderToucher "Receives Plex webhooks and touches media folders on playback events."

Start it:

sc.exe start PlexWebhookFolderToucher

Check status:

sc.exe query PlexWebhookFolderToucher

Stop / delete:

sc.exe stop PlexWebhookFolderToucher
sc.exe delete PlexWebhookFolderToucher

3) Firewall

Allow inbound TCP 5000 from your Plex server to the machine running this service.

Configure Plex Webhooks

  1. In Plex, install/configure the Webhooks feature/channel (depending on your Plex version).
  2. Set the webhook URL to:
http://<service-host>:5000/webhook
  1. Ensure Plex can resolve/reach <service-host>.

Webhook payload reference

Plex webhooks post form data and include a payload JSON field.

This app specifically uses Metadata.Media[0].Part[0].file from that payload.

Notes / troubleshooting

View per-plexed on GitHub