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

# Why is my API Key invalid?

> Resolve API Key invalid errors and learn correct Base URL configuration

## Common Error

When you see an error like this:

```json theme={null}
{
  "error": {
    "message": "Incorrect API key provided: sk-QqHvK***...",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}
```

This usually **isn't** a problem with your API Key itself, but rather a **Base URL misconfiguration**.

<Warning>
  Most common mistake: Using GravitexAI's Key but the request URL still points to OpenAI's `https://api.openai.com`
</Warning>

## What is Base URL?

**Base URL** is the target server address for API requests. Different providers use different Base URLs.

### Base URL and API Key Must Match

| Provider            | Base URL                                           | API Key Format | Match?      |
| ------------------- | -------------------------------------------------- | -------------- | ----------- |
| **GravitexAI**      | [https://api.gravitex.ai](https://api.gravitex.ai) | sk-xxxx...     | ✅ Correct   |
| **OpenAI Official** | [https://api.openai.com](https://api.openai.com)   | sk-xxxx...     | ✅ Correct   |
| ❌ GravitexAI Key    | [https://api.openai.com](https://api.openai.com)   | sk-xxxx...     | ❌ **Wrong** |
| ❌ OpenAI Key        | [https://api.gravitex.ai](https://api.gravitex.ai) | sk-xxxx...     | ❌ **Wrong** |

<Note>
  Key principle: Use the provider's Base URL that matches your API Key
</Note>

## Correct Configuration

### Method 1: Modify Base URL (Recommended)

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

    client = OpenAI(
        api_key="sk-your-gravitex-key",
        base_url="https://api.gravitex.ai/v1"
    )

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello"}]
    )
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    import OpenAI from 'openai';

    const client = new OpenAI({
      apiKey: 'sk-your-gravitex-key',
      baseURL: 'https://api.gravitex.ai/v1'
    });

    const response = await client.chat.completions.create({
      model: 'gpt-4o',
      messages: [{ role: 'user', content: 'Hello' }]
    });
    ```
  </Tab>

  <Tab title="cURL">
    ```bash theme={null}
    curl https://api.gravitex.ai/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-your-gravitex-key" \
      -d '{
        "model": "gpt-4o",
        "messages": [{"role": "user", "content": "Hello"}]
      }'
    ```
  </Tab>
</Tabs>

### Method 2: Environment Variables

<Tabs>
  <Tab title="Linux/macOS">
    ```bash theme={null}
    export OPENAI_API_KEY="sk-your-gravitex-key"
    export OPENAI_BASE_URL="https://api.gravitex.ai/v1"
    ```
  </Tab>

  <Tab title="Windows PowerShell">
    ```powershell theme={null}
    $env:OPENAI_API_KEY="sk-your-gravitex-key"
    $env:OPENAI_BASE_URL="https://api.gravitex.ai/v1"
    ```
  </Tab>

  <Tab title="Windows CMD">
    ```cmd theme={null}
    set OPENAI_API_KEY=sk-your-gravitex-key
    set OPENAI_BASE_URL=https://api.gravitex.ai/v1
    ```
  </Tab>
</Tabs>

## Supported URL Formats

| Format                 | URL                                           | Use Case        |
| ---------------------- | --------------------------------------------- | --------------- |
| With /v1 (recommended) | `https://api.gravitex.ai/v1`                  | Most libraries  |
| With trailing slash    | `https://api.gravitex.ai/v1/`                 | Some frameworks |
| Full path              | `https://api.gravitex.ai/v1/chat/completions` | cURL requests   |

## Troubleshooting

<AccordionGroup>
  <Accordion title="Changed Base URL but still getting errors">
    **Possible causes**:

    1. **Multiple configurations**: Check config files, env vars, code initialization
    2. **Proxy or middleware**: Some tools may redirect requests
    3. **Cache issues**: Restart program or clear cache
    4. **Typos**: Verify the URL spelling
  </Accordion>

  <Accordion title="How to verify Key is valid?">
    Check in GravitexAI console:

    1. Login to [GravitexAI](https://maas.gravitex.ai)
    2. Go to "Tokens" page
    3. Check if Key status is "Enabled"
    4. Confirm account balance is sufficient
  </Accordion>

  <Accordion title="How to configure third-party tools?">
    Most tools have "Custom API" options:

    * **API URL / Base URL**: `https://api.gravitex.ai/v1`
    * **API Key**: Copy from GravitexAI console
    * **Model name**: Refer to model list
  </Accordion>
</AccordionGroup>

## Wrong vs Correct Examples

### ❌ Wrong Configuration

```python theme={null}
client = OpenAI(
    api_key="sk-gravitex-key",
    base_url="https://api.openai.com/v1"
    # ❌ Using OpenAI's URL
)
```

**Result**: OpenAI server rejects GravitexAI's Key

### ✅ Correct Configuration

```python theme={null}
client = OpenAI(
    api_key="sk-gravitex-key",
    base_url="https://api.gravitex.ai/v1"
    # ✅ Using GravitexAI URL
)
```

**Result**: Request successfully sent to GravitexAI

## Quick Test

Verify configuration with cURL:

```bash theme={null}
curl https://api.gravitex.ai/v1/models \
  -H "Authorization: Bearer sk-your-gravitex-key"
```

**Expected result**: Returns available models list

```json theme={null}
{
  "data": [
    {
      "id": "gpt-4o",
      "object": "model"
    }
  ]
}
```

If you get an error, check:

1. API Key copied correctly (no extra spaces)
2. Network connection is working
3. Account balance is sufficient

<Tip>
  Remember: Match the Key to its URL. GravitexAI Key uses `https://api.gravitex.ai/v1`
</Tip>
