DOCS

接入 TOPAPI Connect to TOPAPI

在控制台创建 API Key,按工具填写 Base URL 与模型名,即可通过统一网关调用 Claude、ChatGPT、Grok 等模型,并支持 gpt-image-2 生图。 Create an API key in the console, set the Base URL and model name in your tool, and route Claude, ChatGPT, Grok, and more through one gateway — including gpt-image-2 image generation.

1

创建 API KeyCreate an API key

打开 控制台 → 密钥管理 → 创建。下文中的 YOUR_API_KEY 全部替换为真实密钥。Open the console → API keys → create. Replace every YOUR_API_KEY below with your real key.

2

选对 Base URLPick the right Base URL

OpenAI / Codex / GPT:https://api.topopenai.com/v1。Claude / Anthropic:https://api.topopenai.com(不要加 /v1)。OpenAI / Codex / GPT: https://api.topopenai.com/v1. Claude / Anthropic: https://api.topopenai.com (no trailing /v1).

3

按工具粘贴配置Paste config for your tool

跳到对应段落复制命令即可,无需通读全文。Jump to your tool section and copy the commands. You do not need to read everything.

三个必填项Three required fields

不同软件按钮文案不同,但本质上都在要这三样。 Labels differ by app, but they always ask for the same three values.

字段Field 含义Meaning 示例Example
API Key 控制台创建的密钥Key from the TOPAPI console YOUR_API_KEY
Base URL 请求发往的网关地址Gateway endpoint https://api.topopenai.com/v1
https://api.topopenai.com
Model 要调用的模型名Model name to call gpt-5.6 · claude-opus-5 · grok-4.6 · gpt-image-2

地址口诀:看到 OpenAI / Codex / GPT / ChatGPT → 用带 /v1 的地址;看到 Claude / Anthropic → 用根地址,不要加 /v1 Endpoint rule: OpenAI / Codex / GPT / ChatGPT → URL with /v1. Claude / Anthropic → root URL without /v1.

选择你的工具Choose your tool

点进对应章节,复制命令即可。 Open the matching section and copy the commands.

CC Switch

CC Switch 是跨平台桌面工具,可统一管理 Claude Code、Codex、Grok Build、OpenClaw、Hermes 等配置。推荐先安装 CC Switch,再从 TOPAPI 控制台一键导入密钥。 CC Switch is a cross-platform desktop app for managing Claude Code, Codex, Grok Build, OpenClaw, Hermes, and more. Install it first, then import your TOPAPI key from the console in one click.

下载与安装Download & install

渠道Channel 地址 / 命令Link / command
官方网站Official site https://ccswitch.io
GitHub 发布页GitHub Releases https://github.com/farion1231/cc-switch/releases
macOS Homebrew brew install --cask cc-switch
Windows 从 Releases 下载 .msi 安装包或 Portable .zipDownload the .msi installer or portable .zip from Releases
Linux 从 Releases 下载对应架构包;Arch 可用 paru -S cc-switch-binDownload the matching package from Releases; Arch users can run paru -S cc-switch-bin
macOS · Homebrew
brew install --cask cc-switch

方式一:控制台一键导入(推荐)Option 1: One-click import from console (recommended)

1

安装并打开 CC SwitchInstall and open CC Switch

确保系统已注册 ccswitch:// 协议(正常安装后会自动完成)。Make sure the ccswitch:// protocol is registered (this happens automatically after a normal install).

2

在 TOPAPI 创建或复制 API KeyCreate or copy an API key in TOPAPI

打开 控制台 → 密钥管理。Open the console → API keys.

3

点击「导入到 CCS」Click “Import to CCS”

浏览器会唤起 CC Switch,并自动写入 Provider(名称、Endpoint、API Key 等)。按提示确认导入即可。The browser opens CC Switch and fills in the provider (name, endpoint, API key, and more). Confirm the import when prompted.

若点击后无反应,多半是未安装 CC Switch 或协议未注册。请先安装最新版,或改用下方手动配置。 If nothing happens after clicking, CC Switch is probably not installed or the protocol handler is missing. Install the latest release, or use manual setup below.

