Что такое ключ API?
- Глоссарий IAM
- Что такое ключ API?
An Application Programming Interface (API) key is a random string of characters that identifies and verifies an application or user. It acts as a unique identifier and provides a unique token for authentication. An API is a set of rules or protocols that allows two or more systems and applications to communicate with each other. For a client to be granted access, the API key must be confirmed to ensure that only authorized clients can request and retrieve data.
API key vs API token: What’s the difference?
Both API keys and API tokens are used for authentication and authorization, but they differ in their structure and usage. An API token is a unique string of characters that identifies specific users, excluding applications and other entities. API tokens are also associated with user token-based authentication, which requires additional user-specific data such as information about the token type and the user’s permission rights.
Additionally, it is important to note that an API token has a set expiration date while an API key remains active unless it is manually changed. Because of this, organizations tend to prefer API tokens for authentication purposes because they offer greater security than API keys.
How API keys work
API keys are integrated into the API requests made by users or applications to verify their identity and specific permissions. Here’s a step-by-step overview of API keys in action.
- Generating an API key: Before accessing the API, the client must submit an initial key request to the API server. This typically involves the client providing the necessary credentials or some form of authentication to receive their unique API key.
- Including the API key in the request: Once the API key is obtained, the client must include it within the API request, which is typically part of the query string or in the request header.
- Verifying the API key: The API server will verify and process the key to ensure the key’s validity. If the key matches with the client’s database, the request will be approved. If not, the key is rejected.
- Receiving a response: After validating the API key, the API server will process the request and generate the expected response to the client.
Why are API keys used?
API keys are robust tools that offer security and visibility during the interaction with external services provided by an API. By employing API keys, organizations can enhance their security, automate tasks and control which users have access to APIs.
Enhance security
Cybercriminals tend to target web APIs because they are the gateways that transfer highly sensitive information such as a user’s credentials. With API keys serving to identify and verify each client accessing an API, they counter unauthorized access, hence reducing the risk of data breaches and other security threats.
Automate tasks
API keys automate a wide range of tasks, eliminating the need for manual intervention across several processes. Examples of automated tasks include obtaining data from external sources, regular reporting, integrating systems and monetization. Automating repetitive tasks enhances efficiency and productivity while also reducing human error.
Control access
An API key is a simple way to regulate which users have access to an API. It enables administrators to grant or revoke access privileges based on the specific needs of each application. With organizations limiting the exposure of sensitive information, organizations boost security measures while also improving their visibility over data usage.
When to use an API key
Organizations must understand when to use an API key because it tracks how the API is being used, which is essential to maintain the security of your applications. The most common cases for API keys include blocking anonymous traffic, controlling API calls and filtering logs.
Block anonymous traffic
An API key helps block anonymous traffic by acting as a unique identifier for each authorized user accessing an API. For an authorized user to complete the authentication process, the API key must be included for the API provider to track access. With API keys being a requirement in every request, anonymous traffic can be blocked. This ensures that only authenticated users are granted access to the API’s resources.
Control API calls
An API call is the process of a user or application requesting access to data from the API server. API keys can limit the number of calls made to the server, which helps ensure the optimal performance of an API system while also protecting the system from threat actors. It is important to control the number of calls made to an API because a high volume of API requests can overload the server.
Filter logs
An API server can filter logs based on the specific API keys used in requests. The associated key is logged when a request is made to an API endpoint which allows administrators to trace the source of the request, identify the client making the request and monitor their activity. By filtering logs, administrators gain valuable insights that help address troubleshooting issues and optimize performance.
API key best practices
Here are four best practices organizations must follow to secure their API keys.
Securely store the API key
Treat API keys like your personal passwords. Avoid writing down your API keys on a sticky note or a spreadsheet. Instead, store them in a secrets management system or encrypt the keys using a robust encryption algorithm to prevent them from becoming compromised.
Use HTTPS for API requests
HTTPS uses Transport Layer Security (TLS) encryption, which is a standard practice that ensures data security for communication between applications. It is a best practice for an API key to be accessible over HTTPS since it prevents threat actors from stealing the API key during transmission.
Use a different API key for each application
Using different API keys for each application reduces the risk of multiple applications being affected at once. This ensures that if an API key from one application is compromised, then other applications will remain safe.
Rotate and delete API keys
Securely managing your API keys involves implementing a key rotation strategy, usually on a 30, 60 or 90-day basis. Regularly rotating keys reduces the risk of compromise because cybercriminals will have a limited window of opportunity. Unlike API tokens, API keys remain active unless the user manually regenerates or deletes them, so it is essential to maintain this security practice. Furthermore, consider deleting unnecessary API keys when they are no longer in use to optimize storage capacity and prevent unauthorized access.