What is an API key?
An API (Application Programming Interface) is a contract between software components that enables one application to access functionality or data exposed by another. When you publish functionality via an API, other systems call that API to integrate those features. For example, you might publish an endpoint that accepts a shopping list and returns vendors offering the lowest prices; an e-commerce site can call that endpoint to display daily deals. An API key is a secret token issued to a client or developer to:- Identify the calling application.
- Authenticate and authorize API requests.
- Track and monitor usage for billing and auditing.
How API keys are used — example (OpenAI + Python)
The example below demonstrates a simple chat completion request using OpenAI’s SDK. API keys authenticate this request so the provider can attribute usage to your account.Creating an API key (OpenAI web UI — example flow)
- Open your account settings (cogwheel / settings).
- Select API Keys from the left-hand menu.
- Click Create new secret key.
- Give the key a descriptive name (for example,
MyTestAPI). - Choose the project and permissions (for example, Default project, minimal permissions), then create the key.
Do not share your API key or expose it in client-side/browser code. Anyone with the key can make requests that incur costs on your account.
Best practices to protect API keys
Below are recommended security controls and operational practices to reduce risk.Example: store an API key in an environment variable and use it in Python
Unix / macOS (bash/zsh):Additional operational tips
- Use your provider’s dashboard (for example, the OpenAI usage dashboard) to review usage, set spending limits, and configure alerts: https://platform.openai.com/account/usage
- When demoing publicly or in sample code, use restricted test keys or mocked endpoints to avoid exposing real credentials.
- Limit request rates and set quotas where your API provider supports them.
- Revoke any key immediately if you suspect it has been leaked or abused.
Store API keys in secure secret stores or environment variables and restrict them to server-side use. Regularly rotate and monitor keys to reduce the risk of unauthorized usage and unexpected charges.
Summary
API keys are simple but powerful credentials that authenticate and identify callers to your APIs. Treat them like passwords:- Generate keys in a secure workflow.
- Store keys server-side or in managed secret stores.
- Grant minimal permissions and rotate keys regularly.
- Monitor usage and enforce limits or network restrictions.
- OpenAI API docs: https://platform.openai.com/docs/introduction
- AWS Secrets Manager: https://aws.amazon.com/secrets-manager/
- Azure Key Vault: https://azure.microsoft.com/en-us/services/key-vault/
- Google Secret Manager: https://cloud.google.com/secret-manager
- HashiCorp Vault: https://www.vaultproject.io/