API Key Management: How to Obtain, Store, and Secure API Keys
This guide explains how API keys work and walks through the standard process for obtaining them, followed by practical strategies for secure storage, least-privilege permission design, environment isolation, key rotation and revocation, and monitoring. It also lists common mistakes to avoid, helping developers build a repeatable key management workflow that reduces the risk and impact of credential leaks.
API Key Management: How to Obtain, Store, and Secure API Keys
When integrating with cloud services and open platforms, an API Key is the first credential used to identify the caller and verify access permissions. Whether you are calling a large model inference service, object storage, a message queue, or log and monitoring capabilities, almost every developer eventually runs into the same flow: get a key first, then make the call. In practice, however, key management is a frequent source of security incidents. Hardcoding keys in source code, committing them to version control, shipping them inside frontend bundles, and never rotating them can all turn controlled access into a public entry point.
This article takes a neutral, general perspective and covers how API keys are obtained, how they should be stored, how permissions should be designed, and what operational mechanisms should be in place, so you can build a key management system that works in real projects.
1. What an API Key Actually Is
An API Key is a uniquely identifying string generated by the server. It is carried in requests so the service can confirm who is making the call. It typically has three characteristics:
- Uniqueness: Different applications and environments should hold different keys, making the source easy to distinguish.
- Revocability: If a key is leaked, it should be disabled immediately without affecting other callers.
- Auditability: The server can track call volume and record access logs per key.
It is important to note that an API Key addresses identity recognition and traffic attribution. It is not equivalent to a complete authentication and authorization system. Scenarios involving sensitive data or high-value operations usually require additional measures such as stronger signing mechanisms, short-lived tokens, and mutual verification.
2. The Standard Path to Obtaining an API Key
Console details differ across platforms, but the overall process is logically consistent and can be summarized in four steps.
Step 1: Register an Account and Complete Verification
Before using a cloud or open platform, you need to register an account and complete identity verification. Organization accounts may also need to provide company information to support member management and cost allocation later.
Step 2: Open the Target Product in the Console
Locate the relevant product category in the console (for example, object storage, message queue, container platform, log service, or monitoring and alerting) and confirm that the service is enabled. Some capabilities require initialization within a project or resource directory first.
Step 3: Create the Key and Bind Permissions
Create a new key on the “Access Credentials” or “Key Management” page. The most critical part of this step is permission binding: prefer a least-privilege policy that grants only the resource scope the application actually needs to read and write, rather than applying full permissions by default.
Step 4: Record the Key ID and Key Value
After creation, the page usually displays two pieces of information: a key ID (used to identify which key it is) and a key value (the actual sensitive string). The key value is often shown in full only once at creation time and cannot be viewed again after the page is closed, so it must be saved securely right away.
A schematic call structure looks like this (pseudocode, showing the call shape only):
request = {"endpoint": "<service endpoint>","headers": {"X-Key-Id": "<key ID>","Authorization": "Bearer <key value>"},"body": { ... }}
3. Where to Store Keys: A Tiered Storage Strategy
Obtaining a key is only the starting point; how you store it determines your security level. A widely accepted practice is to store keys by environment tier:
- Local development: Use local environment variables or a local configuration file, and explicitly add it to the version control ignore list.
- Test and staging: Store keys in the platform’s configuration management capability, with namespaces isolated by environment.
- Production: Use a dedicated key hosting and credential management capability, read keys dynamically at runtime, and never persist them as plaintext files.
Here is a counter-example and the corrected approach:
# Bad example: hardcoded key committed to version controlAPI_KEY = "abcd1234efgh5678"# Improved: read from the runtime environment, no plaintext in codeAPI_KEY = os.environ["MY_SERVICE_KEY"]
The test is simple: if a key can be found in a code repository, an image layer, a frontend build artifact, or a log, it has already been exposed.
4. Permission Design: Least Privilege and Environment Isolation
The risk of a key depends on the scope of permissions bound to it. Follow three principles:
- One key per application: Different business modules should not share keys, so a leak can be pinpointed and the affected key disabled precisely.
- One key per environment: Development, test, and production keys should never be reused, preventing test-scoped permissions from crossing into production.
- Separate read and write access: Do not grant write permissions for read-only scenarios. High-risk operations such as deletion should be authorized and approved separately.
For organizations with multiple teams and business lines, hierarchical authorization based on resource directories or project groups can keep permission boundaries aligned with organizational boundaries.
5. Rotation and Revocation: Minimizing the Exposure Window
Treat keys as things that expire. A mature operational process includes:
- Scheduled rotation: Replace keys on a fixed cycle (for example, quarterly), with a short overlap period during which old and new keys coexist so the switch is seamless.
- Immediate revocation: As soon as abnormal calls are detected, disable the key first, then investigate the source.
- Two-person review: Creation and destruction of high-privilege keys should go through an approval flow to avoid single-point mistakes.
The recommended rotation order is: create a new key → gradually update caller configurations → observe the call volume shifting over → revoke the old key. Never delete the old key before creating the new one, or calls will be interrupted.
6. Monitoring and Alerting: Making Abnormal Calls Visible
Key security cannot rely on policy alone; it also depends on observability. Set up the following monitoring items per key:
- Sudden spikes in call volume or calls from unexpected regions;
- A rise in authentication failures within a short period;
- Records of unexpected resources being accessed;
- Keys unused for a long time (possibly leftover credentials that should be cleaned up).
You can use a log service for centralized collection and a monitoring and alerting capability to set threshold notifications, forming a closed loop of detect, locate, and respond.
7. Common Mistakes to Avoid
- Putting keys in frontend code or mobile app packages: any client artifact can be reverse-engineered, so keys must stay on the server.
- Printing keys to logs: logs are often visible to a much wider audience than expected.
- Using one key across all environments: a single leak compromises everything.
- Never rotating keys: the longer a key lives, the higher the chance of exposure.
- Granting full permissions for convenience: this is the most common source of risk.
Conclusion
Obtaining an API Key takes only a few minutes. What really determines your security level is the set of engineering constraints applied afterward: least-privilege binding, environment-isolated storage, periodic rotation, revocability, auditability, and alerting. Making these the default workflow rather than an after-the-fact fix keeps development efficient while keeping the impact of credential leaks within an acceptable range. For teams using cloud platforms such as Baidu AI Cloud, incorporating key management into unified configuration and operational standards is a fundamental part of keeping services running reliably.
