Skip to main content
Welcome back. In this lesson you’ll learn what API keys are, how they authenticate requests, and practical steps to protect them from misuse.

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.
If an attacker obtains your API key, they can make requests that will be billed to your account and potentially access sensitive data.

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.
Do not hard-code your key into source files; instead use secure configuration or secret stores (examples below).

Creating an API key (OpenAI web UI — example flow)

  1. Open your account settings (cogwheel / settings).
  2. Select API Keys from the left-hand menu.
  3. Click Create new secret key.
  4. Give the key a descriptive name (for example, MyTestAPI).
  5. Choose the project and permissions (for example, Default project, minimal permissions), then create the key.
A very important detail: OpenAI only displays the secret key once at creation. If you leave the screen without copying it, you must delete and recreate 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):
Python usage:
For CI systems and production, prefer a secrets manager integrated into your build/deployment pipeline rather than environment variables if supported by your platform.

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.
References and further reading

Watch Video