OpenSource-Hub

grok2api

Application

chenyme/grok2api

Multi-account API gateway for Grok with OpenAI/Anthropic interfaces.

Overview

A pure Go implementation of a Grok API gateway that manages multi-account pools for Grok Build, Web, and Console. Exposes OpenAI and Anthropic-compatible endpoints with robust routing, rate limiting, and credential encryption.

README Preview

\n  \n\n\n\n  面向 Grok Build、Grok Web 与 Grok Console 的多账号 API 网关\n\n\n\n  \n  \n  \n\n\n> [!TIP]\n> **个人新项目**\n> 推荐个人新项目 [DEEIX-AI:DEEIX-Chat 轻量化 AI 平台](https://github.com/DEEIX-AI/DEEIX-Chat):企业级模型路由、对话、文件、工具、计费、身份和运维的一体化 AI 平台,全面且极致的低占用,空载运行时仅占用 34 MB。\n\n> [!NOTE]\n> 本项目仅供学习与研究交流。请务必遵循 Grok 的使用条款及当地法律法规,不得用于非法用途!\n\nGrok2API 是一个纯 Go 实现的 Grok API 网关。项目将 Grok Build OAuth、Grok Web SSO 与 Grok Console SSO 组织为独立账号池,对外提供 OpenAI 风格接口、Anthropic Messages 兼容接口,以及账号、模型、密钥、用量和代理管理后台。\n\n## 功能概览\n\n- **三 Provider**:`grok_build`、`grok_web` 与 `grok_console` 独立路由、额度和故障状态\n- **标准接口**:Responses、Chat Completions、Images、异步 Videos、Anthropic Messages\n- **多账号调度**:优先级、并发限制、额度门控、会话粘滞、冷却与故障切换\n- **账号接入**:Device OAuth、OAuth JSON、SSO JSON、逐行 SSO Token\n- **媒体能力**:图片生成、图片编辑、视频生成、图片本地归档与 URL/Base64 返回\n- **基础设施**:SQLite/PostgreSQL、Memory/Redis、HTTP 与 SOCKS 代理池\n- **安全边界**:AES-256-GCM 凭据加密、客户端密钥哈希、日志脱敏、SSRF 与传输上限\n- **管理后台**:Dashboard、账号、模型、客户端密钥、请求审计、接口文档与热加载设置\n\n## 架构\n\n```mermaid\nflowchart LR\n    Client["API Client"] --> Gateway["Go Gateway"]\n    Admin["React Admin"] --> Gateway\n\n    Gateway --> Router["Model Router"]\n    Router --> Build["Grok Build"]\n    Router --> Web["Grok Web"]\n    Router --> Console["Grok Console"]\n\n    Build --> BuildPool["OAuth Account Pool"]\n    Web --> WebPool["SSO Account Pool"]\n    Console --> ConsolePool["SSO Account Pool"]\n    Build --> Egress["Egress Pool"]\n    Web --> Egress\n    Console --> Egress\n\n    Gateway --> Database["SQLite / PostgreSQL"]\n    Gateway --> Runtime["Memory / Redis"]\n    Gateway --> Media["Media Storage"]\n```\n\n## 快速部署\n\n### Docker Compose\n\n1. 准备配置:\n\n```bash\ngit clone https://github.com/chenyme/grok2api.git\ncd grok2api\ncp config.example.yaml config.yaml\n```\n\n2. 生成并填写安全密钥:\n\n```bash\nopenssl rand -hex 32\nopenssl rand -base64 32\n```\n\n```yaml\nsecrets:\n  jwtSecret: "替换为 hex 随机值"\n  credentialEncryptionKey: "替换为 Base64 随机密钥"\n\nbootstrapAdmin:\n  username: "admin"\n  password: "替换为强密码"\n```\n\n3. 启动:\n\n