Best practices for using API keys

API keys are required for apps and projects that use Plex APIs. Secure your API keys when you create them for maximum security.

It’s possible to secure API keys after they're created and in use, but there can be different constraints based on how the key is used. Protect against unauthorized access by selecting the products it’s used for and regularly replacing keys.

Restrict API keys

When you first create your API keys, apply API restrictions so they run only on specific APIs. You must have a valid API key to use an API. Limit the usage of API keys to only the necessary APIs. You can specify as many APIs as required.

When creating or editing an API key, you can set the key to expire after a certain amount of time. This helps prevent against unauthorized access by ensuring the API key does not persist longer than needed. After the key expires, you must reset the expiration date or make a new key.

Make sure all your API keys have API restrictions and expiration dates. If you have insecure API keys, create new ones with the appropriate security measures and update all your apps with the new API keys. One key per application provides the most security, but you can use a key on multiple apps if the restrictions on the key don’t cause incompatibility issues with the other apps using that key.

When restricting existing API keys by editing apps, make sure the restrictions don’t break any of your existing apps.

Restrict developer portal account access

Prevent unnecessary API key creating by restricting access to the developer portal. Your Plex champion should grant access only to users who need it.

Regularly audit your API keys

Frequently audit your API keys to confirm they’re being actively used. We recommend auditing your API keys at least every three months. Check the usage of your keys in the analytics tab and delete any unneeded keys to keep your APIs secure.

Use independent API keys for different apps

Limit the scope of each key. If an API key is compromised, you can delete and revoke the impacted key without needing to update your other API keys. Note that the test and production keys must be different keys.

Regenerate API keys regularly

API keys can be created and destroyed without affecting the integrations. Rotating API keys limits the potential impact of an API key that is leaked while still providing access to the resource.

Rotate API keys by performing the following steps:

  1. Create a new API key.
  2. Update the resource or application to use the new API key.
  3. Delete the old API key.

A short time overlap between switching keys ensures a smooth turnover without disrupting integrations.

Create a generic IAM ID for developer portal access

Better control access to API keys by creating a single, generic account for creating API keys. If there are unneeded keys, the admin can repurpose or delete the keys.

Store API keys in a secure location

Store your API keys in environment variables or in files outside of your application’s source tree. Keeping the files outside your application's source tree to help ensure your keys do not end up in your source code control system. This is particularly important if you use a public source code management system such as GitHub. Do not embed API keys directly in code. API keys that are embedded in code can be accidentally exposed to the public if you forget to remove the keys from the code that you share.

Encrypt keys on code repositories

Always encrypt any keys stored on code repositories. When sharing code online, it’s easy to forget your credentials in the code, leaving them exposed to the world. Repositories frequently get cloned and forked into new projects, giving new developers access to their complete history. Any credentials within the repository's history will exist in all new repositories born from that source.