方式二:在 CC Switch 中手动添加Option 2: Add manually in CC Switch

在 CC Switch 中为对应应用添加 Provider,填写 TOPAPI 的 API Key 与 Base URL: In CC Switch, add a provider for the target app and fill in your TOPAPI API key and Base URL:

应用 / 协议App / protocol Endpoint / Base URL 建议模型Starter model
Claude Code https://api.topopenai.com claude-opus-5
Codex https://api.topopenai.com gpt-5.6
Grok Build https://api.topopenai.com/v1 grok-4.6

导入或添加成功后,在 CC Switch 中切换到该 Provider,即可让 Claude Code / Codex / Grok Build 走 TOPAPI。更多功能见 ccswitch.io After import, switch to that provider in CC Switch so Claude Code / Codex / Grok Build route through TOPAPI. See ccswitch.io for more features.

Grok Build

Grok Build 是 xAI 官方终端编码 Agent(命令 grok)。安装后把推理地址指到 TOPAPI,即可用 TOPAPI Key 调用 Grok 系列模型。 Grok Build is xAI’s official terminal coding agent (command grok). Point its inference base URL at TOPAPI and use your TOPAPI key with Grok models.

下载与安装Download & install

渠道Channel 地址 / 命令Link / command
官方文档Official docs https://docs.x.ai/build/overview
macOS / Linux curl -fsSL https://x.ai/cli/install.sh | bash
Windows PowerShell irm https://x.ai/cli/install.ps1 | iex
npm npm install -g @xai-official/grok
Install
curl -fsSL https://x.ai/cli/install.sh | bash
Install
irm https://x.ai/cli/install.ps1 | iex
Install
npm install -g @xai-official/grok

接入 TOPAPIConnect to TOPAPI

1

设置环境变量(当前终端)Set env vars (current session)

Key 使用 TOPAPI 控制台创建的密钥;Base URL 带 /v1Use a TOPAPI console key; Base URL must include /v1.

2

写入 ~/.grok/config.tomlWrite ~/.grok/config.toml

Windows 路径为 %USERPROFILE%\.grok\config.toml。务必设置 api_backend = "responses"On Windows: %USERPROFILE%\.grok\config.toml. Always set api_backend = "responses".

3

验证并启动Verify and start

grok inspect · grok -p "你好" -m grok-4.6

Env · current session
export GROK_MODELS_BASE_URL="https://api.topopenai.com/v1"
export XAI_API_KEY="YOUR_API_KEY"
Env · current session
$env:GROK_MODELS_BASE_URL = "https://api.topopenai.com/v1"
$env:XAI_API_KEY = "YOUR_API_KEY"
~/.grok/config.toml
[endpoints]
models_base_url = "https://api.topopenai.com/v1"

[auth]
preferred_method = "api_key"

[model."grok-4.6"]
model = "grok-4.6"
name = "Grok 4.6"
env_key = "XAI_API_KEY"
api_backend = "responses"
context_window = 500000

[model."grok-build-0.1"]
model = "grok-build-0.1"
name = "Grok Build"
env_key = "XAI_API_KEY"
api_backend = "responses"
context_window = 256000

[models]
default = "grok-4.6"
web_search = "grok-4.6"
Verify
grok inspect
grok -p "用一句话介绍 TOPAPI" -m grok-4.6

走 TOPAPI 时请用 API Key 鉴权,不要再走 grok login 的 xAI 账号登录。也可在控制台密钥页点「使用密钥」→ Grok CLI 生成完整配置。模型名以控制台实际可用列表为准。 With TOPAPI, use API-key auth — do not use grok login for xAI account login. You can also open the console API key page → Use key → Grok CLI for a full config. Model names follow the console catalog.

Pi

Pi 是轻量可扩展的终端编码 Agent(pi.dev)。通过 ~/.pi/agent/models.json 添加 OpenAI 兼容 Provider,即可接入 TOPAPI。 Pi is a minimal, extensible terminal coding agent (pi.dev). Add an OpenAI-compatible provider in ~/.pi/agent/models.json to route through TOPAPI.

