> ## Documentation Index
> Fetch the complete documentation index at: https://docs.laozhang.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Invalid API key or 404: set the base URL

> Fix invalid_api_key, 401, and 404 errors with a LaoZhang API key: set the right base URL for the OpenAI, Anthropic, and Gemini SDKs.

An `invalid_api_key` or 404 error usually means the request never reached LaoZhang API or the base URL path is wrong; the key itself is rarely the problem. A LaoZhang API key only works with LaoZhang API hosts, set as shown below.

| Item | Details |
| - | - |
| OpenAI SDK base URL | `https://api.laozhang.ai/v1` |
| Anthropic SDK base URL | `https://api.laozhang.ai` (the SDK adds `/v1/messages`) |
| Google Gen AI SDK | `base_url` set to `https://api.laozhang.ai`, `api_version` set to `v1beta` |
| Authentication | `Authorization: Bearer YOUR_LAOZHANG_API_KEY` |
| Quick check | `GET https://api.laozhang.ai/v1/models` |

## Match the error to its cause

| What you see | Most likely cause | Fix |
| - | - | - |
| `Incorrect API key provided: sk-...` | A LaoZhang API key sent to `api.openai.com` | Change the base URL to `https://api.laozhang.ai/v1` |
| 401 while already using a LaoZhang host | The key was copied partially or with spaces, or the token is disabled or expired | Copy the key again and check the token in [Tokens](https://api.laozhang.ai/token) |
| 404 Not Found | `/v1` missing for the OpenAI SDK, or added for the Anthropic SDK | Compare the path with the table above |
| `//` in the request path | A trailing `/` on the base URL, or a path joined twice | Remove the trailing slash and let the SDK build the path |
| SSL or connection errors | `https://` is missing, or your network can't reach the default host | Add `https://`, or try a fallback host below |

## Configure each SDK

Each SDK builds request paths differently. The OpenAI SDK appends only the resource name, so its base URL must include `/v1`. The Anthropic SDK appends `/v1/messages` itself, so adding `/v1` produces `/v1/v1/messages` and a 404.

<Tabs>
  <Tab title="OpenAI SDK">
    ```python theme={null}
    import os
    from openai import OpenAI

    client = OpenAI(
        api_key=os.environ["LAOZHANG_API_KEY"],
        base_url="https://api.laozhang.ai/v1",
    )

    response = client.chat.completions.create(
        model="gpt-5.4-mini",
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="Anthropic SDK">
    ```python theme={null}
    import os
    import anthropic

    client = anthropic.Anthropic(
        api_key=os.environ["LAOZHANG_API_KEY"],
        base_url="https://api.laozhang.ai",  # no /v1
    )

    message = client.messages.create(
        model="glm-5.2",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(message.content[0].text)
    ```
  </Tab>

  <Tab title="Google Gen AI SDK">
    ```python theme={null}
    import os
    from google import genai

    client = genai.Client(
        api_key=os.environ["LAOZHANG_API_KEY"],
        http_options={"base_url": "https://api.laozhang.ai", "api_version": "v1beta"},
    )

    response = client.models.generate_content(model="gemini-3.8-flash", contents="Hello")
    print(response.text)
    ```
  </Tab>
</Tabs>

Set the `LAOZHANG_API_KEY` environment variable before you run these. To see which models accept the Anthropic format, check the model list in [Claude protocol](/en/api-reference/claude).

## Configure with environment variables

Many tools read OpenAI's standard environment variables, so you can leave the base URL out of your code:

```bash theme={null}
export OPENAI_API_KEY="YOUR_LAOZHANG_API_KEY"
export OPENAI_BASE_URL="https://api.laozhang.ai/v1"
```

If the error persists after you change the URL, look for settings that override each other:

* Your code, a `.env` file, and the shell each set a different URL.
* A third-party client still has an old provider saved.
* The process wasn't restarted and still reads the old variables.

In third-party clients, look for a "Custom API" or "OpenAI-compatible" provider. Enter `https://api.laozhang.ai/v1` as the API URL, your LaoZhang API key, and a full model ID from the [model catalog](/en/models).

## Confirm the setup with one request

```bash theme={null}
curl https://api.laozhang.ai/v1/models \
  -H "Authorization: Bearer $LAOZHANG_API_KEY"
```

A `data` array means the key and host are both correct; the array lists the models this token can call. If you still get a 401, check in [Tokens](https://api.laozhang.ai/token) that the token is enabled, hasn't expired, and has quota left, or create a new token and try again.

## Fallback hosts

All three hosts accept the same API key, paths, and parameters. Only the hostname changes:

* Default: `api.laozhang.ai`
* Direct connections from Europe or North America: `api-vip.laozhang.ai`
* When `api.laozhang.ai` can't be reached: `api2.laozhang.ai`

For the full setup, see [Connect your application to LaoZhang API](/en/api-manual).

## Still stuck?

Email [hi@laozhang.ai](mailto:hi@laozhang.ai) with:

* the request time and time zone;
* the full request URL (without the key) and the SDK you use;
* the HTTP status and the full error body;
* the first 6 and last 4 characters of the key.

Never send the full API key. If the key has appeared in a chat, screenshot, or repository, revoke and replace it first, as described in [API key management](/en/faq/token-management).

## Related pages

* [Connect your application to LaoZhang API](/en/api-manual)
* [Create, store, rotate, and revoke API keys](/en/faq/token-management)
* [Check model availability and access](/en/faq/model-availability)
* [Models API: list available model IDs](/en/api-reference/models)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.