API Directory โ€บ Guides โ€บ API Authentication Explained: Keys, Tokens & OAuth

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 authPublic 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 headerX-API-Key: KEY, apikey: KEY, or a vendor-specific name. Keeps the key out of the URL.
Bearer tokenAuthorization: Bearer TOKEN. Used by OAuth access tokens and many modern key-based APIs.
Basic authAuthorization: Basic base64(user:pass). Sometimes the API key is the username with an empty password.
OAuth 2.0A consent flow that yields a short-lived bearer token representing a specific user. See the OAuth guide.
HMAC request signingYou sign each request (method + path + body + timestamp) with a secret. Used by AWS, some exchanges and payment APIs. Prevents replay and tampering.
mTLS / client certificatesBoth sides present TLS certificates. Banking and high-security B2B APIs.

Reading the docs correctly

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

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

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’