下载与安装Download & install

渠道Channel 地址 / 命令Link / command
官方网站Official site https://pi.dev
文档Docs https://pi.dev/docs/latest
macOS / Linux curl -fsSL https://pi.dev/install.sh | sh
Windows PowerShell irm https://pi.dev/install.ps1 | iex
GitHub https://github.com/earendil-works/pi
Install
curl -fsSL https://pi.dev/install.sh | sh
Install
irm https://pi.dev/install.ps1 | iex

接入 TOPAPIConnect to TOPAPI

编辑配置文件(Linux / macOS:~/.pi/agent/models.json;Windows:%USERPROFILE%\.pi\agent\models.json),添加 TOPAPI Provider: Edit the config file (Linux/macOS: ~/.pi/agent/models.json; Windows: %USERPROFILE%\.pi\agent\models.json) and add a TOPAPI provider:

~/.pi/agent/models.json
{
  "providers": {
    "topapi": {
      "baseUrl": "https://api.topopenai.com/v1",
      "api": "openai-completions",
      "apiKey": "YOUR_API_KEY",
      "models": [
        {
          "id": "gpt-5.6",
          "name": "GPT-5.6 via TOPAPI"
        },
        {
          "id": "claude-opus-5",
          "name": "Claude Opus via TOPAPI"
        },
        {
          "id": "grok-4.6",
          "name": "Grok 4.6 via TOPAPI"
        },
        {
          "id": "gpt-image-2",
          "name": "GPT Image 2 via TOPAPI"
        }
      ]
    }
  }
}
Run
# 若 models.json 里用了环境变量引用,可先导出:
# export TOPAPI_API_KEY="YOUR_API_KEY"
pi --provider topapi -p "用一句话介绍 TOPAPI"
Run
# If models.json references an env var, export it first:
# $env:TOPAPI_API_KEY = "YOUR_API_KEY"
pi --provider topapi -p "用一句话介绍 TOPAPI"

也可用 api: "openai-responses" 走 Responses 协议(视网关与模型支持情况)。会话内用 /modelCtrl+L 切换模型。模型 id 与控制台可用列表保持一致即可。 You can also set api: "openai-responses" when the gateway and model support Responses. Switch models mid-session with /model or Ctrl+L. Keep model ids aligned with the console catalog.

Codex

优先使用一键脚本。脚本会安装或配置 Codex,并写入 TOPAPI 地址。 Prefer the installer. It installs or configures Codex and writes the TOPAPI endpoint.

Install
curl -fsSL https://api.topopenai.com/install/codex.sh | sudo bash
Install
irm https://api.topopenai.com/install/codex.ps1 | iex

运行后按提示填入 TOPAPI Key。能正常对话即表示接入成功。 Paste your TOPAPI key when prompted. A normal reply means setup succeeded.

脚本失败时的手动配置Manual config if the script fails

~/.codex
mkdir -p ~/.codex
cat > ~/.codex/config.toml <<'EOF'
model = "gpt-5.6"
model_provider = "openai"
openai_base_url = "https://api.topopenai.com/v1"
cli_auth_credentials_store = "file"
forced_login_method = "api"
EOF

cat > ~/.codex/auth.json <<'EOF'
{"OPENAI_API_KEY":"YOUR_API_KEY"}
EOF

复制前把 YOUR_API_KEY 换成真实密钥。不要把密钥发给他人或提交到公开仓库。 Replace YOUR_API_KEY before running. Never share the key or commit it to a public repo.

Claude Code

使用 Anthropic 协议。Base URL 填 https://api.topopenai.com,不要加 /v1 Uses the Anthropic protocol. Set Base URL to https://api.topopenai.com without /v1.

Current session
export ANTHROPIC_BASE_URL="https://api.topopenai.com"
export ANTHROPIC_AUTH_TOKEN="YOUR_API_KEY"
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
claude
Current session
$env:ANTHROPIC_BASE_URL = "https://api.topopenai.com"
$env:ANTHROPIC_AUTH_TOKEN = "YOUR_API_KEY"
$env:CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = "1"
claude

