A simple secret that tells an API which app is calling, and how to handle it safely.
An API key is a long, random string that a program sends with each request, to tell the API which app is calling. It is the simplest way to identify callers, track their use and set limits for each one.
When a company opens an API to other developers, it needs to know who is calling. The developer signs up, and the company creates a key for their app. The app then adds the key to every request, usually as a header such as X-API-Key or Authorization.
GET /v1/forecast?city=Pune Host: api.example.com X-API-Key: sk_live_9fA3... (a long random value)
On the server, the key is looked up. If it is valid, the server knows who the caller is, what it may do (its scope) and how much it may do (its rate limit and quota). That is enough for many machine-to-machine cases.
| Place | Example | Verdict |
|---|---|---|
| Request header | X-API-Key: ... or Authorization: Bearer ... | Best. Headers are not stored in browser history and are usually kept out of access logs. |
| Query string | /forecast?api_key=... | Avoid. The key ends up in logs, browser history, bookmarks and Referer headers. |
| Request body | A field inside a JSON body | Possible for POST calls, but unusual, and the key is not available to a gateway before the body is parsed. |
| Cookie | Cookie: key=... | Only for browser sessions. That is a session, not an API key. |
| Credential | Identifies | Lifetime | Best for |
|---|---|---|---|
| API key | An app or customer account | Long, until rotated or revoked | Simple identification, quotas, server-to-server calls |
| OAuth2 access token | An app acting for a user, with approved scopes | Short (minutes to hours) | Acting on behalf of users |
| JWT | A signed set of claims (user, app, expiry) | Short | Passing identity between services |
| mTLS client certificate | A machine, proven by a certificate | Months | Strong service-to-service identity |
| Signed requests (HMAC) | A caller, proven by a signature on each request | Per request | Proving the message was not changed |
Illustrative numbers only: 100,000 requests per hour without a limit, 1,000 with one, 500 customers. On average a key leaks half way through its life, so a shorter life cuts the exposure window. The model does not cover attackers who copy a key and wait.
| Term | Meaning in easy words |
|---|---|
| API key | A long random string that identifies the calling app to an API |
| Scope | The list of things a key is allowed to do (for example, read only) |
| Rate limit / quota | How many calls a key may make per second or per month |
| Key prefix | A short, non-secret start of the key that helps people recognise it |
| Hashing a key | Storing a one-way fingerprint of the key, not the key |
| Rotation | Replacing a key with a new one, ideally with a period when both work |
| Revocation | Cancelling a key so it stops working at once |
| Blast radius | How much damage is possible if one credential leaks |
| Least privilege | Give a key only the access it needs, and no more |
| Secret scanning | Tools that search code and repositories for leaked keys |
| Test key / live key | A key for a sandbox, and one for real data |
| Choice | We gain | We lose |
|---|---|---|
| Long-lived keys | Nothing to renew, simple for clients | A leak is dangerous for a long time |
| Short-lived keys / tokens | A smaller window for attackers | Clients must refresh, and need more code |
| Broad scope | One key does everything | A leak exposes everything |
| Narrow scope | A leak does little harm | More keys to create and manage |
| Hash on the server | A stolen database is not a list of working keys | You can never show a key again, so lost keys must be replaced |
| Strict per-key limits | Protects the service and other customers | Legitimate bursts get refused |