Bearer Token vs API Key: Authorization vs x-api-key (2026)
The short answer: an API key is the credential itself. Bearer is a format for putting that credential in the Authorization header. x-api-key is a separate custom header made specifically to carry a key. So "bearer token vs API key" really compares a credential with the header that carries it. OpenAI-style APIs use Authorization: Bearer <Key>. Anthropic's official API uses x-api-key: <Key>, plus a version header.
How are a bearer token and an API key related?
They sit at different levels.
- API key: a string the provider issues to you. It proves that "this request comes from a particular account or project."
- Bearer token: one of the HTTP authentication schemes (RFC 6750). It means "whoever holds this token is treated as authorized." It defines how you write it:
Authorization: Bearer <token>.
Many services use the API key directly as the bearer token, which is why you see the phrase "Bearer + API key." Both terms point to the same string in the same place. Access tokens issued in OAuth flows also use the Bearer format, but they usually expire and come from a different source than an API key.
x-api-key isn't defined by any HTTP standard. It's just a header name that vendors agreed on. Its value is the API key, with no Bearer prefix.
Which header do OpenAI and Anthropic use?
| Item | OpenAI-style API | Anthropic API |
|---|---|---|
| Auth header | Authorization: Bearer <Key> | x-api-key: <Key> |
| Prefix | Write Bearer (with a trailing space) | No prefix |
| Extra required header | None | anthropic-version |
| Content type | Content-Type: application/json | Same |
An OpenAI example:
curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"你的模型名","messages":[{"role":"user","content":"你好"}]}'
An Anthropic example:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{"model":"你的模型名","max_tokens":256,"messages":[{"role":"user","content":"你好"}]}'
The value of anthropic-version and the model name should follow the official docs. The examples above only show the format. The two APIs also differ in many body fields. See Anthropic API vs OpenAI API: a field-by-field comparison.
Why do some tools have both AUTH_TOKEN and API_KEY variables?
It's the same question seen from the other side. Take Claude Code: ANTHROPIC_API_KEY maps to the x-api-key header, and ANTHROPIC_AUTH_TOKEN maps to the Authorization: Bearer header. The second is common when requests go through a gateway or proxy that validates Bearer credentials. For how to choose, see ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY. The latest variable behavior is defined by the official docs.
What happens if you use the wrong header?
Most services return HTTP 401 when the credential is missing or invalid, with an error type and message in the response body. Exact field names vary by vendor, so check the official docs. Common cases:
| Symptom | Likely cause | How to check |
|---|---|---|
| 401, message says the key is missing or invalid | Wrong header name, for example a Bearer header on the Anthropic API, or x-api-key on an OpenAI API | Compare the header name against the table above |
| 401, but the key looks right | Missing space after Bearer, or a newline or quote around the key | Print the variable's length, and use printf rather than echo to inspect it |
| 400, message says the version is missing | The Anthropic request left out anthropic-version | Add that header |
| 403 or 404 | The key lacks permission for that model or project, or the URL is wrong | Check project permissions and the base URL |
| 429 | Rate limit hit or quota used up, unrelated to the auth format | Check your quota and rate limits |
The status codes above follow general HTTP semantics. What a particular service returns is defined in its docs.
How to debug auth problems yourself
- Confirm which API format you're calling (OpenAI-compatible or Anthropic native), rather than just looking at whose key it is.
- Send one minimal request with curl on its own, to rule out SDK and tool configuration.
- Add
-vto see the headers actually sent, and check the header name, prefix and spacing. - Check that the base URL has the right path, for example whether you added or dropped a
/v1. - Try a freshly generated key, to rule out truncation when copying.
- If it still fails, check whether the official service is having an outage. cdnping's /en/status/openai and /en/status/anthropic show probe results for the API endpoints. For latency problems, see /en/learn/ai-api-latency-china.
How do you get and store the key?
For the process, see How to get a Claude API key and How to get an OpenAI API key. If the concept of a key is still unclear, start with What is an API key? 5 things beginners should know before their first AI API call.
A few general rules for storage: don't put keys in frontend code or public repositories; use environment variables or a secrets manager; revoke and reissue immediately if a key leaks; and use a different key for each purpose so you can revoke one without affecting the others.
What if your tool only asks for a base URL and a key?
Many tools (editor plugins, desktop clients) offer just two input boxes, and the tool builds the auth header for you based on the API format. This is the point made in What does "OpenAI-compatible API" mean?: the other side implements the same paths and header conventions, so you only change the address and the key.
If you don't have an overseas credit card, or don't want to manage several accounts and billing systems separately, you could look at AGI Plan. It is a multi-provider LLM aggregation gateway with monthly subscriptions, paid in RMB, compatible with both the OpenAI and Anthropic API formats, so tools like Claude Code and Codex work by changing a single endpoint address. AGI Plan is a commercial partner of cdnping. The service states plainly that it is not an officially authorized channel, and some models are marked "overseas only." Available models and regions are as listed at agiplan.dev/models. Please follow each service's terms and local regulations. For setup steps, see agiplan.dev/startup.
What to do next
- Send one request with each of the two curl commands above and see the header difference for yourself.
- In your own code, define the auth header in one place, so switching vendors means changing only that spot.
- To learn how to write the request body, read What is Chat Completions? How to write the messages array.