Prevent API key leaks by shortening credential exposure, narrowing permissions, and configuring alerts with loss mitigation, covering temporary API keys, regular rotation, IP whitelists, private connections, budget alerts, and model monitoring alerts.
Overview
An API key is the credential used to call Alibaba Cloud Model Studio model services. Once leaked, anyone who obtains it can call the models within the key's authorization scope and consume your quota. The impact of a leak can be broken down into four factors: leak probability, exposure time, permission scope per call, and how long the anomaly lasts. Leak probability is compressed by the foundation practices throughout this page, and the other three factors are compressed by the following three layers respectively:
Layer | Factor compressed | Key measures |
|---|---|---|
Shorten exposure time | Exposure time | Temporary API keys for clients, one-time display with controlled storage, regular rotation and revocation on departure |
Narrow permission scope | Permission scope per call | One key per business, workspace isolation, IP whitelist and model scope, private connections |
Alerts and loss mitigation | How long the anomaly lasts | Budget alerts with auto-stop, model monitoring alerts, platform risk detection |
- Applications read credentials from environment variables or a key management service instead of hard-coding them; configure sensitive information scanning in CI/CD (with credential scanning tools such as gitleaks), covering API key patterns.
- Maintain a key ledger: start with a basic table of key IDs, purposes, owners, and last rotation dates, then gradually add regions, workspaces, permission scopes, and budgets; never store complete keys in the ledger.
- Restrict key management permissions (create, view, edit, delete) to the administrators who need them.
Shorten the credential exposure time
Store long-term API keys securely
A newly created API key is displayed in full only once at creation; after you close the dialog, the plaintext can no longer be viewed or copied. If you did not save it, you can only reset or recreate the key. See Obtain an API key.
After creation, store the complete key directly in a controlled credential store (a key management service or the secret management of your deployment platform) instead of relaying it through screenshots, emails, chat tools, or ordinary documents; the masked value in the list is only for identification and comparison.
Never place long-term API keys in the following locations:
- Browser scripts, frontend build artifacts, and mobile app packages
- Public or shared code repositories (adding .gitignore alone does not remove credentials from historical commits)
- Notebook outputs and sample code
- Screenshots and screen recordings
- Tickets, group chats, and online documents
- Prompts, knowledge bases, and agent contexts
- Request URLs and query parameters
- Access logs and exception stacks
- Third-party plug-ins and model relay services (services that forward your model calls)
- Shared environments and externally exposed configuration files, images, and backups
Use temporary API keys in client-side scenarios
Browsers, mobile apps, and mini programs cannot keep credentials reliably confidential. In these scenarios, have an authenticated backend issue temporary API keys for the frontend to call directly:
- Keep the long-term API key only on your own server and never deliver it to any client.
- When the frontend needs to call a model, request a temporary key from your backend first.
- The backend calls the temporary API key generation API and returns a short-lived st- key to the frontend.
- The frontend calls Model Studio directly with the st- key.
Rotate keys regularly and revoke them on departure
Long-lived keys configured in code or agents accumulate exposure over time and should be rotated regularly:
- Create a new key and reconfigure its permissions and budget for current needs.
- Update credential references in your business services.
- Verify that the new key works.
- Delete the old key and confirm it can no longer initiate calls.
- Update the key ledger.
Narrow the credential permission scope
One key per business and workspace isolation
Assign independent API keys to different applications and environments (production, testing, and personal debugging): this shrinks the impact of a single key leak and makes it easier to trace issues and rotate keys per key. When you need permission isolation, fine-grained model access control, or independent cost accounting, assign a non-default workspace to the business (a workspace is a container in Model Studio that isolates permissions and costs; every account has a default workspace by default); the base calling scope of a key is constrained by its workspace. See Permission management.
Also restrict key management permissions to the necessary administrators; accounts that can log in to the console to manage credentials should follow least privilege and enable multi-factor authentication as required by your organization.
Configure the IP whitelist and model scope
After setting the permission of an API key to Custom in the key creation or edit panel, you can further narrow its calling scope:
- IP whitelist: allow only the public egress IPs or CIDR blocks actually used by the business, up to 20 entries (use a service such as curl ifconfig.me to find your public egress). All IPv4 addresses are allowed by default; creating a key alone does not mean source restrictions are in effect. When calling through NAT, a proxy, or a gateway, enter the public egress address that Model Studio actually sees, not the intranet address of the server.
- Model scope: once enabled, the key can only call the selected models (up to 30, constrained by the models authorized in the workspace), preventing leaks from being used for high-cost models.
Call through a private connection
For scenarios with high network security requirements, you can establish a private connection (PrivateLink) between your VPC and Model Studio: calls bypass the public network, so external attackers whose networks are not connected cannot call the service even if the key leaks. For configuration, see Access Model Studio APIs over a private network.
Configure alerts and loss mitigation
Budget alerts and auto-stop
Set monthly budgets and alert thresholds for the account, a workspace, or a single API key; prioritize independent budgets for high-risk keys such as development, testing, demos, and third-party integrations. When spending reaches the threshold and when the budget is exhausted, notifications are sent via in-site messages, SMS, email, or bots; after enabling stop on budget exhaustion, the service restricts pay-as-you-go calls for that dimension and returns 429. For configuration, see Budget management.
Model monitoring alerts
Model monitoring aggregates call counts, token usage, failure counts, and failure rates per workspace, supports filtering by API key, and lets you create alert rules from preset or custom alert templates that notify contacts or contact groups. Configure call-volume or call-count surge alerts for high-risk keys: anomalies become visible within minutes after the calls occur, and off-hours triggers call for manual intervention. First-time alert configuration requires authorizing the Cloud Monitor service role under data delivery; for configuration, see Alerts.
Platform risk detection
The platform periodically scans public environments such as public code repositories to identify leaked API keys and proactively notifies you; the actual channels depend on what is enabled (which may include SMS, email, and in-site messages). It also identifies anomalous behavior such as logins from unfamiliar IPs, off-hours traffic spikes, and abnormal cost surges.
Pre-launch checklist
Individual developers and small teams can start with the minimum: set a budget with alert notifications for the key, keep the credential only in environment variables, and rotate it regularly. The full checklist is as follows:
- Production, testing, and third-party integrations do not share the same API key; key owners and purposes are clear.
- Region, workspace, model scope, and request egress IPs are verified, with permissions covering only the necessary scope.
- Long-term keys are stored only in controlled backends and never enter clients, source code, prompts, shared documents, or logs.
- Keys have not been delivered to untrusted third-party relay services or plug-in environments; if they were, delivery has stopped and keys have been rotated.
- Credentials are injected through environment variables or secrets; sensitive files and build artifacts contain no plaintext.
- Client-direct scenarios use short-term keys issued by an authenticated backend, and the issuing long-term key has narrowed permissions.
- New keys are stored under control; old keys, idle keys, and keys of departed personnel have been reviewed.
- Budgets and alerts at the account, workspace, and key levels are configured, with notification channels and owners clarified.
- The availability impact and effective delay of stop on budget exhaustion have been evaluated.
- Rotation and leak response procedures are defined; investigation steps are described in API key leak investigation.
FAQ
Do temporary API keys have fewer permissions than long-term API keys?
Do temporary API keys have fewer permissions than long-term API keys?
With an IP whitelist configured, do I still need to worry about key leaks?
With an IP whitelist configured, do I still need to worry about key leaks?
What should I do if I have given a key to a third-party relay service?
What should I do if I have given a key to a third-party relay service?