Fix OpenAI API 401 "Incorrect API key provided"
When the OpenAI API answers 401 Incorrect API key provided, read the masked key in the message and rule out env variables, spaces, wrong project, deleted keys.
Incorrect API key provided: sk-test-***alid. You can find your API key at https://platform.openai.com/account/api-keys.The server could not authenticate the key sent with the request. This is about the key itself, not quota or billing, so resending the same request gives the same answer. The message echoes the first and last characters of the key it received, which is your best clue.
[1] Reproduce the error shape with a fake key. No account or billing is involved.
$ curl -sS -w "\nHTTP %{http_code}\n" https://api.openai.com/v1/models -H "Authorization: Bearer sk-test-invalid"{ "error": { "message": "Incorrect API key provided: sk-test-***alid. You can find your API key at https://platform.openai.com/account/api-keys.", "type": "invalid_request_error", "param": null, "code": "invalid_api_key" }}HTTP 401The code is invalid_api_key. In the message, sk-test-***alid keeps the first 8 and last 4 characters of the value sent, sk-test-invalid. That is what this single call showed; the masking rule is not documented, as far as the docs checked. In a real error, compare those characters with the key you meant to use to see whether the server received the right one.
[2] Tell an empty variable apart from a wrong key. When a shell variable is empty, the header still goes out as Bearer with nothing after it, so the message differs from a request with no key at all. First, the empty-variable case.
$ K=""$ curl -sS -w "\nHTTP %{http_code}\n" https://api.openai.com/v1/models -H "Authorization: Bearer $K"{ "error": { "message": "Incorrect API key provided: ''. You can find your API key at https://platform.openai.com/account/api-keys.", "type": "invalid_request_error", "param": null, "code": "invalid_api_key" }}HTTP 401If the key in the message is '', the key was not wrong; the variable was empty. Look for a misspelled variable name or a .env file that was not loaded. Next, the case where no Authorization header is sent at all.
$ curl -sS -w "\nHTTP %{http_code}\n" https://api.openai.com/v1/models{ "error": { "message": "Missing bearer authentication in header", "type": "invalid_request_error", "param": null, "code": null }}HTTP 401Missing bearer authentication in header also means the code never sent a key. Check that the line building the header actually runs.
[3] Match the masked characters against the key you intended. A prefix like sk-your-, sk-xxxx or YOUR_API_KEY is a placeholder from sample code. Different last characters mean a different key is being sent.
[4] Trace where the key comes from. Of the causes in the official docs, these are the ones you can check yourself.
| Suspect | Check |
|---|---|
| Typo or extra space | Trailing space or newline from copy-paste. The docs name "a typo or an extra space" |
| Key from another organization or project | The organization ID and project in the request match the key's owner |
| Deleted or deactivated key | The key still appears in the API keys list |
| Cached old key | The docs suggest clearing browser cache and cookies |
Check a shell variable by its length, without printing the secret.
$ echo -n "$OPENAI_API_KEY" | wc -cIf the count is 1 or 2 higher than a freshly created key of the same type (project key against project key), a newline may be mixed in. The docs do not state key length and it can differ by key type, so compare like with like. To see the last characters directly, run the command below; a \n at the end means a newline is mixed in.
$ printf '%s' "$OPENAI_API_KEY" | tail -c 4 | od -c[5] Check that the runtime is not overriding your key. One forum thread (its replies were not read) reports this error with a seemingly correct key. A common suspect is a new key in the code but an old environment variable left in the shell, IDE or container; open a new terminal or recreate the container and see whether the last characters in the message change.
[6] Create a new key and swap it in. The docs' own fix is to verify the key and organization ID, and generate a new key if unsure. A model list in response means the key problem is solved. Pass the real key through an environment variable so it stays out of shell history.
$ curl -sS https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"[7] If it is still 401, read the message again. The docs list other 401 messages separately.
| Message | Cause (official docs) |
|---|---|
You must be a member of an organization to use the API | The account is not in an organization (removed, or the organization was deleted) |
IP not authorized | The request IP is not in the project or organization IP allowlist |
A 429 You exceeded your current quota is a billing or limit problem, not authentication. See OpenAI API 429 "exceeded your current quota".