Skip to content

API Authentication Explained: Basic Auth, Session Cookies, Bearer Tokens and JWT

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

An API that changes orders, shows a balance or sends messages has to know who is asking. HTTP itself remembers nothing between requests, so every request must carry proof: a password, a cookie that points to a session, or a token. This lesson walks through the three approaches you will meet in real projects, in the order they appeared. You will see the exact Authorization headers on the wire, find out why Basic auth is only safe over HTTPS, compare server sessions with tokens, and build and verify a JSON Web Token in your browser so that its strengths and limits are clear rather than magic.

Quick answer

Authentication proves who is calling; authorization decides what they may do. A protected API answers 401 with a WWW-Authenticate header when proof is missing or wrong, and 403 when the caller is known but not allowed. Proof travels as Authorization: Basic ... (username and password in base64, not encrypted), as a session cookie that points to data stored on the server, or as Authorization: Bearer <token>. A JWT is a signed token whose payload anyone can read; the server must verify its signature and expiry on every request.

40 min read and practice · Content checked on 30 November 2026

In this lesson you will

  • Tell authentication from authorization and 401 from 403
  • Send Basic and bearer credentials and choose between sessions and tokens
  • Read a JWT, verify its signature and avoid the common JWT mistakes

401, WWW-Authenticate and Basic auth

Authentication answers "who are you?" and authorization answers "are you allowed to do this?". HTTP has a status code for each failure. 401 Unauthorized, despite its name, means not authenticated: send credentials and try again. The server says which kind it accepts in WWW-Authenticate, here Basic realm="Fake Realm". 403 Forbidden means the server knows who you are and the answer is still no, so sending the same credentials again will not help.

Basic auth is the oldest scheme: the client joins username and password with a colon, encodes the result in base64 and sends it in every request. The recording shows ali:s3cret becoming YWxpOnMzY3JldA== and base64 -d turning it straight back. Base64 is an encoding, not encryption, so anyone who sees the request sees the password. That is acceptable only over HTTPS, and mostly for scripts and internal tools. Browsers also show their own grey login box for it, which you cannot style, and there is no clean way to log out.

Example: Basic auth with the wrong and the right password
Real output: curl -i -u ali:wrong https://httpbin.org/basic-auth/ali/s3cret (date removed)
HTTP/2 401
access-control-allow-credentials: true
access-control-allow-origin: *
access-control-expose-headers: Retry-After, RateLimit, RateLimit-Policy, WWW-Authenticate
content-length: 0
www-authenticate: Basic realm="Fake Realm"
x-content-type-options: nosniff
Real output: curl -s -i -u ali:s3cret https://httpbin.org/basic-auth/ali/s3cret (status and body)
HTTP/2 200
{
  "authenticated": true,
  "user": "ali",
  "authorized": true
}
Real output: the header curl sent (from curl -v), then decoding it
> Authorization: Basic YWxpOnMzY3JldA==
$ echo YWxpOnMzY3JldA== | base64 -d
ali:s3cret

The password is a demo value for httpbin, which accepts whatever user and password you put in the URL. Never paste real credentials into a test service.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 2 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

Should I store a JWT in localStorage or in a cookie?

For a website calling its own API, an HttpOnly, Secure, SameSite cookie is safer because scripts cannot read it. localStorage is easier for APIs on other domains but exposes the token to any XSS bug, so keep such tokens short-lived.

How do I log a user out when I use JWTs?

Delete the token on the client, and on the server keep access tokens short and revoke the longer-lived refresh token. For instant logout you need a server-side denylist, which brings back some of the state that sessions already had.

Is OAuth the same thing as JWT?

No. OAuth 2.0 is a process for getting an access token, for example "Sign in with Google" or giving an app access to your calendar. JWT is one possible format for tokens. OAuth tokens may be JWTs or random strings.

How long should an access token last?

Common choices are 5 to 60 minutes, paired with a refresh token that lasts days or weeks and can be revoked. Shorter is safer because a stolen token stops working sooner.

Is an API key the same as a bearer token?

Both are secrets sent with each request, but an API key usually identifies a project or account and rarely expires, while a bearer token usually stands for a logged-in user for a short time. Keep API keys on the server, never in front end code.

Sources

Facts in this lesson were checked against these official pages on 30 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.