Simple wrapper for querying against HTTP API on the Plex Media Server
Small module which helps you query the Plex Media Server HTTP API.
PlexAPI(options | hostname)
Instantiate a PlexAPI client.
The parameter can be a string representing the server's hostname, or an object with the following properties:
generated uuid v4. Note: you should really provide this rather than let it get generated. Every time your app runs, a new "device" will get registered on your Plex account, which can lead to poor performance once hundreds or thousands of them get created. Trust me!
Plex Home Theater,
Plex for Xbox One. Default
Plex Home Theater,
Windows. Default is
Xbox One. Default is whatever
Here's an example of what an app shows up as on the Plex web interface
The rows in that example from top to bottom are
Retrieve content from URI
Aside from requesting the API and returning its response, an
.uri property are created to easier follow the URIs available in the HTTP API. At the moment URIs are attached for Directory and Server items.
var PlexAPI = require"plex-api";var client = "192.168.0.1";clientquery"/"thenconsole.log"%s running Plex Media Server v%s"resultfriendlyNameresultversion;// array of children, such as Directory or Server items// will have the .uri-property attachedconsole.logresult_children;throw "Could not connect to server";;
Send a POST request and retrieve the response
This is identical to
query(uri), except that the request will be a POST rather than a GET.
Note that the parameters can only be passed as a query string as part of the uri, which is all Plex requires. (
Content-Length will always be zero)
var PlexAPI = require"plex-api";var client = "192.168.0.1";clientpostQuery"/playQueue?type=video&uri=someuri&shuffle=0'"thenconsole.log"Added video to playQueue %s"resultplayQueueID;// array of children, such as Directory or Server items// will have the .uri-property attachedconsole.logresult_children;throw "Could not connect to server";;
Perform an API action
When performing an "action" on the HTTP API, the response body will be empty.
As the response content itself is worthless,
perform() acts on the HTTP status codes the server responds with.
var PlexAPI = require"plex-api";var client = "192.168.0.1";// update library section of key "1"clientperform"/library/sections/1/refresh"then// successfully started to refresh library section #1throw "Could not connect to server";;
Find matching child items on URI
query() behind the scenes, giving all directories and servers the beloved
var PlexAPI = require"plex-api";var client = "192.168.0.1";// filter directories on Directory attributesclientfind"/library/sections" type: "movie"then// directories would be an array of sections whose type are "movie"throw "Could not connect to server";;// criterias are interpreted as regular expressionsclientfind"/library/sections" type: "movie|shows"then// directories type would be "movie" OR "shows"throw "Could not connect to server";;// shorthand to retrieve all Directoriesclientfind"/"then// directories would be an array of Directory itemsthrow "Could not connect to server";;
An authenticator is used by plex-api to authenticate its request against Plex Servers with a PlexHome setup. The most common authentication mechanism is by username and password.
You can provide your own custom authentication mechanism, read more about custom authenticators below.
Comes bundled with plex-api. Just provide
options.password when creating a PlexAPI instance and you are good to go.
See the plex-api-credentials module for more information about its inner workings.
In its simplest form an
authenticator is an object with one required function
authenticate() which should return the autentication token needed by plex-api to satisfy Plex Server.
An optional method
initialize() could be implemented if you need reference to the created PlexAPI instance when it's created.
// OPTIONAL// plexApi === the PlexAPI instance just created// REQUIRED// plexApi === the PlexAPI instance requesting the authentication token// invoke callback if something failsif somethingFailedreturn callback'I haz no cluez about token!';// or when you have a tokencallbacknull 'I-found-this-token';
For more information about the API capabilities, see the unofficial Plex API documentation.
$ npm test
Contributions are more than welcome! Create an issue describing what you want to do. If that feature is seen to fit this project, send a pull request with the changes accompanied by tests.
Some of URIs on the Plex Server responds with XML instead of JSON. Previous versions of plex-api used xml2json to translate between XML to JSON. We have now replaced xml2json with xml2js which might result in a different JSON format when requesting URIs responding with XML.
postQuery()to perform POST requests by @OverloadUT
.query(), by @OverloadUT
.uriin some cases by @pjeby
We're now retrieving JSON from the Plex HTTP API instead of XML, which got translated to JSON by this module. Direct consequences:
result.attributesare now available directly in
clientquery"/"thenconsole.logresultfriendlyName; // was result.attributes.friendlyNameconsole.log"Directory count:" result_childrenlength; // was result.directory.length;
v1.0.0 mostly to be a better semver citizen and some housekeeping.
(The MIT License)
Copyright (c) 2013-2015 Phillip Johnsen <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.