Skip to content

Troubleshooting 401 Unauthorized When Calling OpenAI Compatible APIs ​

First Locate the Issue with a Single Command ​

Run the following command to test:

bash
curl -sS -i https://api.treenew.online/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.6-luna","messages":[{"role":"user","content":"hi"}],"max_tokens":1}'

max_tokens: 1 is deliberate: this call only needs to prove authentication works, so the cost is negligible.

Reading the result:

  • HTTP 401 — the Key or the Authorization header is at fault, not your client. Continue to the checklist below.
  • HTTP 200 — the Key is fine; the problem is in your client configuration (wrong base URL, the Key never got saved, an environment variable overriding it, and so on).
  • 404 saying the model does not exist — authentication actually succeeded; that is a different problem, see Model Not Found.

Running this one command first saves most of the guesswork.

Check Item by Item ​

  • Missing or Incorrect Bearer Prefix: Verify the header is exactly Authorization: Bearer sk-xxx. Rule out a missing prefix, lowercase bearer, or using Basic.
  • Whitespace or Newlines in the Key: After copying the Key from the console, trim it to remove any leading or trailing whitespace characters.
  • Using Login Password Instead of Token: Confirm the Key was created on the https://api.treenew.online Token page, not your account login password.
  • Token Deleted/Expired/Disabled: Log in to the console and check the token status. Exhausting quota can cause the token to be disabled.
  • Token IP or Model Whitelist Mismatch: Check the token binding settings to ensure the current IP and requested model match the whitelist.
  • Key Placed in the Wrong Field (e.g. Base URL): Base URL must be set to https://api.treenew.online/v1. The Key belongs only in the Authorization header.
  • Key Changes Not Taking Effect Due to Client Storage: After updating the Key, restart the client. Check whether environment variables or caches are overriding the new value.

Difference Between 401 and 403 ​

401 means authentication information is invalid or missing. 403 means authentication succeeded but access is denied. The troubleshooting approaches differ.

Still Not Working ​

Collect the desensitized request headers (with Key value hidden), request timestamp, and token name for feedback.

See also ​

Home · Console