chenyme / grok2api

chenyme / grok2api

A multi-account API gateway for Grok Build, Grok Web, and Grok Console. 这是一个用于 Grok Build、Grok Web 和 Grok Console 的多账号 API 网关。

Tip

Check out DEEIX-AI / DEEIX-Chat, a lightweight, integrated AI platform for model routing, chat, files, tools, billing, identity, and operations. 查看 DEEIX-AI / DEEIX-Chat,这是一个轻量级、集成的 AI 平台,支持模型路由、聊天、文件、工具、计费、身份验证和运维管理。

Note

This project is for technical research and learning purposes only. Please comply with Grok’s official terms of use and local laws when using it; otherwise, you will be solely responsible for all consequences! 本项目仅供技术研究和学习使用。使用时请遵守 Grok 的官方使用条款及当地法律法规;否则,由此产生的一切后果由您自行承担!

Sponsors

Want to sponsor this project? 想要赞助本项目吗?

  • Krill AI provides fast, stable API access to GPT, Claude, Gemini, and leading Chinese models, with enterprise customization, invoicing, 7×16 support, and optimized WebSocket connections for faster first-token latency. Register through the exclusive link and use code “grok2api” for 23% off your first Codex package. Krill AI 提供快速、稳定的 GPT、Claude、Gemini 及主流国产模型 API 访问,支持企业定制、开票、7×16 小时支持,并优化了 WebSocket 连接以实现更快的首字延迟。通过专属链接注册并使用优惠码“grok2api”,首次购买 Codex 套餐可享 77 折优惠。

  • DEEIX-Chat is an open-source, self-hostable AI Chat platform for individuals, teams, and enterprises that need stable, long-term, unified access to multiple models. It brings models, conversations, files, tool calling, and administration together in one deployable and extensible system. Click here to start deploying. DEEIX-Chat 是一个开源、可自托管的 AI 聊天平台,适用于需要稳定、长期、统一访问多种模型的个人、团队和企业。它将模型、对话、文件、工具调用和管理功能集成在一个可部署、可扩展的系统中。点击此处开始部署。

  • Right Code is an enterprise-grade AI Agent distribution platform that primarily provides stable access services for Claude Code, Codex, Gemini, and other models. It supports invoicing and dedicated one-to-one assistance for enterprises and teams. Thanks to Right Code for providing token support. Click here to register and get started. Right Code 是一个企业级 AI Agent 分发平台,主要为 Claude Code、Codex、Gemini 等模型提供稳定的访问服务。它支持企业和团队开票及一对一专属协助。感谢 Right Code 提供 Token 支持。点击此处注册并开始使用。

  • FennoAI provides enterprise-grade OpenAI/Anthropic-compatible APIs for Codex, Claude Code, and OpenCode, processing hundreds of billions of tokens daily with global business settlement and invoicing. Through the Grok2API exclusive offer, USD 1.99 unlocks USD 50 in Coding Plan credits, plus referral commissions up to 20%. FennoAI 为 Codex、Claude Code 和 OpenCode 提供企业级 OpenAI/Anthropic 兼容 API,日处理 Token 量达数千亿,支持全球业务结算和开票。通过 Grok2API 专属优惠,支付 1.99 美元即可解锁 50 美元的 Coding Plan 额度,并可获得最高 20% 的推荐佣金。

  • Qiniu Cloud AI, Qiniu Cloud’s (02567.HK) enterprise MaaS platform, offers protocol-compatible access to 150+ global models for text, image, audio, video, and files, serving 1.69+ million users. Grok2API registrations through the exclusive link receive 12 million free enterprise tokens or 3 million developer tokens. 七牛云 AI 是七牛云 (02567.HK) 的企业级 MaaS 平台,提供 150 多种全球模型的协议兼容访问,涵盖文本、图像、音频、视频和文件,服务超过 169 万用户。通过专属链接注册 Grok2API 可获得 1200 万免费企业 Token 或 300 万开发者 Token。

Overview

