这个页面不是抽象介绍页,而是为了接住这些真实搜索需求: Claude API 国内怎么用、Codex base_url 怎么改、OpenAI API 怎么充值、Claude Code / Codex / OpenCode / OpenClaw 怎么接中转站。 统一 gateway 是 https://api.xai-router.com,统一环境变量是 XX_API_KEY。 This is not an abstract product page. It is built to capture practical search intent: how to use Claude API domestically, how to replace Codex base_url, how to recharge for OpenAI API usage, and how to connect Claude Code, Codex, OpenCode, or OpenClaw to a gateway. The shared gateway is https://api.xai-router.com, and the shared environment variable is XX_API_KEY.
先解决实际问题。 你最可能是为了让工具跑起来,而不是为了研究 API 平台抽象概念。 Start with the real task. Most people arrive here because they want a tool to run, not because they want platform theory.
文档负责接搜索流量。 所以这里优先给你 gateway、key、base_url、代码片段和常见报错说明。 Docs are here to capture search traffic. That means this page should prioritize gateway, key, base_url, config snippets, and common error handling.
如果你还没注册或充值。 看完配置以后,直接回主站开通,然后再回来复制代码即可。 If you have not registered or recharged yet. Finish reading the setup, open an account on the main site, and then come back for the exact snippets you need.
先去主站创建账号并充值。这个文档页负责配置说明,真正的开户、余额和计费展示都在控制台。 Create your account and recharge on the main site first. This docs page focuses on setup, while account opening, balance, and billing live in the main console.
统一 gateway:https://api.xai-router.com,统一环境变量:XX_API_KEY。
Use the shared gateway https://api.xai-router.com and the shared environment variable XX_API_KEY.
你不需要理解所有术语,只需要找到你要用的工具,复制对应配置片段,然后启动它。 You do not need to understand every abstraction. Just find your tool, copy the right snippet, and launch it.
Claude Code 国内怎么用、Codex base_url 怎么改、OpenAI API 支付宝充值、Cursor / OpenCode / OpenClaw 怎么接中转站。 How to use Claude Code domestically, how to replace Codex base_url, how to recharge for OpenAI API, and how to connect Cursor, OpenCode, or OpenClaw.
本页所有示例都基于同一条接入链路和同一个 key 变量名: Every example on this page uses the same gateway and the same API key variable:
Codex 最常见的搜索问题是“base_url 怎么换、wire_api 该写什么、环境变量怎么放”。这里直接给你能跑的配置。
The most common Codex questions are how to replace base_url, what wire_api should be, and where the environment variable belongs. The snippet below is the direct working configuration.
配置文件路径:Linux / macOS 用 ~/.codex/config.toml,Windows 用 %USERPROFILE%\.codex\config.toml。 Config file path: use ~/.codex/config.toml on Linux / macOS and %USERPROFILE%\.codex\config.toml on Windows.
model_provider = "xai"
model = "gpt-5.5"
model_reasoning_effort = "xhigh"
plan_mode_reasoning_effort = "xhigh"
model_reasoning_summary = "none"
model_verbosity = "medium"
approval_policy = "never"
sandbox_mode = "danger-full-access"
[model_providers.xai]
name = "OpenAI"
base_url = "https://api.xai-router.com"
wire_api = "responses"
requires_openai_auth = false
env_key = "XX_API_KEY"
echo 'export XX_API_KEY="sk-..."' >> ~/.bashrc
source ~/.bashrc
codex
echo 'export XX_API_KEY="sk-..."' >> ~/.zshrc
source ~/.zshrc
codex
setx XX_API_KEY "sk-..."
:: Reopen CMD, then run
codex
[Environment]::SetEnvironmentVariable("XX_API_KEY", "sk-...", "User")
# Reopen PowerShell, then run
codex
~/.zshrc 后仍读不到 XX_API_KEY。
如果 Codex 启动后还报缺少 key,把导出语句同步写进 ~/.bash_profile 和 ~/.bashrc。
Some older macOS terminals, IDE sessions, or bash-inherited login environments may still miss XX_API_KEY after you only update ~/.zshrc.
If Codex still cannot read the key, mirror the export into ~/.bash_profile and ~/.bashrc.
Claude Code 在这个站点上的核心思路是:继续使用它熟悉的 Anthropic 兼容变量名,但把请求映射到 XAI Router 的 GPT 链路上。 For Claude Code, the practical pattern is to keep using Anthropic-compatible environment variables while routing traffic to the XAI Router GPT stack underneath.
顺序上,先设置环境变量,再运行 claude。
Set the environment variables first, then launch claude.
export XX_API_KEY="sk-..."
export ANTHROPIC_AUTH_TOKEN="$XX_API_KEY"
export ANTHROPIC_BASE_URL="https://api.xai-router.com"
export ANTHROPIC_DEFAULT_OPUS_MODEL="claude-opus-4-6"
export ANTHROPIC_DEFAULT_SONNET_MODEL="claude-sonnet-4-6"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="claude-haiku-4-5"
claude
set XX_API_KEY=sk-...
set ANTHROPIC_AUTH_TOKEN=%XX_API_KEY%
set ANTHROPIC_BASE_URL=https://api.xai-router.com
set ANTHROPIC_DEFAULT_OPUS_MODEL=claude-opus-4-6
set ANTHROPIC_DEFAULT_SONNET_MODEL=claude-sonnet-4-6
set ANTHROPIC_DEFAULT_HAIKU_MODEL=claude-haiku-4-5
claude
$env:XX_API_KEY="sk-..."
$env:ANTHROPIC_AUTH_TOKEN=$env:XX_API_KEY
$env:ANTHROPIC_BASE_URL="https://api.xai-router.com"
$env:ANTHROPIC_DEFAULT_OPUS_MODEL="claude-opus-4-6"
$env:ANTHROPIC_DEFAULT_SONNET_MODEL="claude-sonnet-4-6"
$env:ANTHROPIC_DEFAULT_HAIKU_MODEL="claude-haiku-4-5"
claude
OpenCode 更适合“我就想把工具跑起来”的用户,所以这里给你两套配置: Responses API 推荐配置 和 Chat API 兼容兜底配置。 OpenCode is often used by people who simply want the tool to run, so this page gives you both a recommended Responses API profile and a Chat API-compatible fallback profile.
全局配置文件路径:Linux / macOS 用 ~/.config/opencode/opencode.jsonc,Windows 用 %USERPROFILE%\.config\opencode\opencode.jsonc。 Global config file path: use ~/.config/opencode/opencode.jsonc on Linux / macOS and %USERPROFILE%\.config\opencode\opencode.jsonc on Windows.
{
"$schema": "https://opencode.ai/config.json",
"model": "openai/gpt-5.5",
"small_model": "openai/gpt-5.4-mini",
"provider": {
"openai": {
"options": {
"baseURL": "https://api.xai-router.com/v1",
"apiKey": "{env:XX_API_KEY}"
}
}
}
}
{
"$schema": "https://opencode.ai/config.json",
"model": "xai-router-chat/gpt-5.4-mini",
"small_model": "xai-router-chat/gpt-5.4-mini",
"provider": {
"xai-router-chat": {
"npm": "@ai-sdk/openai-compatible",
"options": {
"baseURL": "https://api.xai-router.com/v1",
"apiKey": "{env:XX_API_KEY}"
},
"models": {
"gpt-5.4-mini": {}
}
}
}
}
export XX_API_KEY="sk-..."
opencode debug config
opencode run "hello"
set XX_API_KEY=sk-...
opencode debug config
opencode run "hello"
$env:XX_API_KEY="sk-..."
opencode debug config
opencode run "hello"
OpenClaw 支持走 Responses API 或 Chat API。推荐优先用 api = "openai-responses",如果你需要兼容路径,再退到 Chat API。 OpenClaw can be configured against either the OpenAI Responses API or the OpenAI Chat API. Prefer api = "openai-responses" first, and only fall back to Chat compatibility when needed.
配置文件路径:Linux / macOS 用 ~/.openclaw/openclaw.json,Windows 用 %USERPROFILE%\.openclaw\openclaw.json。 Config file path: use ~/.openclaw/openclaw.json on Linux / macOS and %USERPROFILE%\.openclaw\openclaw.json on Windows.
{
"agents": {
"defaults": {
"model": { "primary": "xai-router/gpt-5.5" }
}
},
"models": {
"mode": "merge",
"providers": {
"xai-router": {
"baseUrl": "https://api.xai-router.com/v1",
"apiKey": "${XX_API_KEY}",
"api": "openai-responses",
"models": [{ "id": "gpt-5.5", "name": "gpt-5.5" }]
}
}
}
}
{
"agents": {
"defaults": {
"model": { "primary": "xai-router/gpt-5.4-mini" }
}
},
"models": {
"mode": "merge",
"providers": {
"xai-router": {
"baseUrl": "https://api.xai-router.com/v1",
"apiKey": "${XX_API_KEY}",
"api": "openai-completions",
"models": [{ "id": "gpt-5.4-mini", "name": "gpt-5.4-mini" }]
}
}
}
}
export XX_API_KEY="sk-..."
openclaw models status
set XX_API_KEY=sk-...
openclaw models status
$env:XX_API_KEY="sk-..."
openclaw models status
真正要改的是客户端配置里的 API 访问入口,也就是 base_url 或客户端对应的 provider/gateway 配置,不是浏览器里打开的官网地址。
What you need to change is the API access endpoint inside the client configuration, usually base_url or the client’s provider/gateway entry, not the website you open in a browser.
最常见原因是变量没有写进当前 shell 会读取的配置文件,或者你改完后没有重开终端。先确认 XX_API_KEY 在当前会话里确实存在。
The usual cause is that the variable was not written to the shell profile actually used by the current session, or the terminal was not reopened after editing it.
因为首页负责转化和建立信任,文档页负责承接搜索流量并快速回答配置问题。这两个页面解决的是不同阶段的问题。 Because the homepage is for conversion and trust building, while the docs page is for search capture and practical configuration answers. They solve different stages of the user journey.
直接回主站开户,或者走支持群。企业问题优先描述你的场景、用量、权限要求和付款方式,这样响应会更快。 Go back to the main site to open the account, or use the support channel. For enterprise requests, describe your use case, expected usage, permission needs, and payment flow to get a faster answer.
先去主站注册、充值并复制 key,再回来对照本页配置即可。 Create your account, recharge, and copy the key from the main site first, then come back to apply the configuration shown here.
把你使用的工具名、报错信息、当前配置片段和系统环境一起发到支持入口,定位会更快。 Send the tool name, error message, current config snippet, and system details to the support channel for faster diagnosis.