Curl Bearer Token Auth Header Example

A bearer token is a credential you present in a header; whoever holds it is treated as its owner. In curl that is one -H flag, and the main thing to get right is where the token is stored while you use it.

Last updated . How this page is checked.

The command

curl -H "Authorization: Bearer $TOKEN" https://httpbin.org/bearer

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-H "Authorization: Bearer TOKEN"The standard form. Scheme, one space, then the token.
--oauth2-bearer TOKENBuilds the same header for you.
-vShows the request, including the header, to confirm what was sent.

Keeping the token out of history

Pasting a token into the command stores it in shell history and exposes it in the process list. Put it in an environment variable and reference that in double quotes, so the shell expands it. Single quotes would send the literal text $TOKEN, which produces a 401 that looks like a bad token.

For tokens you fetch from a login endpoint, capture the value straight into the variable so it never appears on screen.

read -rs TOKEN                       # paste, no echo, not in history
export TOKEN
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/me

TOKEN=$(curl -s -d "client_id=...&client_secret=..." \
  https://auth.example.com/oauth/token | jq -r .access_token)

Reading the failure

401 Unauthorized means the server did not accept the credential: missing header, malformed header, expired or revoked token. Its WWW-Authenticate response header often says which, for example error="invalid_token". 403 Forbidden means the token was accepted and identifies you, but lacks the permission or scope for this action. Getting a new token fixes the first and never the second.

curl -i -H "Authorization: Bearer $TOKEN" https://api.example.com/admin \
  | grep -i -E "^(HTTP|www-authenticate)"

Common mistakes

  • The word Bearer is required, with exactly one space after it. Sending the token alone is the most common cause of a 401 with a valid token.
  • Stray whitespace or a newline copied along with the token breaks the header.
  • Not every API uses this scheme. Some want X-API-Key, Token, or the key as a Basic-auth username. Follow the API's own documentation.

Related curl examples

More under Authentication

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I send a bearer token with curl?

Add the header -H "Authorization: Bearer YOUR_TOKEN" to the request, or use --oauth2-bearer YOUR_TOKEN.

Why do I get 401 with a valid bearer token?

Usually the header is malformed: the word Bearer is missing, there is extra whitespace, or the token was placed in single quotes so a shell variable was not expanded.

What is the difference between 401 and 403 with a bearer token?

401 means the token was missing, invalid or expired. 403 means the token is valid but does not grant permission for the requested action.