When running the Plex API commands to return a list of media items, it may be helpful to filter the list of items to only a select few. This is more helpful if you have thousands of media items in your Plex library.
The API provides the ability to filter out items that are of interest to you. A filter can be applied to any attribute that is returned in the response from the Plex server.
How to use filtering with the Plex API
Filters are expressed using a querystring parameter. Each querystring contains a field, an operator, and a value. For example, to specify a filter, you would use:
http://[IP Address]:32400/library/sections/[Library ID]/all?X-Plex-Token=[PlexToken]&[field][operator][value]
A field is any valid attribute that is returned in a response from the Plex server. For example, the
year attribute for a movie or the
contentRating attribute for a TV show.
The field name is case-sensitive, so you will need to use
contentRating and not
contentrating. The API will return a 200 (success) status code, but the response will include all media items for the library, instead of just the filtered items.
The operator is used to compare the attribute values with the value provided in the querystring. The following table lists the valid operators:
|=||Returns all media that is tagged with or equal to the value.|
|!=||Returns all media that is not tagged with or not equal to the value.|
|>=||Returns all media that is greater than or equal to the value.|
|<=||Returns all media that is less than or equal to the value.|
Examples of using the operators with both string and numeric values are given below.
The value is used to compare with the actual value of the field. The value provided in the querystring may or may not be case-sensitive. For example, when filtering by studio, "marvel" and "Marvel" will return the same media items, but when filtering by content rating, that value is case-sensitive.
You can specify multiple filter parameters for a request. When you do specify multiple filters, they will be logically ANDed together, which means only media items that satisfy all the filter criteria will be returned.
The following are filtering examples that can be used with the Plex API. Note: the movies library ID is 2, while the TV shows library is 3 in the examples.
Get a list of all Marvel movies in a library:
Note, the actual studio name is "Marvel Studios", but only part of a string needs to match for an item to be returned.
Get a list of all Marvel movies in a library that were released from the year 2011 and later:
Get a list of all TV shows that have a TV-Y content rating:
contentRating field is a string value, it has specific values it uses, and is case-sensitive.
If you use a specific naming convention for your movies that includes the codecs used for the video and audio streams in the file names, you can filter on values in the file names. The following will return all moves that have AC3 in the file name: