EN ▾
https://api.unrestrictedaiapi.com/v1uncensored2026-10-06
Get API key

LLM API Error 401: Fixing Authentication Failures

An llm api error 401 means the server could not authenticate your request. In nearly every case the cause is on the client side: a mistyped header, a key with a stray character, or a request sent to the wrong host. This guide walks through the checks in the order that finds the problem fastest.

Updated

A key that does not fit a glowing lock, illustrating an authentication failure when calling an LLM API

Key points

  1. 401 is an authentication failure, not a rate limit or a billing error.
  2. The header must be exactly Authorization: Bearer YOUR_KEY, with one space and no quotes.
  3. A key sent to the wrong base URL fails just like a wrong key.
  4. Test with curl first; if curl works, the bug is in your app's config.

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_KEY with the word Bearer missing.
  • Authorization: Bearer: YOUR_KEY with an extra colon.
  • Authorization: Bearer "YOUR_KEY" with quote marks inside the value.
  • Authorization: Bearer YOUR_KEY with two spaces.
  • A header called X-API-Key or api-key instead of Authorization. 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.py works, but API_KEY=abc on its own line does not reach child processes. Use export.
  • Shadowed by another value. Libraries can read OPENAI_API_KEY automatically. 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:

StatusUsual meaningWhere to look
401Missing or invalid keyHeader, key, base URL
429Rate limit or temporary overloadBack off and retry; see the docs for limits
400Malformed request bodyJSON, model name, parameters
404Wrong pathBase 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.

Questions and answers

Does a 401 mean I am out of credit?

No. A 401 is about authentication: the key is missing, malformed or unknown. Running out of credit is a separate condition.

Why does the same key work in curl but not in my app?

Your app is probably reading a different value, such as an old environment variable, a quoted .env entry or a key with trailing whitespace. Print the length and repr of the key your code actually uses.

Can I use my OpenAI key with this API?

No. Each key works only on the host that issued it. Use the key from the API key page together with this site's base URL.

Is a 401 charged?

No. Failed or refused requests are not charged.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.

Get API key