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

# 认证

> XRouter API Key 在 OpenAI、Anthropic Messages 和 Gemini 兼容接口中的认证方式。

XRouter API Key 通常以 `sk-` 开头。不同接口格式使用的认证头不同，但都使用同一把 XRouter API Key。

<Warning>
  不要把真实 API Key 写入客户端前端代码、公开仓库、Issue、截图或聊天记录中。
</Warning>

## OpenAI 兼容接口

OpenAI 兼容接口使用 Bearer token。

```http theme={null}
Authorization: Bearer sk-your-api-key
```

示例：

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

适用端点包括：

* `/v1/models`
* `/v1/chat/completions`
* `/v1/responses`

## Anthropic Messages 接口

Anthropic Messages 格式通常使用 `x-api-key`，并带上 `anthropic-version`。

```http theme={null}
x-api-key: sk-your-api-key
anthropic-version: 2023-06-01
```

示例：

```bash Terminal theme={null}
curl "https://api.xrouter.dev/v1/messages" \
  -H "x-api-key: sk-your-api-key" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-claude-model-id",
    "max_tokens": 256,
    "messages": [
      { "role": "user", "content": "Hello" }
    ]
  }'
```

XRouter 也兼容 Bearer token：

```http theme={null}
Authorization: Bearer sk-your-api-key
```

## Gemini 兼容接口

Gemini 兼容接口推荐使用 `x-goog-api-key`。

```http theme={null}
x-goog-api-key: sk-your-api-key
```

示例：

```bash Terminal theme={null}
curl "https://api.xrouter.dev/v1beta/models/your-gemini-model-id:generateContent" \
  -H "x-goog-api-key: sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          { "text": "Hello" }
        ]
      }
    ]
  }'
```

部分客户端只支持 `key` 查询参数，XRouter 也兼容：

```text theme={null}
https://api.xrouter.dev/v1beta/models/your-gemini-model-id:generateContent?key=sk-your-api-key
```

<Warning>
  优先使用请求头。查询参数可能被浏览器历史、代理日志或应用日志记录。
</Warning>

## 模型列表的返回格式

`/v1/models` 会根据请求头判断返回格式：

| 请求头                               | 返回格式           |
| --------------------------------- | -------------- |
| `Authorization: Bearer ...`       | OpenAI 模型列表    |
| `x-api-key` + `anthropic-version` | Anthropic 模型列表 |
| `x-goog-api-key` 或 `key` 查询参数     | Gemini 模型列表    |