以上环境变量仅对当前终端会话有效。新开终端需要重新执行,或写入 shell 配置文件。 These env vars apply to the current terminal session only. Re-run them in a new shell, or persist them in your shell profile.

Cursor

在 Cursor 中通过「覆盖 OpenAI Base URL」接入 TOPAPI。Cursor 侧需要用模型映射:在设置里添加短名称,例如 g-5.6cc-5g-4.6,分别对应 gpt-5.6claude-opus-5grok-4.6 Point Cursor at TOPAPI with Override OpenAI Base URL. Cursor requires model mapping: add short names such as g-5.6, cc-5, and g-4.6 for gpt-5.6, claude-opus-5, and grok-4.6.

配置步骤Setup steps

1

打开 Cursor 设置Open Cursor Settings

Cursor SettingsModels(或设置搜索 “Models / OpenAI”)。Cursor SettingsModels (or search “Models / OpenAI”).

2

填写 API Key 与 Base URLSet API key and Base URL

OpenAI API Key:填 TOPAPI 控制台密钥。Override OpenAI Base URL:开启后填 https://api.topopenai.com/v1(必须带 /v1)。OpenAI API Key: your TOPAPI console key. Override OpenAI Base URL: enable and set https://api.topopenai.com/v1 (include /v1).

3

添加自定义模型(映射名)Add custom models (mapped names)

点击 Add Model / 添加模型,按下方映射表填写短名称,不要直接填完整上游 ID(除非已在网关配置透传)。Click Add Model and enter the short mapped names from the table below. Do not paste full upstream IDs unless your gateway already accepts them as-is.

4

启用并在对话中选择Enable and select in chat

打开对应模型开关,在 Chat / Agent 模型列表中选择 g-5.6 / cc-5 / g-4.6 等映射名。Toggle the models on, then pick mapped names like g-5.6, cc-5, or g-4.6 in Chat / Agent.

配置项Setting Value
OpenAI API Key YOUR_API_KEY
Override OpenAI Base URL https://api.topopenai.com/v1

模型映射(必做)Model mapping (required)

Cursor 通过自定义模型名发请求。请在 Cursor 中添加右侧短名称;网关侧按 短名称 → 真实模型 映射(写法:真实模型 = 短名称)。 Cursor sends the custom model name you add. Register the short name in Cursor; on the gateway map short name → real model (notation: real model = short name).

TOPAPI 真实模型TOPAPI model Cursor 中填写 / 映射名Name in Cursor (mapped) 映射写法Mapping form
gpt-5.6 g-5.6 gpt-5.6 = g-5.6
claude-opus-5 cc-5 claude-opus-5 = cc-5
grok-4.6 g-4.6 grok-4.6 = g-4.6
gpt-image-2 gpt-image-2 生图走 API;Cursor 聊天一般不用Image API model; usually not used in Cursor chat

重点:Cursor 里必须添加并选择映射后的短名称,不要直接填完整模型 ID。完整映射:gpt-5.6 = g-5.6claude-opus-5 = cc-5grok-4.6 = g-4.6 Important: In Cursor, add and select the short mapped names — not the full model IDs. Full mapping: gpt-5.6 = g-5.6, claude-opus-5 = cc-5, grok-4.6 = g-4.6.

若 Verify / 校验失败:检查 Base URL 是否带 /v1、Key 是否替换、以及模型开关是否打开。部分 Cursor 内置能力仍走官方通道,自定义 Base URL 主要作用于 Chat / Agent 等走 OpenAI 兼容接口的请求。 If Verify fails: check that Base URL includes /v1, the key is real, and the model toggle is on. Some built-in Cursor features still use Cursor’s backend; the custom Base URL mainly applies to Chat / Agent traffic over the OpenAI-compatible API.

TRAE / OpenClaw / Hermes

在「添加模型 / 自定义 Provider / API Provider」中填写 Key、地址与模型。按协议类型选对 Base URL。 In add model / custom provider / API provider, fill key, endpoint, and model. Match the Base URL to the protocol.

