Token-Based Authentication: How It Works and When to Use It
Every API call needs proof of who's calling, and tokens are how modern APIs get it. This guide explains how token-based authentication works, the token types you'll meet, tokens vs sessions, how to validate, refresh and revoke tokens, and when to use them, with tested code.

TL;DR:
What is token-based authentication?
Token-based authentication is a way for a client to prove who it is by sending a token, a string issued by an authorization server, with each request instead of a password or session cookie.
The API checks the token, either by verifying its signature or by asking the server that issued it, then allows or denies the call.
Tokens expire, can be refreshed, and can be revoked.
In 2024, developers pushed 23.8 million new secrets, such as API keys and tokens, to public GitHub repositories, according to GitGuardian's State of Secrets Sprawl 2025 report. The same report found that 70% of secrets leaked in 2022 were still valid two years later.
That's the trade-off at the heart of token-based authentication. Tokens let any app, service or AI agent call an API without sharing a password. They also work for whoever holds them, so how long they live, what they allow and how fast you can kill them decide how much a leak costs you.
This guide explains how token-based authentication works, the token types you'll meet, how it compares with sessions, and how to validate, refresh and revoke tokens, with code we ran and tested.

What is token-based authentication?
Token-based authentication is an authentication method where the client sends a token with each request to prove its identity, instead of sending credentials every time. The token is issued once, after the client signs in or proves who it is, and is then reused until it expires.
So what is token based authentication in everyday terms? It works like a wristband at a festival. You show your ID once at the gate, you get a wristband, and every stage after that only checks the wristband. If you lose it, whoever finds it gets in, until the wristband expires.
Is token authentication the same as a hardware token?
No. "Token" means two different things in security. In API and web security, a token is a digital string such as a JWT or an opaque access token. In multi-factor authentication, a token is a physical device or app that generates one-time codes, such as a YubiKey or an authenticator app.
Both prove identity, but they solve different problems. A hardware token proves a person is present during sign-in. An access token proves, on every API call, that sign-in already happened. This guide covers the second kind: token authentication for APIs and applications.
How does token-based authentication work?
Token-based authentication works in six steps: the client authenticates once, receives a token, stores it, sends it with every request, the API validates it, and the token eventually expires, gets refreshed or is revoked. No password crosses the network after step one.
- Authenticate. A user signs in, or an application proves its identity with a client secret, certificate or signed assertion.
- Issue. The authorization server returns an access token, often with a refresh token and an expiry time.
- Store. The client keeps the token somewhere safe: server memory, a secrets manager, or an HttpOnly cookie for browser apps.
- Present. The client sends the token on each API call, usually in the
Authorization: Bearerheader. - Validate. The API verifies the token's signature and claims locally, or asks the authorization server whether it's active.
- Expire, refresh or revoke. When the token expires, the client gets a new one with its refresh token. If something goes wrong, the token is revoked.
Here's a token based authentication example. The client gets a token, then uses it:
The Bearer scheme comes from RFC 6750. It means possession is enough: anyone holding the token can use it. For more on that, see what is a bearer token.

What types of authentication tokens are there?
The main types of authentication tokens are access tokens, refresh tokens, ID tokens, API keys and personal access tokens. Each has a different job, lifetime and risk. Mixing them up is one of the most common token mistakes.
Two rules keep these straight:
- Access tokens go to APIs; ID tokens go to apps. An ID token proves a sign-in to the client. Using it to call an API skips the authorization server's checks on scope and audience.
- Refresh tokens never leave the client-to-authorization-server path. An API should never see one.
Access token vs refresh token is the pair you'll handle most: the access token is short-lived and used constantly; the refresh token is longer-lived, used rarely, and must be guarded more carefully because it can mint new access tokens. If you still rely on API keys, see our guide to API key management.
What is the difference between JWT and opaque tokens?
A JWT carries its claims inside the token and can be verified locally; an opaque token is a random string that only the issuer can look up. Both are access tokens. The choice decides how your API validates them and how fast you can revoke them.
For JWT internals, see what is a JWT. For implementing JWT sign-in end to end, see JWT authentication.
What is the difference between token-based and session-based authentication?
Session-based authentication keeps the user's state on the server and gives the browser a cookie with a session ID; token-based authentication puts what the API needs inside, or behind, a token the client presents. Sessions suit one web app on one domain. Tokens suit APIs called by many clients, services and partners.
The token vs session authentication choice isn't all-or-nothing. A common, safe pattern is a backend-for-frontend: the browser holds a session cookie with the backend, and the backend holds the tokens and calls the APIs. The browser never sees an access token, and the APIs still get one.
How should an API validate tokens?
An API validates a token either locally, by checking a JWT's signature and claims, or remotely, by asking the authorization server through token introspection (RFC 7662). Local checks are fast. Introspection is slower but always current. Most APIs need to understand both.
Here's an API module that does each:
The local check pins the algorithm and checks issuer, audience, token type and expiry. Skipping any one of those is how most JWT vulnerabilities happen. The introspection call sends the token to the authorization server, which answers {"active": true, ...} or {"active": false}.
How do you revoke a token?
Revoke an opaque token at the authorization server and it stops working on the next call; a JWT keeps working until it expires unless the API also checks a denylist. That difference is the main reason to choose one format over the other.
For JWTs, you have three options:
- Keep access tokens short, 5 to 15 minutes, and revoke the refresh token. The damage window is one token lifetime.
- Denylist the token's
jtiuntil its expiry, and check the list on each call. - Introspect high-risk calls only, such as payments and exports, and verify the rest locally.
RFC 7009 defines a standard revocation endpoint, so clients can revoke their own tokens at sign-out.
How do refresh tokens work, and why rotate them?
A refresh token lets a client get a new access token without the user signing in again; rotation means each refresh token works once and is replaced by a new one. If an old refresh token is ever used again, someone has copied it, so the server cancels the whole chain.
This is called refresh token rotation with reuse detection. The OAuth 2.0 Security Best Current Practice (RFC 9700) requires refresh tokens for public clients, such as browser and mobile apps, to be either sender-constrained or rotated. Here's the logic from our test authorization server:
Every refresh token belongs to a family that starts at sign-in. Using one marks it as used and returns a new pair in the same family. Replaying a used one revokes the family, which also locks out the legitimate client. That's intentional: the user signs in again, and the attacker's copy is dead.
What happened when we tested token validation, revocation and refresh?
We ran an authorization server, both validation methods and the refresh logic with Node.js 18+ and jose 5.9.6. Every result matched the behaviour described above:

The latency numbers come from one laptop with the authorization server on the same machine. Across a real network, introspection adds a round trip, often several milliseconds. That's why many teams verify JWTs locally for everyday calls and introspect, or use opaque tokens, for the calls where instant revocation matters most.
What are the pros and cons of token-based authentication?
Token-based authentication scales across services, clients and partners and never sends passwords to APIs, but tokens work for whoever holds them and self-contained tokens are hard to revoke. The pros win for APIs; the cons are manageable with short lifetimes and good storage.
Pros:
- No passwords on every call. Credentials are sent once, to the authorization server.
- Works for every kind of client: browsers, mobile apps, back-end services, partners and AI agents.
- Scales without shared session storage. JWTs verify anywhere with the issuer's public key.
- Carries permissions. Scopes and claims limit what each token can do. See OAuth scopes.
- Expires on its own. A short lifetime caps the damage of a leak.
Cons:
- Bearer tokens work for whoever holds them, unless they're bound to a key with mTLS or DPoP.
- JWTs are hard to revoke before they expire.
- Token storage in browsers is risky; scripts can read tokens in
localStorage. - Tokens leak in places passwords don't, such as logs, URLs, screenshots and code repositories.
- More moving parts: an authorization server, keys, rotation and refresh logic.
When should you use token-based authentication?
Use token-based authentication for any API that's called by more than one client, by other services, by partners or by AI agents. Use sessions for a single server-rendered web app, and keep API keys for identifying applications on low-risk APIs.
Token-based authentication best practices
The most important token-based authentication best practices are short lifetimes, strict validation, safe storage, rotation and the ability to revoke. These apply whether your tokens are JWTs or opaque strings.
- Send tokens only over HTTPS, and only in the
Authorizationheader. Never put tokens in URLs or query strings, where they end up in logs and browser history. - Keep access tokens short-lived: 5 to 15 minutes for user-facing apps, up to an hour for machine-to-machine.
- Validate everything: signature with a pinned algorithm, issuer, audience, expiry and token type. Never trust a token you've only decoded.
- Scope every token to the API and operations it needs. A token for the orders API must fail on the billing API.
- Rotate refresh tokens and detect reuse, as RFC 9700 requires for public clients.
- Keep tokens out of browser JavaScript. Use a backend-for-frontend with HttpOnly, Secure, SameSite cookies, not
localStorage. - Use PKCE for every authorization code flow, and never the implicit flow, which OAuth 2.1 removes.
- Bind high-value tokens to the client with mTLS (RFC 8705) or DPoP (RFC 9449), so a stolen token is useless alone.
- Make revocation possible: opaque tokens or introspection for high-risk APIs, a
jtidenylist or short lifetimes for JWTs. - Scan code and logs for leaked tokens, and rotate anything that leaks immediately.
- Give each AI agent its own short-lived, narrowly scoped token, and use token exchange (RFC 8693) when it acts for a user. See securing MCP endpoints.
These are the token side of the principle of least privilege: every token should allow as little as possible, for as short a time as possible.
Where should tokens be validated: API gateway or service?
Validate tokens at the API gateway so every API applies the same rules, and keep a light check in each service for calls that don't come through the gateway. Gateway validation removes duplicate code and gives you one place to change algorithms, audiences and introspection settings.
The hard part comes with several gateways. Each one validates tokens with its own plugin and settings, so one gateway checks the audience and another doesn't. The DigitalAPI API Gateway applies the same token validation, scope and introspection policy across the gateways you already run. Teams using the DigitalAPI API Developer Portal give partners self-service credentials with the right scopes and lifetimes from day one.
See how DigitalAPI applies one token policy across every gateway you run → Book a Demo
Key takeaways
- Token-based authentication replaces passwords and sessions on each request with a token the API can check.
- Know your tokens: access tokens go to APIs, refresh tokens to the authorization server, ID tokens to apps.
- JWTs validate fast and locally but revoke slowly; opaque tokens revoke instantly but need introspection.
- Rotate refresh tokens with reuse detection, and keep access tokens to minutes.
- Keep tokens out of URLs, logs and browser JavaScript, and bind high-value tokens to the client.
Frequently asked questions
1. What is token-based authentication?
Token-based authentication is a method where a client proves its identity by sending a token with each request instead of a password or session cookie. An authorization server issues the token after the client signs in, and the API validates it by checking its signature or asking the issuer. Tokens expire and can be refreshed or revoked.
2. What is an example of token-based authentication?
A partner app sends its client ID and secret to an authorization server and receives an access token valid for 10 minutes. It then calls GET /orders with the header Authorization: Bearer <token>. The API verifies the token, checks it carries the orders:read scope, and returns the orders.
3. What is the difference between token and session authentication?
Session authentication stores the user's state on the server and gives the browser a session ID cookie. Token authentication gives the client a token that carries, or points to, the identity and permissions the API needs. Sessions suit one web app on one domain; tokens suit APIs called by many apps, services, partners and agents.
4. Is JWT the same as token-based authentication?
No. Token-based authentication is the overall method. A JWT is one token format that carries signed claims the API can verify locally. Token-based authentication can also use opaque tokens, which are random strings the API checks with the issuer through introspection.
5. What is an opaque token?
An opaque token is a random string with no readable content. The API can't verify it on its own, so it asks the authorization server whether the token is active through token introspection (RFC 7662). Opaque tokens can be revoked instantly and reveal nothing to the client, at the cost of a network call per check.
6. What is the difference between an access token and a refresh token?
An access token is short-lived, often minutes, and is sent to the API on every call. A refresh token lives longer, is sent only to the authorization server, and is used to get new access tokens without signing in again. Refresh tokens are more valuable to attackers, so they should be rotated and guarded closely.
7. Where should I store tokens in a web app?
Don't store tokens in localStorage or sessionStorage, because any script running on the page can read them. The safest pattern is a backend-for-frontend that keeps tokens on the server and gives the browser an HttpOnly, Secure, SameSite session cookie. Mobile apps should use the platform keychain or keystore.
8. How do you revoke a token?
Opaque tokens are revoked at the authorization server and stop working on the next introspection call. JWTs stay valid until they expire, so keep them short-lived, revoke the refresh token, and add a denylist of revoked token IDs (jti) for high-risk APIs. RFC 7009 defines a standard revocation endpoint.
9. Is token-based authentication secure?
Token-based authentication is secure when tokens are short-lived, sent only over HTTPS, validated strictly, scoped narrowly and stored out of reach of scripts. The main risk is that bearer tokens work for whoever holds them, so leaked tokens must expire within minutes or be bound to the client with mTLS or DPoP.
10. Is OAuth the same as token-based authentication?
No. OAuth 2.0 is an authorization framework that defines how clients get access tokens from an authorization server. Token-based authentication is the broader pattern of presenting a token instead of credentials. OAuth is the most common way to issue the tokens, and OpenID Connect adds the sign-in layer on top.
Conclusion
Token-based authentication is how almost every modern API knows who's calling: one sign-in, then a token on every request. It works for browsers, mobile apps, services, partners and AI agents in a way sessions and passwords never could.
The format matters less than the discipline around it. Keep access tokens short, validate them strictly, rotate refresh tokens, store tokens out of reach of scripts, and make sure you can revoke them. For how tokens fit with keys, roles and the rest of API security, read our guide to API authentication and the difference between authentication and authorization.
How safely do you grant API access to partners and AI agents? Get your free readiness score: Check Your API Access Readiness
Prefer a walkthrough? Book a Demo
One email a fortnight. Worth opening.
A short digest of what we're writing, what we're learning from customers, and the handful of links you'd actually want from us. No tracking pixels.













.avif)
