Run this curl test first
Before changing code, take your app out of the picture. This one command sends the minimum valid request to the API:
curl -i https://api.unrestrictedaiapi.com/v1/models \
-H "Authorization: Bearer YOUR_KEY"
GET /v1/models costs nothing, so it is a safe way to test a key. The -i flag prints the status line and headers.
- 200 and a model list: the key is fine. Your app is sending something different, so go to the sections on environment variables and base URL.
- 401: the key or the header is wrong. Carry on with the next section.
- Connection or DNS error: the base URL is wrong, which is a different problem from 401.
Paste the key by hand into this test, not through a variable. That removes the environment from the list of suspects.
Cause 1: the header is not quite right
The API expects Authorization: Bearer <key>. Small deviations all produce an llm api error 401:
Authorization: YOUR_KEYwith the word Bearer missing.Authorization: Bearer: YOUR_KEYwith an extra colon.Authorization: Bearer "YOUR_KEY"with quote marks inside the value.Authorization: Bearer YOUR_KEYwith two spaces.- A header called
X-API-Keyorapi-keyinstead ofAuthorization. Those are conventions of other services.
If you use an OpenAI SDK, you do not write the header yourself. The SDK builds it from the api_key argument. If you see a 401 anyway, the value passed to the SDK is the problem, not the header format.
Cause 2: the key itself is damaged
Keys are long random strings, so copy mistakes are common and hard to spot by eye.
- Trailing newline or space. Copying from a terminal or a text file often adds one. Many HTTP libraries then send an invalid header or a key that does not match.
- Truncated key. Double-clicking selects only part of the string if the key contains characters that break word selection. Use the copy button on the API key page instead.
- Smart quotes. Pasting through a word processor or chat app can swap straight quotes for curly ones.
- Old or rotated key. If you created a new key, the old one may no longer be the one you are using.
To check for invisible characters in Python, print the repr of the value:
import os
key = os.environ.get("API_KEY", "")
print(repr(key), len(key))
print(repr(key.strip()) == repr(key))
If the second line prints False, you have whitespace around the key. Call .strip() when you read it.
Cause 3: the wrong base URL
This is the most common mistake when switching an existing app over to a new provider. Your key is only valid on the host that issued it. The base URL for this site is:
{"base_url": "https://api.unrestrictedaiapi.com/v1"}
Two failure patterns show up again and again:
- You set the new key but left the old base URL. The request goes to the old provider, which rejects a key it has never seen and returns 401. The error text then mentions the old provider, which is a useful clue.
- You set the new base URL but left an old key in the environment. The request reaches us, and we reject the other provider's key.
Also check the path. The base URL already ends in /v1, so SDKs add /chat/completions to it. If you build the URL by hand, the full path is /v1/chat/completions, not /v1/v1/chat/completions.
Cause 4: environment variables and config files
If curl works but your app fails, something is feeding the app a different key than you think.
- Variable not exported. In a shell,
API_KEY=abc python app.pyworks, butAPI_KEY=abcon its own line does not reach child processes. Useexport. - Shadowed by another value. Libraries can read
OPENAI_API_KEYautomatically. If an old one is set in your shell profile, it may override what you intended. - Quotes in a .env file. Some loaders keep the quotes as part of the value. If your key arrives wrapped in quote characters, remove them or test with the repr check above.
- Not restarted. Servers and notebooks read environment variables at start-up. Restart after editing.
- Different environment in production. Containers, CI and hosting dashboards each have their own variable store. Print the key length, never the key itself, in the logs.
A minimal Python check that proves the config
This script uses the OpenAI SDK and reports clearly if authentication fails:
import os
from openai import OpenAI, AuthenticationError
client = OpenAI(
base_url="https://api.unrestrictedaiapi.com/v1",
api_key=os.environ["API_KEY"].strip(),
)
try:
r = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Reply with the word ok."}],
max_tokens=5,
)
print("auth ok:", r.choices[0].message.content)
except AuthenticationError as e:
print("401:", e.status_code, e.response.headers.get("X-Request-Id"))
If this prints an X-Request-Id, include it when you write to support, as it lets the team find your request. The same check in other languages is in the quickstart for five languages.
401 versus other errors
Do not spend time on authentication if the error is something else. A short map:
| Status | Usual meaning | Where to look |
|---|---|---|
| 401 | Missing or invalid key | Header, key, base URL |
| 429 | Rate limit or temporary overload | Back off and retry; see the docs for limits |
| 400 | Malformed request body | JSON, model name, parameters |
| 404 | Wrong path | Base URL and endpoint |
Check the model name too. Use uncensored exactly; a wrong model ID is a request error, not a 401. Spec details, including the endpoints and headers the API accepts, are on the OpenAI-compatible API spec page.
Proxies, browsers and last resorts
Two setups cause 401s that look mysterious.
Proxies and gateways
A reverse proxy, API gateway or serverless wrapper between your app and the API may drop or rewrite the Authorization header. Test from the same machine with curl and compare. Some frameworks also forward the header only to same-origin hosts, so a custom fetch wrapper can silently lose it on a cross-origin call.
Keys in front-end code
Calling the API straight from browser JavaScript exposes the key to anyone who opens developer tools, and browsers may also block the request for cross-origin reasons that look like auth errors. Keep the key on a server, and let your front end call your own endpoint, which in turn calls the API with the key attached. If a key was ever pasted into public code or a screenshot, replace it.
Still stuck
If a freshly copied key still returns 401 in the curl test, generate the key again from the API key page, paste it straight into the command, and try once more. If that fails, send support the time of the request, the endpoint and the X-Request-Id response header. Never send the full key in a message; the first and last four characters are enough to identify it.