工具Tool 协议Protocol Base URL 建议模型Starter model
TRAE SOLO OpenAI / Codex https://api.topopenai.com/v1 gpt-5.6
TRAE SOLO Claude / Anthropic https://api.topopenai.com claude-opus-5
OpenClaw / Hermes OpenAI https://api.topopenai.com/v1 gpt-5.6
OpenClaw / Hermes Anthropic https://api.topopenai.com claude-opus-5

OpenClaw

OpenAI mode
export OPENAI_API_KEY="YOUR_API_KEY"
openclaw onboard --auth-choice custom-api-key \
  --custom-base-url https://api.topopenai.com/v1 \
  --custom-api-key-env OPENAI_API_KEY \
  --custom-compatibility openai \
  --custom-model gpt-5.6
Anthropic mode
export ANTHROPIC_API_KEY="YOUR_API_KEY"
openclaw onboard --auth-choice custom-api-key \
  --custom-base-url https://api.topopenai.com \
  --custom-api-key-env ANTHROPIC_API_KEY \
  --custom-compatibility anthropic \
  --custom-model claude-opus-5

同一把 TOPAPI Key 可用于多个工具。建议按工具拆分 Key,便于限额与吊销。 The same TOPAPI key works across tools. Prefer separate keys per tool for limits and revocation.

速查表Quick reference

只给你几个输入框时,对照这张表。 Use this table when an app only exposes a few fields.

场景Scenario Base URL Key 变量 / 字段 起步模型Starter model
OpenAI SDK / Codex https://api.topopenai.com/v1 OPENAI_API_KEY gpt-5.6
Claude Code https://api.topopenai.com ANTHROPIC_AUTH_TOKEN claude-opus-5
Image API https://api.topopenai.com/v1 Authorization: Bearer gpt-image-2
Cursor https://api.topopenai.com/v1 OpenAI API Key g-5.6 / cc-5 / g-4.6
TRAE · OpenAI https://api.topopenai.com/v1 API Key gpt-5.6
TRAE · Claude https://api.topopenai.com API Key claude-opus-5
CC Switch 推荐:控制台密钥页点「导入到 CCS」。下载: Recommended: click “Import to CCS” on the API keys page. Download: ccswitch.io · GitHub Releases
Grok Build https://api.topopenai.com/v1 XAI_API_KEY grok-4.6 / grok-build-0.1
Pi https://api.topopenai.com/v1 models.json 中的 apiKeyapiKey in models.json gpt-5.6

API 示例API examples

面向直接写代码的调用方。只配置 CLI / 桌面工具时可跳过。 For direct HTTP/SDK calls. Skip if you only configure CLI or desktop tools.

RESPONSES POST /v1/responses
CHAT POST /v1/chat/completions
ANTHROPIC POST /v1/messages
IMAGE POST /v1/images/generations

gpt-image-2

curl · responses
curl https://api.topopenai.com/v1/responses \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6",
    "input": "用中文简单介绍一下 TOPAPI。"
  }'
curl · chat completions
curl https://api.topopenai.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6",
    "messages": [
      {"role": "user", "content": "用一句话介绍 TOPAPI。"}
    ]
  }'
curl · messages
curl https://api.topopenai.com/v1/messages \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "用中文简单介绍一下 TOPAPI。"}
    ]
  }'
node · fetch
const response = await fetch("https://api.topopenai.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "gpt-5.6",
    messages: [{ role: "user", content: "用一句话介绍 TOPAPI。" }]
  })
});

console.log(await response.json());

生图模型:gpt-image-2。接口:POST /v1/images/generations,Base URL 使用带 /v1 的地址。 Image model: gpt-image-2. Endpoint: POST /v1/images/generations. Use the Base URL with /v1.

curl · gpt-image-2
curl https://api.topopenai.com/v1/images/generations \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "生成一张极简风格的 TOPAPI 产品图",
    "size": "1024x1024"
  }'

排错Troubleshooting

