API Authentication Explained: Keys, Tokens & OAuth
Why some APIs need a key and others don't โ and the difference between API keys, bearer tokens and OAuth, in plain language.
Authentication is how an API knows who is calling. Different APIs use different schemes; the docs’ ‘Authentication’ section tells you which, and exactly where the credential goes. Getting this one detail wrong is the most common reason a first request returns 401 Unauthorized.
The schemes you will meet
| No auth | Public data APIs (weather, facts, images). Just send the request. Often still rate-limited by IP. |
| API key in the URL | ?api_key=KEY or ?token=KEY. Simple; leaks into logs and browser history. |
| API key in a header | X-API-Key: KEY, apikey: KEY, or a vendor-specific name. Keeps the key out of the URL. |
| Bearer token | Authorization: Bearer TOKEN. Used by OAuth access tokens and many modern key-based APIs. |
| Basic auth | Authorization: Basic base64(user:pass). Sometimes the API key is the username with an empty password. |
| OAuth 2.0 | A consent flow that yields a short-lived bearer token representing a specific user. See the OAuth guide. |
| HMAC request signing | You sign each request (method + path + body + timestamp) with a secret. Used by AWS, some exchanges and payment APIs. Prevents replay and tampering. |
| mTLS / client certificates | Both sides present TLS certificates. Banking and high-security B2B APIs. |
Reading the docs correctly
- Note the exact header name and prefix.
Authorization: Bearer,Authorization: TokenandAuthorization: KEY(no prefix) are all real and not interchangeable. - Check whether there is a separate secret as well as a key, and whether requests must be signed.
- Look for an account/user header some APIs require alongside the key (e.g. Edamam’s
Edamam-Account-User). - See if the credential goes to a different host for the token exchange (common with OAuth:
accounts.example.comvsapi.example.com).
Two worked examples
# 1. Bearer token curl https://api.example.com/v1/me \ -H "Authorization: Bearer $TOKEN" # 2. OAuth client-credentials: key+secret -> token -> call TOKEN=$(curl -s -X POST https://accounts.example.com/oauth/token \ -d grant_type=client_credentials \ -u "$CLIENT_ID:$CLIENT_SECRET" | jq -r .access_token) curl https://api.example.com/v1/data -H "Authorization: Bearer $TOKEN"
Why 401 happens
- Credential in the wrong place (header vs. query, wrong header name).
- Missing or wrong prefix (
Bearervs.Token). - Trailing newline or space copied with the key.
- New key not yet activated, or expired token not refreshed.
- Right key, wrong environment (test key against the production host).
- Clock skew breaking a signed/HMAC request — sync your system time.
Authentication vs. authorization
Authentication (401 when it fails) is who are you. Authorization (403 when it fails) is are you allowed to do this. A valid key hitting an endpoint outside your plan returns 403, not 401 — the fix is a plan upgrade or a differently-scoped token, not a new key.
FAQ
Where do I put my API credentials?
Wherever that specific API’s docs say — there is no universal rule. When you have a choice, prefer a header over the query string so the secret stays out of logs.
What is the difference between Bearer and Basic auth?
Basic sends a base64-encoded username:password (trivially decoded, so HTTPS-only). Bearer sends an opaque token that the server issued; the token, not a password, is the secret.
My requests work in Postman but fail in code — why?
Usually Postman is adding a header you forgot (often Authorization or Content-Type), or it is following a redirect that drops the auth header. Compare the raw request Postman sends with yours.
How is request signing different from a bearer token?
A bearer token is sent as-is on every call. With signing, you never send the secret — you send a per-request hash computed from it, so a captured request can’t be reused or altered.
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 โ