Skip to main content

API Keys - Service Authentication Strings

About 2 min read

An API key is a unique string used to identify the caller in communication between applications. When accessing an API provided by a web service or cloud platform, including the API key in the request allows the server to determine "who is calling this API." Strictly speaking, however, an API key is a means of identification rather than authentication, and you need to understand that it is fundamentally different from the fine-grained access control offered by tokens such as OAuth.

How API Keys Work

There are mainly two ways to send an API key. The first is to include it in the request header, embedding it in an HTTP header such as <code>Authorization: Bearer sk-xxxx</code> or <code>X-API-Key: xxxx</code>. The second is to attach it to the URL as a query parameter (<code>?api_key=xxxx</code>), but this is no longer recommended for security reasons because the key remains in server logs and browser history. Even with the header method, if the communication is not encrypted with TLS, the key flows in plaintext, so the use of HTTPS is a basic prerequisite.

The Difference Between API Keys and OAuth Tokens

An API key is an identifier indicating "which application this request came from," and it is usually a fixed string. An OAuth access token, on the other hand, is a dynamic token that expresses "which user granted authorization, with which scope (range of permissions), and until when it is valid." If an API key leaks, there is a risk that every operation tied to that key can be executed without limit, whereas an OAuth token can localize the scope of damage through limited scopes and expiration times. In practice, it is common to use an API key for rate-limiting access to a public API, and an OAuth token for accessing user data.

The Reality of Key Leakage Incidents

API key leakage is one of the most frequent security incidents in development environments. There is no end to cases where someone commits an AWS access key to a public GitHub repository, only for it to be detected within minutes by automated scanning bots and abused for cryptocurrency mining. In 2023, a company's pasting of an API key into a Slack message led to a large-scale data breach. AWS has introduced a mechanism that automatically detects and revokes leaked keys on GitHub, but there are still cases where damage occurs in the brief window before detection. In the startup security checklist, too, key management is listed as a top-priority item.

Best Practices for Secret Management

Writing an API key directly into source code (hardcoding) is the most dangerous anti-pattern. Instead, the minimum measure is to manage keys with environment variables (a <code>.env</code> file) and exclude them from Git tracking with <code>.gitignore</code>. For more robust operations, use a secret management service such as HashiCorp Vault or AWS Secrets Manager. These services provide features such as encryption at rest for keys, access logging, and automatic rotation, allowing the entire key lifecycle to be managed securely. As a basic principle of key management, it is important to restrict the people and systems that can access keys to the least privilege.

Practical techniques for secret management are also explained systematically in API security books on Amazon.

The Importance of Rotation

API keys should be regularly replaced with new ones (rotation). In many cases, a key has leaked without anyone noticing, and regular rotation functions as insurance that limits the period of damage from such "unnoticed leaks." AWS and Google Cloud recommend rotation every 90 days, and they also provide automatic rotation features. During rotation, providing a "grace period" in which both the old and new keys remain valid for a certain time prevents service interruptions. Please also refer to the detailed guide to API key management.

Common Misconceptions

The misconception that "security is airtight as long as you have an API key" is persistent, but an API key is merely an identifier and does not provide sufficient protection on its own. In addition to the API key, defense in depth that combines IP address restrictions, rate limiting, referrer checks, and the like is essential. Moreover, embedding an API key in front-end JavaScript lets anyone view it through the browser's developer tools, so as a principle the design should handle keys only on the server side.

Related Terms

Was this article helpful?

XHatena