Basic

Proving who you are

A private API kept answering 401. That number means you did not prove who you are. Authentication is a header that carries a token the server checks.

What you will see

200

About 1 min of reading

Try It

Send a credential header and turn a 401 into a 200.

1

Call a private endpoint with no credential. The server answers 401.

curl -s -o /dev/null -w "%{http_code}" https://api.example/private

Before you run it, say out loud what it will print. Then open the answer.

What it prints
401
2

Add the credential header from an environment variable. Now it answers 200.

curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $TOKEN" https://api.example/private

Before you run it, say out loud what it will print. Then open the answer.

What it prints
200
Never paste the token literally into the command. It lands in shell history. Read it from the environment so the secret stays out of your history and logs.

Data Flow

A private endpoint checks the credential header before doing work, answers 401 if it is missing, and 200 if the token is valid.
1 Server checks credential header
↓
2 Missing gives 401
↓
3 Valid token gives 200
↓
4 Token read from env not pasted
Why this works
Auth is a header

Authentication is a header that proves the caller. A Bearer token, an API key header, or a signed request are all just ways to fill that header. The server checks it before it does any work, and answers 401 if it is missing or bad.

Secrets leak from history

A token typed on the command line is stored in shell history and often in logs. Read it from an environment variable or a file. This is not style, it is security: a leaked token is a leaked account.

Examples

The 401 is a prompt

401 Unauthorized

401 does not mean you are wrong, it means you are unknown. The server is asking for proof. Add the credential header and try again.

Carry a token

curl -H 'Authorization: Bearer sk-abc123' https://api.example/users

The Authorization header carries the token. The server checks it and, if valid, answers 200 with the data.

Keep the secret out

curl -H "Authorization: Bearer $TOKEN" https://api.example/users

Read the token from the environment, do not paste it in the command. A pasted secret lands in your shell history and your logs.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Asking in the URL
Previous Lesson
Asking in the URL
Next Lesson
Pages, not the whole list
Pages, not the whole list →