REST API API Keys
An API key is a long, random string that identifies the app calling an API. Weather services, map services, and payment platforms hand out keys to developers. The key tells the provider who sends each request, which lets it apply usage limits and track billing.
The Library Card Picture
A library card identifies a member. The librarian scans it, sees how many books the member holds, and applies the borrowing rules. The card does not prove that the carrier is the rightful owner, and a lost card lets anyone borrow in that name. API keys work the same way.
How a Key Travels
Developer ── signs up ──→ Provider Dashboard
Developer ←── key: sk_live_8f3a...c91 ── Provider Dashboard
App ── GET /forecast
X-API-Key: sk_live_8f3a...c91 ──→ API Server
│ looks up key
│ checks plan, limits, status
App ←── 200 OK + data ─────────────────── API Server
Where to Send the Key
Place the key in a request header, such as X-API-Key or Authorization.
GET /forecast?city=Pune X-API-Key: sk_live_8f3a...c91
Never place a key in the URL query string. URLs appear in browser history, server logs, proxy logs, and shared links. A key in a URL spreads to all of those places.
API Keys vs Tokens
| Feature | API Key | Access Token (OAuth or JWT) |
|---|---|---|
| Identifies | The app or project | A user and an app |
| Lifetime | Long, until rotated | Short, minutes to hours |
| Permissions | Plan or key settings | Scopes from user consent |
| Best for | Server-to-server and public data | User data and sign-in |
Keys suit simple use cases such as reading public data or billing by project. Calls that act on behalf of a user need tokens.
Designing a Good Key
- Generate at least 32 random bytes from a secure random source.
- Add a readable prefix, such as pk_test_ or sk_live_, so people spot the type and environment.
- Show the full key only once, at creation time.
- Store only a hash of the key in the database, as you would with a password.
Creation: random key ──→ show to developer once
└─→ store hash(key) + prefix + owner + scopes
Request: incoming key ──→ hash ──→ compare with stored hash
Key Settings
Let developers restrict each key. Restrictions limit the damage of a leak.
- Scopes: read only, or read and write.
- Allowed IP addresses or allowed website origins.
- Expiry date.
- Separate keys for testing and production.
Rotation and Revocation
Keys leak through code repositories, screenshots, and chat messages. Offer a dashboard button that creates a new key and keeps the old one active for a short overlap. Developers update their apps, and the old key then stops working. A leaked key needs an immediate revoke button.
Day 0 New key created (old + new both valid) Day 7 Old key disabled
Rate Limits and Usage Tracking
The key gives providers a handle for fair use. Apply limits per key, and return 429 Too Many Requests with a Retry-After header when a key exceeds its plan. Show usage charts in the dashboard so developers see their numbers before a bill arrives.
Status Codes
- 401 Unauthorized: the key is missing or invalid.
- 403 Forbidden: the key is valid and lacks permission for this action.
- 429 Too Many Requests: the key passed its limit.
Protecting Keys as a Developer
- Keep keys in environment variables or a secrets manager.
- Exclude key files from version control with an ignore rule.
- Avoid embedding secret keys in mobile apps or browser code.
- Scan repositories for accidental key commits.
Common Mistakes
Treating Keys as User Login
A key proves which app calls, and it says nothing about which person sits behind the screen. Add user authentication for private data.
One Key for Everything
A single key across all projects makes rotation painful. Issue one key per app and environment.
Plain Text Storage
A database leak exposes every customer key when keys sit unhashed. Store hashes.
