An API (application programming interface) is essentially a gateway that allows outside software to communicate with your application. Using that metaphor, it’s easy to see the importance of an API key – it grants access to your application’s gateway. Anyone with a key can get in, which makes API key management critical to your digital security strategy.
First, API keys are only half the equation. An API key is like a username, but a program still needs a password – or secret – to gain access to your application. The API key and secret are often collectively referred to as API credentials. The best practices for API key management involve the secure creation, usage, storage, and handling of API credentials.
You should allow authorized users to generate, edit, and delete their own API keys. This is an API key management best practice because it makes handling API keys more efficient, which in turn enables good security hygiene.
If it’s easy for users to generate API keys at will, then each API key can have limited privileges to specific resources. Rather than trying to cover all bases with a single set of credentials, authorized users can tailor access with each set of credentials.
Plus, if any authorized user can generate their own API key, they won’t need to share credentials with someone else, keeping each key and secret more secure.
When you’re assigning privileges to an API key, you should always apply the principle of least privilege. That means you should grant the minimum privileges required for the task at hand. The principle of least privilege helps mitigate the damage from breaches and compromised credentials by restricting the amount of lateral movement possible.
During the development process, you may be tempted to embed an API key directly in your source code for convenience but doing so is a huge security risk. Even if you don’t intend to publish that source code, you never know where it will end up. Some future developer may share your code online without realizing they’re exposing a hard-coded API key.
It’s best practice for API key management to store your credentials on a separate system. Not only will this prevent any credentials from being accidentally exposed, but it also makes it easier to dynamically add and regenerate API keys without needing to rewrite your source code.
If you’re developing web or mobile apps that are accessed from outside your organization, you must store your API keys and secrets on a backend system. An experienced hacker can easily reverse-engineer web and mobile apps to access API credentials stored on front-end web servers. You should instead keep your API credentials on a separate server that’s inaccessible to external users and under stricter security controls.
Some developers get into the bad habit of leaving API keys exposed in their private repositories for ease of use. However, no matter how private and secure your internal code repository is, there’s always the threat of a breach. That’s why it’s important to remove or hash your API credentials before you push your source code to the repository.
API keys and secrets should never be sent over anything but an encrypted endpoint. Encrypting API keys will ensure that, even if they are intercepted in transit, potential hackers are unable to use them. You should encrypt your API keys in the HTTP header, the POST body, or within the call to the JavaScript (or in the script itself).
The final best practice for API key management is to delete credentials once you’re done using them. Forgotten API credentials are an easy target for hackers because they’re often left unintentionally exposed. For example, when a user leaves the company, their API keys may not be automatically deleted. Just as you would collect physical access passes or building keys, you should always remove API credentials as part of the employee offboarding process.
If you’re not sure whether an API key is still being used by a critical process, you should try disabling it first to see if anything breaks. If not, you can safely delete it.
Following best practices for API key management will increase the security of your APIs while still allowing full functionality. API credential management is also a core component of a comprehensive secrets management strategy. A secrets management tool makes it easier to follow API key management best practices at scale without sacrificing the speed required for DevOps and CI/CD development.