Skip to content

aurora-develop/Claude2api

Repository files navigation

Claude2api

中文文档 | English README | API 文档 | English API

Claude2api 是一个基于 Go + Gin 的 claude.ai 网页 API 代理服务,可以把 claude.ai 网页会话包装成常见的 OpenAI / Anthropic 兼容接口。

项目会使用接近真实浏览器的 TLS / Header / Cookie 环境访问 https://claude.ai,并对外提供 JSON 与 SSE 流式响应。

API 文档

详细接口说明见:API.md

功能特性

  • OpenAI Chat Completions 兼容接口:POST /v1/chat/completions
  • Anthropic Messages 兼容接口:POST /v1/messages
  • OpenAI Responses 兼容接口:POST /v1/responses
  • 模型列表接口:GET /v1/models
  • 支持非流式与 SSE 流式返回
  • 支持 conversation_id 持久会话模式,保持多轮对话连续性
  • 支持完整浏览器 Cookie 模式,更接近 claude.ai 浏览器请求环境
  • 支持 Bearer sessionKey 模式,便于本地简单调用
  • Bearer 模式下会自动生成可由前端生成的浏览器环境 Cookie/Header;签名或 Cloudflare 类 Cookie 不伪造、不传递
  • completion 请求会携带从真实浏览器请求逆向得到的 claude.ai web tools 字段
  • Referer 会按请求阶段动态设置为 /new/chat/<conversation_id>
  • Datadog/RUM Cookie 与 trace headers 会按浏览器 SDK 的字段结构生成
  • 使用独立 tlsclient 模块封装 github.com/bogdanfinn/tls-client 的 Chrome 指纹、CookieJar 和浏览器基础 Header
  • 支持 Docker / Docker Compose 部署

支持的模型

/v1/models 只会返回以下模型,请求时也只允许使用这些模型:

  • claude-fable-5
  • claude-opus-4-8
  • claude-haiku-4-5
  • claude-opus-4-7
  • claude-opus-4-6
  • claude-opus-3
  • claude-sonnet-4-6
  • claude-sonnet-5

使用其他模型会返回 invalid_request_error

环境要求

  • Go 1.26.4 或更高版本
  • 一个可用的 claude.ai 浏览器会话
  • 可选:Docker / Docker Compose

快速开始

1. 构建

Windows:

go build -o claude2api.exe .

Linux / macOS:

go build -o claude2api .

2. 运行

使用 sessionKey:

CLAUDE_SESSION_KEY='你的-sessionKey' PORT=8080 ./claude2api.exe

使用完整浏览器 Cookie:

CLAUDE_COOKIE='sessionKey=...; sessionKeyLC=...; anthropic-device-id=...; ...' PORT=8080 ./claude2api.exe

服务地址:

http://127.0.0.1:8080/v1

3. 测试

curl http://127.0.0.1:8080/v1/chat/completions \
  -H 'Authorization: Bearer 你的-sessionKey' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "claude-sonnet-5",
    "messages": [{"role": "user", "content": "Reply with exactly: pong"}],
    "stream": false
  }'

预期返回格式:

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "model": "claude-sonnet-5",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "pong"},
      "finish_reason": "stop"
    }
  ]
}

Docker 部署

GitHub Actions 会自动构建 Docker 镜像并推送到 GitHub Container Registry:

ghcr.io/aurora-develop/claude2api

拉取镜像:

docker pull ghcr.io/aurora-develop/claude2api:latest

运行远程镜像:

docker run --rm -p 8080:8080 \
  -e CLAUDE_SESSION_KEY='你的-sessionKey' \
  ghcr.io/aurora-develop/claude2api:latest

Docker build

docker build -t claude2api .

Docker run

使用 sessionKey:

docker run --rm -p 8080:8080 \
  -e CLAUDE_SESSION_KEY='你的-sessionKey' \
  claude2api

使用完整 Cookie:

docker run --rm -p 8080:8080 \
  -e CLAUDE_COOKIE='sessionKey=...; sessionKeyLC=...; anthropic-device-id=...; ...' \
  claude2api

Docker Compose

CLAUDE_SESSION_KEY='你的-sessionKey' docker compose up --build

或者:

CLAUDE_COOKIE='sessionKey=...; sessionKeyLC=...; anthropic-device-id=...; ...' docker compose up --build

配置项

环境变量 默认值 说明
PORT 8080 本地 HTTP 服务端口。
CLAUDE_BASE_URL https://claude.ai claude.ai 上游地址。
CLAUDE_SESSION_KEY claude.ai 的 sessionKey。配置后请求端可以不传 Bearer。
CLAUDE_COOKIE 从浏览器复制的完整 claude.ai Cookie。推荐用于更接近浏览器环境。
CLAUDE_TIMEZONE Asia/Singapore 发送给 claude.ai 的时区。
CLAUDE_LOCALE en-US 发送给 claude.ai 的语言区域。
DEFAULT_MODEL claude-sonnet-5 请求未指定模型时使用的默认模型。必须在支持模型列表内。

认证方式

所有 /v1/* 接口都需要认证。

方式一:Bearer sessionKey

Authorization: Bearer <claude.ai sessionKey>

如果服务端已经配置 CLAUDE_SESSION_KEY,请求端可以不传这个 Header。

Bearer 模式下,用户只需要提供 sessionKey。服务端会自动生成这些前端可生成的环境值:

  • sessionKeyLC
  • anthropic-device-id
  • activitySessionId
  • ajs_anonymous_id
  • __ssid
  • _dd_s
  • traceparent / Datadog RUM 相关 Header
  • 部分 UI / analytics Cookie

以下不能伪造的服务端签名或 Cloudflare Cookie 不会自动生成,也不会在 Bearer 模式下传递:

  • routingHint
  • cf_clearance
  • __cf_bm
  • _cfuvid

方式二:完整浏览器 Cookie

X-Claude-Cookie: <从 claude.ai 浏览器请求中复制的完整 Cookie>

这种方式最接近浏览器行为。代理会复用 Cookie 中的:

  • sessionKey
  • sessionKeyLC
  • anthropic-device-id
  • lastActiveOrg
  • routingHint
  • Cloudflare 相关 Cookie

如果服务端已经配置 CLAUDE_COOKIE,请求端可以不传 X-Claude-Cookie

致谢

感谢 LINUX DO 社区 —— 本项目在此发布,感谢社区用户的反馈与帮助。

英文版:

注意事项

  • 每次 completion 请求都会创建一个临时 claude.ai 会话,请求结束后会尝试删除。
  • usage 中的 token 数是近似值,目前主要根据输出文本长度估算。
  • 如果 Bearer sessionKey 模式和浏览器行为不一致,建议使用完整 Cookie 模式。
  • 如果上游返回 429,说明 claude.ai 当前账号或会话触发了速率限制。
  • 请不要把自己的 sessionKey、完整 Cookie、抓包文件提交到公开仓库。

About

No description, website, or topics provided.

Resources

Stars

21 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors