What Is a Bearer Token? How Bearer Authentication Works. A bearer token grants access to whoever holds it. How to send it in the Authorization header, opaque vs JWT formats, 401 vs 403 errors and common mistakes.
A bearer token is an access credential that grants access to whoever holds it, with no further proof of identity. The client sends it in the HTTP Authorization header as Authorization: Bearer followed by the token, as defined in RFC 6750, “Bearer Token Usage”, and the API validates the token before it serves the request.
Here is the request you will copy most often, with your token stored in a shell variable:
curl https://api.example.com/v1/orders \
-H "Authorization: Bearer $ACCESS_TOKEN"
On the wire, the same request looks like this (the token here is a short made-up JWT):
GET /v1/orders HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.c2lnbmF0dXJl
If you got here from a 401 or 403 response, jump to 401 vs 403 and the common mistakes. The rest of the post explains what “bearer” means, the two token formats you will meet, how to describe bearer authentication in OpenAPI, how to test it with curl and Postman, and how to limit the damage a leaked token can do.
Key takeaways
- “Bearer” describes how a token is used (holding it is enough), while “JWT” describes a token format. A bearer token can be a JWT or an opaque string.
- Send it in the Authorization header over HTTPS. RFC 6750 section 2 also allows a form body parameter and a query parameter, but the OAuth security best practice now says clients must not put access tokens in the URL (RFC 9700 section 4.3.2).
- A 401 with
invalid_tokenmeans “get a new token”, while a 403 withinsufficient_scopemeans the token is valid but not allowed to do this (RFC 6750 section 3.1).- Refresh tokens are never sent to APIs; they go only to the authorization server (RFC 6749 section 1.5).
- Short lifetimes, audience restriction and, for high-value APIs, sender-constrained tokens (mTLS or DPoP) limit what a stolen token can do.
What does “bearer” mean in a bearer token?
RFC 6750 defines a bearer token as a security token with the property that any party in possession of it can use it in any way that any other party in possession of it can, and it adds that using one does not require the bearer to prove possession of cryptographic key material (RFC 6750 section 1.2). In plain terms, the API does not check who is sending the token. It checks that the token is valid, and if it is, the request goes through.
A cinema ticket works the same way. The usher checks that the ticket is real and for this screening, but never asks whether you are the person who bought it. That makes bearer tokens easy to use, because the client does not have to prove anything beyond holding the token, and it is also their main risk, because anyone who copies the token from a log file, a browser history or a proxy can use it for as long as it stays valid. RFC 6750’s security recommendations start from this risk: client implementations must ensure bearer tokens are not leaked to unintended parties (RFC 6750 section 5.3).
How does bearer authentication work?
In OAuth 2.0, three parties are involved. The authorization server issues tokens, the client is the app or script that calls the API, and the resource server is OAuth’s name for your API. RFC 6749, “The OAuth 2.0 Authorization Framework” defines how a client obtains an access token, and RFC 6750 defines how the client then presents it to the API.
The cycle runs like this:
- The client asks the authorization server’s token endpoint for an access token, for example with its client ID and secret, or after a user signs in.
- The authorization server answers with a JSON document that contains the
access_token, itstoken_type(hereBearer) and usually anexpires_inlifetime in seconds (RFC 6749 section 5.1). - The client sends the token in the Authorization header on every API call.
- The API validates the token (its signature or its status, its expiry, its audience and its scope) and serves the request or rejects it.
- When the token expires, the API rejects it and the client gets a new one.
The client requests a token (1) and receives it (2), then calls the API with an Authorization: Bearer header and gets 200 OK (3). Anyone who copied the token gets the same answer, because holding it is enough (4). Once the token expires, the API answers 401 with invalid_token and the client fetches a new token (5).
What are the two bearer token formats: opaque tokens and JWTs?
RFC 6750 says nothing about what is inside a bearer token, and RFC 6749 describes an access token as a string that is “usually opaque to the client” (RFC 6749 section 1.4). In practice you will meet two formats, and both are sent the same way:
| Opaque token | JWT | |
|---|---|---|
| What it looks like | A random string, such as a long run of letters and digits | Base64url parts separated by dots (RFC 7519 section 3) |
| What the API learns from the token alone | Nothing | The claims inside it, such as sub, aud, scope and exp, once the signature checks out |
| How the API validates it | Asks the authorization server through token introspection (RFC 7662) | Checks the signature with the issuer’s public key, then the claims |
| Revocation | Takes effect on the next introspection call | The token stays valid until it expires, unless the API also introspects |
A JWT, or JSON Web Token (RFC 7519), can be signed (a JWS) or encrypted (a JWE). A signed JWT has a header, the claims and a signature, so the API can verify it locally without a network call. An opaque token carries no readable data, so the API sends it to the authorization server’s introspection endpoint, which returns a JSON document with an active field and, when the token is active, details such as its scope, client and expiry (RFC 7662 section 2.2).
Keycloak’s documentation says its access tokens are JWTs and that “you can locally validate access tokens using the public key of the issuing realm”, and it also documents an introspection endpoint (Keycloak, “OpenID Connect layers”). Our post on token introspection vs local JWT validation compares the two in detail.
A JWT carries its claims with it, so every role or group in it makes the Authorization header longer. RFC 6585 section 5 defines status 431 (Request Header Fields Too Large) for a server that refuses headers that are too big, and our post on fixing Keycloak’s 431 error shows how to slim the token down.
How do you send a bearer token?
RFC 6750 gives clients three ways to send the token, and a client must not use more than one of them in the same request (RFC 6750 section 2).
The Authorization header is the one to use, and resource servers must support it. The value is the word Bearer, one or more spaces, and the token. The token grammar, called b64token, allows letters, digits and the characters -, ., _, ~, + and /, followed by optional = padding (RFC 6750 section 2.1), which is why a JWT fits in the header without any extra encoding. HTTP treats the scheme name as case-insensitive (RFC 9110 section 11.1), so a server should accept bearer as well as Bearer, but clients should send Bearer as RFC 6750 writes it.
A form-encoded body parameter named access_token is allowed only when the request’s Content-Type is application/x-www-form-urlencoded, the body is single-part ASCII, and the method has a body (so never GET). The spec says to use it only when the client cannot set the header (RFC 6750 section 2.2).
A URI query parameter, ?access_token=..., is the one to avoid. RFC 6750 already said it “SHOULD NOT be used unless it is impossible to transport the access token in the Authorization request header field or the HTTP request entity-body” (RFC 6750 section 2.3), and the OAuth 2.0 Security Best Current Practice, published in January 2025, made it stricter: “Clients MUST NOT pass access tokens in a URI query parameter in the way described in Section 2.3 of [RFC6750]” (RFC 9700 section 4.3.2). URLs end up in browser history, server access logs and proxy logs (RFC 6750 section 5.3), and anyone who can read those can use the token.
401 vs 403: why is my bearer token rejected?
When a request has no usable token, RFC 6750 says the API must answer with a WWW-Authenticate header using the Bearer scheme, which can carry a realm, a scope, an error, an error_description and an error_uri (RFC 6750 section 3). If the request had no credentials at all, the server “SHOULD NOT include an error code or other error information” (RFC 6750 section 3.1), so the response is just the challenge:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example"
If a token was sent and rejected, the challenge carries one of three error codes, each with its own status (RFC 6750 section 3.1):
| Error code | HTTP status | What it means | What the client should do |
|---|---|---|---|
invalid_request |
400 Bad Request | The request is malformed: a parameter is missing or repeated, or the token was sent in more than one way | Fix the request; a new token will not help |
invalid_token |
401 Unauthorized | The token is expired, revoked, malformed or invalid for another reason | Get a new access token and retry |
insufficient_scope |
403 Forbidden | The token is valid but lacks the privileges this resource needs | Ask for a token with the scope named in the challenge |
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example", error="invalid_token", error_description="The access token expired"
The split matters because the fixes are different. A 401 means the API does not accept the credential, so the client should fetch a new token and retry, which RFC 6750 explicitly allows for invalid_token. When a 401 keeps coming back with a fresh token, check these in order:
- The header is exactly
Bearer, one space and the token, with nothing else around it. - The token has not expired: decode its
expclaim, or compare the time you got it withexpires_in. - The token’s issuer (
iss) is the one your API trusts, which catches a token from a test realm sent to production. - The token’s audience (
aud) includes your API. - You sent an access token, not an ID token or a refresh token.
A 403 means the API accepted who you are but not what you asked for. Fetching another token with the same scope will fail the same way, because RFC 6750 says insufficient_scope means the request requires higher privileges than the access token provides. Look at the scope attribute in the challenge, then at the scopes and roles the token actually carries, and ask the authorization server for a token that includes them. Many APIs also return 403 for their own permission checks, such as a missing role, without a WWW-Authenticate header at all. For Keycloak-specific causes, see our Keycloak 403 Forbidden troubleshooting guide.
Common bearer token mistakes
These come up again and again when an API that works in one tool fails in another:
- Leaving out the
Bearerprefix, or adding a colon.Authorization: eyJ...andAuthorization: Bearer: eyJ...both break theBearerspace token form in RFC 6750 section 2.1. So does storing a token that already starts withBearerand then adding the prefix again. - Ignoring expiry. The token response tells you how long the token lasts in
expires_in(RFC 6749 section 5.1). A client that caches a token without that lifetime works for a few minutes and then starts failing with 401. - Putting the token in the URL. As above, RFC 9700 section 4.3.2 says clients must not do this.
- Sending the wrong token. An OpenID Connect ID token is meant for the client, and its audience is the client’s ID (OpenID Connect Core 1.0 section 2), so an API that checks
audwill reject it. Our post on ID tokens vs access tokens explains the difference. - Logging the header. Request logging, error trackers and analytics tools often capture headers by default, and a token in a log can be used by anyone who reads it.
- Storing tokens where scripts can read them. In a browser, where you keep the token decides who can steal it. Our Keycloak token storage best practices cover the options.
How do you describe bearer authentication in OpenAPI?
If you document your API with OpenAPI, declare bearer authentication as an http security scheme with scheme: bearer, then apply it globally or per operation. This example follows the Swagger documentation for bearer authentication:
openapi: 3.0.3
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
responses:
UnauthorizedError:
description: Access token is missing or invalid
security:
- bearerAuth: []
paths:
/v1/orders:
get:
responses:
"200":
description: The list of orders
"401":
$ref: "#/components/responses/UnauthorizedError"
Swagger’s bearer authentication documentation says bearerFormat “is used mainly for documentation purposes, as a hint to the clients”, so the API still has to validate the token itself.
How do you test a bearer token with curl or Postman?
With curl, pass the header with -H, as at the top of this post. Add -i to print the response headers, so you can read the WWW-Authenticate challenge when the call fails:
curl -i https://api.example.com/v1/orders \
-H "Authorization: Bearer $ACCESS_TOKEN"
In Postman, open the request’s Authorization tab, choose Bearer Token as the auth type and paste the token into the Token field. Postman then adds the Bearer prefix to the Authorization header for you, and its documentation suggests storing the token in a variable rather than pasting it into each request (Postman, “Authorization types”). If you would rather not copy tokens by hand, the OAuth 2.0 auth type can fetch the token from your authorization server, and its Auto-refresh access token option refreshes the token in the background before you send a request that uses it. Postman’s OAuth 2.0 documentation says “Auto-refresh is available when a refresh token is present”, so this works only when your authorization server returns one.
When a call fails, paste the token into our JWT decoder and compare its exp, aud, iss and scope claims with what the API expects.
Identity management as a service, on open source
Skycloak is a managed identity platform in the same category as Auth0 or Okta, built on real upstream Keycloak. You get SSO, MFA, SCIM and audit logs without running the servers, and you can export and self-host whenever you want.
Are refresh tokens bearer tokens?
A refresh token is a credential the client uses to get new access tokens, and RFC 6749 says refresh tokens are intended for use only with authorization servers and are never sent to resource servers (RFC 6749 section 1.5). So the token your API should expect in an Authorization: Bearer header is always an access token. A refresh token is still a bearer credential in the sense that holding it is often enough to use it, which is why it needs the same care in storage. Our post on access tokens vs refresh tokens covers how the two work together.
How do you limit or bind a bearer token?
Because anyone holding a bearer token can use it, the safest token is one that can do very little, for a short time, in one place. The standards give you four tools, from the simplest to the strongest.
Short lifetimes. RFC 6750 says token servers “SHOULD issue short-lived (one hour or less) bearer tokens” (RFC 6750 section 5.3), so a leaked token stops working soon.
Audience. The aud claim “identifies the recipients that the JWT is intended for”, and a recipient that does not find itself in it must reject the token (RFC 7519 section 4.1.3). RFC 9700 says access tokens “SHOULD be audience-restricted to a specific resource server”, and that when a token was not meant for it, “the resource server MUST refuse to serve the respective request” (RFC 9700 section 2.3). When one service needs to call another with a token for a different audience, token exchange swaps one token for another; our post on how Keycloak token exchange works walks through it.
Scope. The scope lists what the token may do at that API, such as orders:read, and RFC 9700 says the privileges of an access token “SHOULD be restricted to the minimum required for the particular application or use case” (RFC 9700 section 2.3). Audience answers “is this token meant for me?” and scope answers “is this token allowed to do this?”, and your API should check both on every request.
Sender-constrained tokens. These remove the “bearer” property itself by binding the token to a key the legitimate client holds, so a stolen token is useless without that key. RFC 9700 says authorization and resource servers should use such mechanisms “to prevent misuse of stolen and leaked access tokens” (RFC 9700 section 2.2.1). There are two standard ways:
- Mutual TLS certificate-bound tokens (RFC 8705 section 3): the authorization server binds the token to the client’s TLS certificate, and the API accepts it only over a mutual TLS connection that uses the same certificate.
- DPoP, Demonstrating Proof of Possession (RFC 9449): the client signs a small proof JWT for each request with its own private key and sends it in a
DPoPheader (RFC 9449 section 4), and the request uses theDPoPauthorization scheme instead ofBearer(RFC 9449 section 7.1).
GET /v1/orders HTTP/1.1
Host: api.example.com
Authorization: DPoP eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.c2lnbmF0dXJl
DPoP: eyJ0eXAiOiJkcG9wK2p3dCIsImFsZyI6IkVTMjU2In0.eyJodG0iOiJHRVQifQ.cHJvb2Y
Sender-constraining takes more work, because every client needs a key pair and every API has to check the proof, so it is worth it mainly for high-value APIs or clients that run where tokens can leak. Keycloak’s documentation says “DPoP binds an access token and a refresh token together with the public part of a client’s key pair” (Keycloak, “OpenID Connect layers”), and our post on DPoP with the Keycloak Admin API in Node.js shows it in code.
Bearer tokens vs API keys
An API key is often sent in a header too, sometimes even with the Bearer scheme, but it is a different kind of credential. An OAuth access token is issued by an authorization server, which can give it a lifetime (RFC 6749 section 5.1) and limit it by scope and audience. None of the RFCs above defines API keys, so how a key is issued, how long it lasts and what it allows is up to each API. Our post on API keys vs OAuth helps you choose between them.
Example: get a bearer token from a Skycloak realm and call an API
Here is the whole cycle with Keycloak on Skycloak as the authorization server, using the client credentials grant that a backend service would use. When you create an application in a Skycloak realm, the dashboard gives you its client ID, client secret and issuer URL, which ends in /realms/ and the realm name (Skycloak quickstart). The client credentials grant “MUST only be used by confidential clients” (RFC 6749 section 4.4), so use a client that has a secret, and see the Keycloak Server Administration Guide’s section on using a service account for setting up the client and its roles.
The Keycloak documentation gives the token endpoint as /realms/{realm-name}/protocol/openid-connect/token (Keycloak, “OpenID Connect layers”), which is the issuer URL followed by /protocol/openid-connect/token. This call uses jq to keep only the access token:
ISSUER="https://your-cluster-id.app.skycloak.io/realms/my-realm" # from the dashboard
ACCESS_TOKEN=$(curl -s -X POST "$ISSUER/protocol/openid-connect/token" \
-d grant_type=client_credentials \
-d client_id=orders-worker \
-d client_secret="$CLIENT_SECRET" | jq -r .access_token)
The full response (trimmed here) is the JSON document from step 2 of the flow, with access_token and token_type as its required fields (RFC 6749 section 5.1):
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIn0...",
"expires_in": 300,
"token_type": "Bearer",
"scope": "profile email"
}
The client then calls the API with the token in the header:
curl -i https://api.example.com/v1/orders \
-H "Authorization: Bearer $ACCESS_TOKEN"
Once the token expires, an API that follows RFC 6750 answers with the challenge below, and the client requests a new token from the same endpoint and retries:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="orders-api", error="invalid_token", error_description="The access token expired"
On the API side, validate the JWT locally against the realm’s public keys at /realms/{realm-name}/protocol/openid-connect/certs, or ask Keycloak through the introspection endpoint at /realms/{realm-name}/protocol/openid-connect/token/introspect, which, in the words of Keycloak’s “OpenID Connect layers” page, “can only be invoked by confidential clients”. Our post on verifying a Keycloak-issued access token on the backend has the code for both.
Bearer token security checklist
These come from RFC 6750’s own recommendations (section 5.3), RFC 9700 and the checks covered above:
- Send tokens only over HTTPS, and have clients validate the server’s TLS certificate.
- Use the Authorization header, and never put tokens in URLs.
- Keep access tokens short-lived, and send refresh tokens only to the authorization server.
- Restrict each token’s audience to the API that should accept it, and check
aud,iss,expandscopein the API on every request. - Keep tokens out of logs, error messages and analytics tools.
- Return the right challenge: 401 with
invalid_tokenfor a bad token, 403 withinsufficient_scopefor a token that lacks permission. - Consider DPoP or mTLS for APIs where a leaked token would do real damage.
Frequently asked questions
What is the difference between Bearer and Basic authentication?
Basic authentication sends a user ID and password, base64-encoded, in the Authorization header (RFC 7617 section 2), so every request carries the long-lived password. Bearer authentication sends a token that an authorization server issued, which can expire, carry limited scope and be revoked without anyone changing a password. Both use the Authorization header, with different scheme names.
Is a bearer token the same as an access token?
In OAuth 2.0, the access token is the credential and “bearer” is the way it is used. An access token sent with the Bearer scheme is a bearer token, but an access token can also be sender-constrained with DPoP or mTLS, and then it is no longer a bearer token.
Is a bearer token a JWT?
Not necessarily. A bearer token can be a JWT or an opaque string, and the API validates the two differently, as the formats section above explains.
Can I decode a bearer token?
If it is a signed JWT that is not also encrypted, yes: its header and claims are base64url-encoded (RFC 7519 section 3), so anyone who holds it can read them, which is why sensitive data does not belong in its claims. Decoding is not verifying, though, and your API must still check the signature. An opaque token has nothing to decode, so the API uses introspection instead.
How long does a bearer token last?
As long as the authorization server decides, and it tells the client in expires_in (RFC 6749 section 5.1). RFC 6750 recommends one hour or less (section 5.3). The Keycloak response in the example above has an expires_in of 300 seconds.
Should a bearer token go in the URL?
No. RFC 9700 section 4.3.2 says clients must not pass access tokens in a URI query parameter, because URLs are kept in browser history and server logs, where anyone who finds the token can use it.
Next steps
To see the whole cycle for yourself, create an application in a Skycloak realm, request a token with the curl command in the example above, and call your API with it. Then paste the token into the JWT decoder to check its aud, scope and exp claims against what your API expects, and read the WWW-Authenticate header on any failed response, which tells you whether the problem is the request, the token or its permissions. If you would rather not run Keycloak yourself, try Skycloak or read about our managed Keycloak hosting.
Sources
- RFC 6750, The OAuth 2.0 Authorization Framework: Bearer Token Usage
- RFC 6749, The OAuth 2.0 Authorization Framework
- RFC 9700, Best Current Practice for OAuth 2.0 Security
- RFC 7519, JSON Web Token (JWT)
- RFC 7662, OAuth 2.0 Token Introspection
- RFC 8705, OAuth 2.0 Mutual-TLS Client Authentication and Certificate-Bound Access Tokens
- RFC 9449, OAuth 2.0 Demonstrating Proof of Possession (DPoP)
- RFC 9110, HTTP Semantics
- RFC 6585, Additional HTTP Status Codes
- RFC 7617, The Basic HTTP Authentication Scheme
- OpenID Connect Core 1.0
- Swagger, Bearer Authentication
- Postman, Authorization types
- Postman, OAuth 2.0
- Keycloak, OpenID Connect layers
- Keycloak Server Administration Guide, Using a service account