一次只改一项:Key → 地址 → 模型名。 Change one thing at a time: key → endpoint → model name.

不知道该用哪个地址?Which endpoint should I use?

OpenAI / Codex / GPT / ChatGPT → https://api.topopenai.com/v1。Claude / Anthropic → https://api.topopenai.com OpenAI / Codex / GPT / ChatGPT → https://api.topopenai.com/v1. Claude / Anthropic → https://api.topopenai.com.

Key 无效 / 没有权限Invalid key or unauthorized

确认已替换 YOUR_API_KEY、复制无多余空格、密钥属于当前 TOPAPI 账号且未禁用。 Confirm YOUR_API_KEY was replaced, no extra spaces were copied, and the key belongs to your active TOPAPI account.

模型名填什么?Which model name?

先跑通再换:OpenAI 用 gpt-5.6,Claude 用 claude-opus-5,Grok 用 grok-4.6,生图用 gpt-image-2。在 Cursor 里必须用映射名:g-5.6 / cc-5 / g-4.6(即 gpt-5.6 = g-5.6claude-opus-5 = cc-5grok-4.6 = g-4.6)。完整列表以控制台为准。 Start simple: gpt-5.6, claude-opus-5, grok-4.6, or gpt-image-2 for images. In Cursor, use mapped names: g-5.6 / cc-5 / g-4.6 (gpt-5.6 = g-5.6, claude-opus-5 = cc-5, grok-4.6 = g-4.6). Use the console catalog for the full list.

Cursor 里选不了模型 / 报模型无效?Cursor cannot select a model / invalid model?

Cursor 必须用映射短名称:在 Models 里添加并启用 g-5.6cc-5g-4.6,不要直接填完整 ID。映射为 gpt-5.6 = g-5.6claude-opus-5 = cc-5grok-4.6 = g-4.6。同时确认 Base URL 为 https://api.topopenai.com/v1。详见 Cursor Cursor requires short mapped names: add and enable g-5.6, cc-5, and g-4.6 under Models — not full IDs. Mapping: gpt-5.6 = g-5.6, claude-opus-5 = cc-5, grok-4.6 = g-4.6. Base URL must be https://api.topopenai.com/v1. See Cursor.

命令找不到 / 无响应Command not found or no response

Windows 用 PowerShell,macOS / Linux 用终端。提示 npm / node 不存在时,先安装 Node.js LTS。Claude Code 的环境变量仅对当前会话生效。 Use PowerShell on Windows and Terminal on macOS/Linux. Install Node.js LTS if npm/node is missing. Claude Code env vars apply to the current session only.

API_KEY_REQUIRED / INVALID_API_KEY

检查 Authorization: Bearer ... 请求头是否完整,以及是否误把示例占位符提交上去。 Check that the Authorization: Bearer ... header is complete and that the placeholder was not left in place.

「导入到 CCS」没反应?“Import to CCS” does nothing?

先安装 CC Switch 并打开一次,再重试。仍失败则在 CC Switch 里手动添加 Provider,地址与模型见 CC Switch 章节。 Install CC Switch, open it once, then try again. If it still fails, add a provider manually using the endpoints in the CC Switch section.

Grok Build 连不上 TOPAPI?Grok Build cannot reach TOPAPI?

确认已设置 GROK_MODELS_BASE_URL=https://api.topopenai.com/v1XAI_API_KEY,且 config.toml 中每个模型都写了 api_backend = "responses"。不要使用 grok login 的 xAI 登录。详见 Grok Build Confirm GROK_MODELS_BASE_URL=https://api.topopenai.com/v1 and XAI_API_KEY are set, and every model in config.toml has api_backend = "responses". Do not use grok login for xAI. See Grok Build.

Pi 如何指定 TOPAPI 模型?How does Pi select a TOPAPI model?

~/.pi/agent/models.json 里配置 Provider 与模型 id,启动时加 --provider topapi,会话中用 /model 切换。见 Pi Configure the provider and model ids in ~/.pi/agent/models.json, start with --provider topapi, and switch with /model mid-session. See Pi.