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

# 请求认证：Bearer、x-api-key、x-goog-api-key

> Anyone 所有端点使用同一个 API key。根据你使用的 API 格式，以 Bearer token、x-api-key 或 x-goog-api-key 传递。

每个发送到 Anyone 的请求都必须包含 API key 来识别你的账户并授权访问。你在控制台创建 API key，同一个 API key 适用于所有端点格式——OpenAI、Anthropic Claude 和 Google Gemini。唯一的区别是使用哪个头来传递。

## 获取你的 API Key

<Steps>
  <Step title="打开控制台">
    在浏览器中打开 Anyone，例如 `https://anyone.ai`。
  </Step>

  <Step title="进入 API Key 管理">
    点击侧边栏的 **API Key 管理**。
  </Step>

  <Step title="创建 API Key">
    点击 **添加 API Key**，起个名字，配置模型限制或配额，然后点击 **提交**。
  </Step>

  <Step title="复制 API Key">
    复制显示的 API key 值。Anyone 只显示一次完整 API key，请妥善保存。
  </Step>
</Steps>

## 认证方式

Anyone 支持三种认证方式，对应每个 API 系列的原生格式。三种方式使用同一个 API key 值——只是头名称不同。

### OpenAI 兼容（大多数端点）

在 `Authorization` 头中以 Bearer API key 传递。所有标准端点使用此方式。

**适用于：** `POST /v1/chat/completions`、`POST /v1/completions`、`POST /v1/responses`、`POST /v1/embeddings`、`POST /v1/audio/*`、`POST /v1/images/*`、`POST /v1/rerank`、`GET /v1/realtime`、`GET /v1/models`

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.anyone.ai/v1/chat/completions \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Hello"}]
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_TOKEN",
      base_url="https://api.anyone.ai/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello"}]
  )
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: "YOUR_TOKEN",
    baseURL: "https://api.anyone.ai/v1",
  });

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

### Anthropic Claude

在 `x-api-key` 头中传递 API key，并包含 `anthropic-version` 头。Anyone 用这些头来识别应以 Claude Messages 格式处理的请求。

**适用于：** `POST /v1/messages`

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.anyone.ai/v1/messages \
    -H "x-api-key: YOUR_TOKEN" \
    -H "anthropic-version: 2023-06-01" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "claude-sonnet-4-6",
      "max_tokens": 1024,
      "messages": [{"role": "user", "content": "Hello"}]
    }'
  ```

  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      api_key="YOUR_TOKEN",
      base_url="https://api.anyone.ai",
  )

  message = client.messages.create(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      messages=[{"role": "user", "content": "Hello"}],
  )
  ```

  ```javascript JavaScript theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic({
    apiKey: "YOUR_TOKEN",
    baseURL: "https://api.anyone.ai",
  });

  const message = await client.messages.create({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: [{ role: "user", content: "Hello" }],
  });
  ```
</CodeGroup>

### Google Gemini

在 `x-goog-api-key` 头中传递 API key，或作为 `key` 查询参数。两者等效。

**适用于：** `POST /v1beta/models/{model}:generateContent`

<CodeGroup>
  ```bash cURL（头方式） theme={null}
  curl "https://api.anyone.ai/v1beta/models/gemini-3.1-pro-preview:generateContent" \
    -H "x-goog-api-key: YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [{"parts": [{"text": "Hello"}]}]
    }'
  ```

  ```bash cURL（查询参数） theme={null}
  curl "https://api.anyone.ai/v1beta/models/gemini-3.1-pro-preview:generateContent?key=YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [{"parts": [{"text": "Hello"}]}]
    }'
  ```

  ```python Python theme={null}
  import google.generativeai as genai

  genai.configure(
      api_key="YOUR_TOKEN",
      client_options={"api_endpoint": "https://api.anyone.ai"},
  )

  model = genai.GenerativeModel("gemini-3.1-pro-preview")
  response = model.generate_content("Hello")
  ```
</CodeGroup>

## 认证错误

| 状态码                | 原因                    | 修复方法                       |
| ------------------ | --------------------- | -------------------------- |
| `401 Unauthorized` | API key 缺失、格式错误或已被删除。 | 检查你发送的头是否正确，API key 值是否完整。 |
| `403 Forbidden`    | API key 存在但无权访问请求的模型。 | 在控制台编辑 API key，检查其允许的模型列表。 |

## 安全建议

<Warning>
  永远不要将 API key 放在客户端代码、浏览器 JavaScript 或公开仓库中。如果 API key 泄露，立即在控制台删除并创建新的。
</Warning>

将 API key 存储在环境变量或密钥管理器中，在运行时传递给应用。在服务器端，像对待任何其他 API 凭据一样对待你的 Anyone API key。
