lukehagar / plex-api
Requires
- php: ^8.2
- brick/date-time: ^0.7.0
- brick/math: ^0.12.1
- galbar/jsonpath: ^3.0
- guzzlehttp/guzzle: ^7.0
- phpdocumentor/type-resolver: ^1.8
- speakeasy/serializer: ^4.0.3
Requires (Dev)
- laravel/pint: ^1.18.1
- phpstan/phpstan: ^2.1.0
- phpunit/phpunit: ^10
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2025-03-10 00:10:43 UTC
README
Summary
Plex-API: An Open API Spec for interacting with Plex.tv and Plex Media Server
Plex Media Server OpenAPI Specification
An Open Source OpenAPI Specification for Plex Media Server
Automation and SDKs provided by Speakeasy
Documentation
SDKs
The following SDKs are generated from the OpenAPI Specification. They are automatically generated and may not be fully tested. If you find any issues, please open an issue on the main specification Repository.
Language | Repository | Releases | Other |
---|---|---|---|
Python | GitHub | PyPI | - |
JavaScript/TypeScript | GitHub | NPM \ JSR | - |
Go | GitHub | Releases | GoDoc |
Ruby | GitHub | Releases | - |
Swift | GitHub | Releases | - |
PHP | GitHub | Releases | - |
Java | GitHub | Releases | - |
C# | GitHub | Releases | - |
Table of Contents
SDK Installation
The SDK relies on Composer to manage its dependencies.
To install the SDK and add it as a dependency to an existing composer.json
file:
composer require "lukehagar/plex-api"
SDK Example Usage
Example
declare(strict_types=1); require 'vendor/autoload.php'; use LukeHagar\Plex_API; $sdk = Plex_API\PlexAPI::builder() ->setSecurity( '<YOUR_API_KEY_HERE>' ) ->build(); $response = $sdk->server->getServerCapabilities( ); if ($response->object !== null) { // handle response }
Authentication
Per-Client Security Schemes
This SDK supports the following security scheme globally:
Name | Type | Scheme |
---|---|---|
accessToken |
apiKey | API key |
To authenticate with the API the accessToken
parameter must be set when initializing the SDK. For example:
declare(strict_types=1); require 'vendor/autoload.php'; use LukeHagar\Plex_API; $sdk = Plex_API\PlexAPI::builder() ->setSecurity( '<YOUR_API_KEY_HERE>' ) ->build(); $response = $sdk->server->getMediaProviders( xPlexToken: 'CV5xoxjTpFKUzBTShsaf' ); if ($response->object !== null) { // handle response }
Available Resources and Operations
Available methods
activities
- cancelServerActivities - Cancel Server Activities
- getServerActivities - Get Server Activities
authentication
- getSourceConnectionInformation - Get Source Connection Information
- getTokenDetails - Get Token Details
- getTransientToken - Get a Transient Token
- postUsersSignInData - Get User Sign In Data
butler
- getButlerTasks - Get Butler tasks
- startAllTasks - Start all Butler tasks
- startTask - Start a single Butler task
- stopAllTasks - Stop all Butler tasks
- stopTask - Stop a single Butler task
hubs
- getRecentlyAdded - Get Recently Added
- getGlobalHubs - Get Global Hubs
- getLibraryHubs - Get library specific hubs
library
- deleteLibrary - Delete Library Section
- getActorsLibrary - Get Actors of library media
- getAllLibraries - Get All Libraries
- getAllMediaLibrary - Get all media of library
- getCountriesLibrary - Get Countries of library media
- getGenresLibrary - Get Genres of library media
- getLibraryDetails - Get Library Details
- getLibraryItems - Get Library Items
- getMediaMetaData - Get Media Metadata
- getRecentlyAddedLibrary - Get Recently Added
- getRefreshLibraryMetadata - Refresh Metadata Of The Library
- getSearchAllLibraries - Search All Libraries
- getSearchLibrary - Search Library
- getFileHash - Get Hash Value
- getMetadataChildren - Get Items Children
- getTopWatchedContent - Get Top Watched Content
log
- enablePaperTrail - Enabling Papertrail
- logLine - Logging a single line message.
- logMultiLine - Logging a multi-line message
media
- getBannerImage - Get Banner Image
- getThumbImage - Get Thumb Image
- markPlayed - Mark Media Played
- markUnplayed - Mark Media Unplayed
- updatePlayProgress - Update Media Play Progress
playlists
- addPlaylistContents - Adding to a Playlist
- clearPlaylistContents - Delete Playlist Contents
- createPlaylist - Create a Playlist
- deletePlaylist - Deletes a Playlist
- getPlaylist - Retrieve Playlist
- getPlaylistContents - Retrieve Playlist Contents
- getPlaylists - Get All Playlists
- updatePlaylist - Update a Playlist
- uploadPlaylist - Upload Playlist
plex
- getServerResources - Get Server Resources
- getCompanionsData - Get Companions Data
- getGeoData - Get Geo Data
- getHomeData - Get Plex Home Data
- getPin - Get a Pin
- getTokenByPinId - Get Access Token by PinId
- getUserFriends - Get list of friends of the user logged in
search
- getSearchResults - Get Search Results
- performSearch - Perform a search
- performVoiceSearch - Perform a voice search
server
- getMediaProviders - Get Media Providers
- getServerIdentity - Get Server Identity
- getAvailableClients - Get Available Clients
- getDevices - Get Devices
- getMyPlexAccount - Get MyPlex Account
- getResizedPhoto - Get a Resized Photo
- getServerCapabilities - Get Server Capabilities
- getServerList - Get Server List
- getServerPreferences - Get Server Preferences
sessions
- getSessionHistory - Get Session History
- getSessions - Get Active Sessions
- getTranscodeSessions - Get Transcode Sessions
- stopTranscodeSession - Stop a Transcode Session
statistics
- getBandwidthStatistics - Get Bandwidth Statistics
- getResourcesStatistics - Get Resources Statistics
- getStatistics - Get Media Statistics
updater
- applyUpdates - Apply Updates
- checkForUpdates - Checking for updates
- getUpdateStatus - Querying status of updates
users
- getUsers - Get list of all connected users
video
- getTimeline - Get the timeline for a media item
- startUniversalTranscode - Start Universal Transcode
watchlist
- getWatchList - Get User Watchlist
Error Handling
Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.
By default an API error will raise a Errors\SDKException
exception, which has the following properties:
Property | Type | Description |
---|---|---|
$message |
string | The error message |
$statusCode |
int | The HTTP status code |
$rawResponse |
?\Psr\Http\Message\ResponseInterface | The raw HTTP response |
$body |
string | The response content |
When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the getMediaProviders
method throws the following exceptions:
Error Type | Status Code | Content Type |
---|---|---|
Errors\GetMediaProvidersBadRequest | 400 | application/json |
Errors\GetMediaProvidersUnauthorized | 401 | application/json |
Errors\SDKException | 4XX, 5XX | */* |
Example
declare(strict_types=1); require 'vendor/autoload.php'; use LukeHagar\Plex_API; use LukeHagar\Plex_API\Models\Errors; $sdk = Plex_API\PlexAPI::builder() ->setSecurity( '<YOUR_API_KEY_HERE>' ) ->build(); try { $response = $sdk->server->getMediaProviders( xPlexToken: 'CV5xoxjTpFKUzBTShsaf' ); if ($response->object !== null) { // handle response } } catch (Errors\GetMediaProvidersBadRequestThrowable $e) { // handle $e->$container data throw $e; } catch (Errors\GetMediaProvidersUnauthorizedThrowable $e) { // handle $e->$container data throw $e; } catch (Errors\SDKException $e) { // handle default exception throw $e; }
Server Selection
Server Variables
The default server {protocol}://{ip}:{port}
contains variables and is set to https://10.10.10.47:32400
by default. To override default values, the following builder methods are available when initializing the SDK client instance:
Variable | BuilderMethod | Supported Values | Default | Description |
---|---|---|---|---|
protocol |
setProtocol(Plex_API\ServerProtocol protocol) |
- "http" - "https" |
"https" |
The protocol to use for the server connection |
ip |
setIp(string ip) |
string | "10.10.10.47" |
The IP address or hostname of your Plex Server |
port |
setPort(string port) |
string | "32400" |
The port of your Plex Server |
Example
declare(strict_types=1); require 'vendor/autoload.php'; use LukeHagar\Plex_API; $sdk = Plex_API\PlexAPI::builder() ->setProtocol('https') ->setIp('247.38.141.142') ->setPort('6717') ->setSecurity( '<YOUR_API_KEY_HERE>' ) ->build(); $response = $sdk->server->getMediaProviders( xPlexToken: 'CV5xoxjTpFKUzBTShsaf' ); if ($response->object !== null) { // handle response }
Override Server URL Per-Client
The default server can be overridden globally using the setServerUrl(string $serverUrl)
builder method when initializing the SDK client instance. For example:
declare(strict_types=1); require 'vendor/autoload.php'; use LukeHagar\Plex_API; $sdk = Plex_API\PlexAPI::builder() ->setServerURL('https://10.10.10.47:32400') ->setSecurity( '<YOUR_API_KEY_HERE>' ) ->build(); $response = $sdk->server->getMediaProviders( xPlexToken: 'CV5xoxjTpFKUzBTShsaf' ); if ($response->object !== null) { // handle response }
Override Server URL Per-Operation
The server URL can also be overridden on a per-operation basis, provided a server list was specified for the operation. For example:
declare(strict_types=1); require 'vendor/autoload.php'; use LukeHagar\Plex_API; use LukeHagar\Plex_API\Models\Operations; $sdk = Plex_API\PlexAPI::builder() ->setSecurity( '<YOUR_API_KEY_HERE>' ) ->build(); $response = $sdk->plex->getServerResources( 'https://plex.tv/api/v2', clientID: '3381b62b-9ab7-4e37-827b-203e9809eb58', includeHttps: Operations\IncludeHttps::Enable, includeRelay: Operations\IncludeRelay::Enable, includeIPv6: Operations\IncludeIPv6::Enable ); if ($response->plexDevices !== null) { // handle response }
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Contributions
While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!