Grok2API is a Go gateway with a built-in React admin console. It manages independent Grok Build, Grok Web, and Grok Console account pools and exposes unified OpenAI- and Anthropic-compatible APIs. Grok2API 是一个使用 Go 语言编写的网关,内置 React 管理控制台。它管理独立的 Grok Build、Grok Web 和 Grok Console 账号池,并对外提供统一的 OpenAI 和 Anthropic 兼容 API。

Loading

The Gateway routes requests through the Provider Registry. Account Sync refreshes credentials, quota, and models. Each Provider keeps independent account state and uses an isolated egress scope; usage, audits, and client billing are finalized after the request. 网关通过提供商注册中心(Provider Registry)路由请求。账号同步(Account Sync)负责刷新凭据、配额和模型。每个提供商保持独立的账号状态并使用隔离的出站范围;使用情况、审计和客户计费在请求完成后结算。

Core capabilities

AreaCapabilities
APIsResponses, Chat Completions, Anthropic Messages, Images, and asynchronous Videos
ClientsCodex, Claude Code, OpenAI-compatible SDKs, and Anthropic-compatible SDKs
AccountsBulk import/export, quota sync, credential renewal, conversion, tools, and cleanup
RoutingModel discovery, Provider pinning, sticky sessions, quota/concurrency guards, and bounded failover
SessionsStored responses, compact, prompt-cache affinity, and optional reasoning replay
MediaImage generation/editing, video jobs, local archiving, and URL/Base64/SSE output
EgressHTTP/SOCKS/Resin, subscriptions, probes, proxy pools, allocation, fallback, and FlareSolverr
OperationsDashboard, model routes, client keys, audits, runtime settings, and media libraries

核心功能

领域功能
API响应、聊天补全、Anthropic 消息、图像及异步视频
客户端Codex、Claude Code、OpenAI 兼容 SDK 及 Anthropic 兼容 SDK
账号批量导入/导出、配额同步、凭据更新、转换、工具及清理
路由模型发现、提供商绑定、会话保持、配额/并发保护及有限故障转移
会话存储响应、压缩、提示词缓存亲和性及可选的推理重放
媒体图像生成/编辑、视频任务、本地归档及 URL/Base64/SSE 输出
出站HTTP/SOCKS/Resin、订阅、探测、代理池、分配、故障转移及 FlareSolverr
运维仪表盘、模型路由、客户端密钥、审计、运行时设置及媒体库

Provider boundaries

ProviderAuthenticationModelsMain capabilities
Grok BuildOAuth / Device OAuthDiscovered per accountResponses, Chat, Messages, compact, stored responses, paid-account video
Grok WebSSOBuilt-in, filtered by tierResponses, Chat, Messages, stored responses, images, image editing, video
Grok ConsoleSSOBuilt-in StatelessResponses, Chat, Messages, images, image editing, video

提供商边界

提供商认证方式模型主要功能
Grok BuildOAuth / 设备 OAuth按账号发现响应、聊天、消息、压缩、存储响应、付费账号视频
Grok WebSSO内置,按等级过滤响应、聊天、消息、存储响应、图像、图像编辑、视频
Grok ConsoleSSO内置无状态响应、聊天、消息、图像、图像编辑、视频

Each Provider keeps its own credentials, quota, health, cooldown, concurrency, and model capabilities. Failover stays within the selected Provider. 每个提供商维护各自的凭据、配额、健康状态、冷却时间、并发限制和模型能力。故障转移仅在所选提供商内部进行。

Quick start

Official images support linux/amd64 and linux/arm64. 官方镜像支持 linux/amd64 和 linux/arm64。

git clone https://github.com/chenyme/grok2api.git
cd grok2api
cp config.example.yaml config.yaml

Generate secrets and place them in config.yaml: 生成密钥并将其放入 config.yaml:

openssl rand -hex 32
openssl rand -base64 32

secrets:

jwtSecret: "replace-with-the-generated-hex-value"
credentialEncryptionKey: "replace-with-the-generated-base64-key"