curl with authorization: Bearer token, Basic, API key and headers
Checkcraft is a desktop app for checking APIs and logs without code, with a free edition. Download, no sign-up →
Most APIs require auth, and in curl it goes in a header or the -u flag. A Bearer token goes in the Authorization header, login and password via -u, an API key in the header named in the service docs. Below are working commands and the common mistakes that make the server answer 401.
Bearer token
Write the full header: -H "Authorization: Bearer your_token". The word Bearer and the space are required. Keep the token in an environment variable and substitute it so it stays out of shell history and screenshots.
Basic and API key
For Basic use -u login:password; curl encodes the pair in Base64 itself. Leave the password out and curl asks for it interactively. Pass an API key the way the service requires: usually a header like X-API-Key, sometimes a URL parameter, but parameters end up in server logs.

Why 401 comes back
Most often the token expired, was copied with an extra space or newline, sent in the wrong header or without the word Bearer. On Windows quotes sometimes eat part of the header. Use -v to see the header actually sent.
Commands with auth
# Bearer токен из переменной окружения
export TOKEN="eyJhbGciOi..."
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/v1/orders
# Basic: логин и пароль
curl -u "user:password" https://api.example.com/v1/orders
# API-ключ в заголовке
curl -H "X-API-Key: $API_KEY" https://api.example.com/v1/orders
# POST с токеном и JSON
curl -X POST https://api.example.com/v1/orders \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"sku":"MON-24","qty":2}'
# cookie сессии
curl -b "session=abc123" https://app.example.com/api/profileOn Windows variables and quotes differ; see curl on Windows and PowerShell.

Which method where
| Method | In curl | Where you meet it |
|---|---|---|
| Bearer token | -H "Authorization: Bearer ..." | OAuth 2.0, JWT, most modern APIs |
| Basic | -u user:password | Internal services, 1C, older APIs |
| API key | -H "X-API-Key: ..." | Public service APIs |
| Cookie | -b "name=value" | Web app APIs after login |
Check what was sent
-v prints the sent headers on lines starting with a greater-than sign. An empty Authorization or a stray character means a variable or quoting mistake. A JWT expiry can be checked by decoding it: JWT decoder.
More on curl
Section overview and all guides: curl command cheat sheet: flags and examples for APIs.
- curl errors: codes 6, 7, 22, 28, 35, 60 and how to fix them
- curl on Windows and PowerShell: how to run it and why commands fail
- Download a file with curl: -o, -O, redirects and resume
- curl through a proxy: -x, environment variables, SOCKS and no_proxy
- PHP cURL: GET and POST requests with JSON, headers and error handling
Frequently asked questions
How do I pass a Bearer token in curl?
-H "Authorization: Bearer token"; the word Bearer and the space are required.
How do I pass login and password?
-u login:password; curl builds the Basic header.
Why do I get 401 with a token?
Expired, extra whitespace, wrong header or missing Bearer; check with -v.
Repeat this on your own API
Checkcraft is a desktop app that saves API response checks without code and shows what did not match. Start with a sample, then use your own sanitized data. Free runs on Windows and Linux with no time limit.
Download FreeDownload and first runNo sign-up or email needed, the archive is ready in a couple of minutes. See the edition limits.
References
Start with the linked example and compare its result with your own test requirement.