What Is an API Key? (And How to Keep It Safe)
An API key is a secret that identifies your app to an API. Here's what it does, why APIs use them, and how not to leak yours.
An API key is a secret string — often something like sk_live_8f3b91c0a4e7 — that you send with every request to identify your application to an API. It answers the service’s first two questions: who is calling? and are they allowed to?
What the key is for
- Authentication — proving the request comes from a registered account.
- Rate limiting — counting your calls so the service can enforce ‘1,000 per day’.
- Billing and quotas — attributing usage to the right plan.
- Access control — some keys are scoped to specific endpoints or permissions.
- Analytics and abuse handling — the service can see which key is misbehaving and disable just that one.
How you send it
There is no single standard. Read each API’s auth docs for the exact method:
| Query parameter | ?api_key=YOUR_KEY — simplest, but the key ends up in server logs and browser history. Common on read-only data APIs. |
| Custom header | X-API-Key: YOUR_KEY or apikey: YOUR_KEY — keeps the key out of the URL. |
| Authorization header | Authorization: Bearer YOUR_KEY — the most common modern style, shared with OAuth tokens. |
| Basic auth | The key as the username (empty password), base64-encoded in Authorization: Basic. |
# the same request, key in a header (preferred) curl https://api.example.com/v1/data \ -H "Authorization: Bearer $API_KEY" # key in the query string (avoid if you can) curl "https://api.example.com/v1/data?api_key=$API_KEY"
API key vs. token vs. OAuth
- An API key is usually long-lived, tied to your account or project, and used for server-to-server calls.
- An access token (e.g. from OAuth) is short-lived and often represents a specific user who granted your app permission.
- Some APIs use a key to get a token: you send your permanent key/secret once and receive a temporary bearer token for subsequent calls.
Keeping your key safe
- Never commit keys to git or ship them in front-end JavaScript or a mobile app binary — anyone can read them there.
- Load keys from environment variables or a secrets manager, not from source code.
- Proxy third-party API calls through your own backend so the key stays server-side.
- Scope keys to the minimum permissions, and use separate keys for development and production.
- Rotate a key immediately if it leaks, and set spend limits where the API supports them.
Common errors
- 401 Unauthorized — key missing, wrong, revoked, or sent in the wrong place (header vs. query).
- 403 Forbidden — the key is valid but not allowed to use this endpoint or plan feature.
- New key not working — some APIs take minutes to hours to activate a fresh key.
- Trailing whitespace — a newline copied with the key is a classic cause of 401s.
FAQ
Is an API key a password?
It plays a similar role — a secret that grants access — but it identifies an application or project rather than a person, and it is designed to be used programmatically and rotated easily.
Can I use an API key in front-end code?
Only if the key is meant to be public (some map and analytics keys are, and are locked to specific domains). For anything secret, call the API from your backend so the key never reaches the browser.
What do I do if my key leaks?
Revoke or rotate it in the provider’s dashboard immediately, then update your app with the new key. Check usage logs for unexpected activity.
Why does my brand-new key return 401?
Besides typos and whitespace, some APIs (e.g. OpenWeatherMap) need time to propagate a new key — wait and retry before assuming it is broken.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