Gemini Web 2 API is a lightweight Go proxy that exposes OpenAI-compatible /v1/chat/completions and /v1/models endpoints while forwarding requests to the Gemini web experience. It supports streaming responses, session continuity, token refresh, and a small telemetry endpoint for the built-in WebUI.
- OpenAI-compatible chat and model endpoints
- Streaming SSE responses
- Session reuse with
X-Session-IDorconversation_id - Optional proxy and cookie-based token refresh
- Telemetry endpoint at
/api/telemetry - Embedded dashboard at
/and manual page at/help - WebUI defaults to English and can switch to Chinese
- Enter the project directory:
cd e:/Project/AI/All2API/geminiweb2api- Install dependencies and run:
go mod tidy
go run ./cmd/geminiweb2apiProject layout now uses folders by responsibility:
cmd/geminiweb2api: application entrypointinternal/server: HTTP server assembly and routinginternal/gemini: Gemini request/response logicinternal/config,internal/logging,internal/httpclient,internal/token,internal/metrics: infrastructure modulesinternal/web: embedded dashboard and help pages
- Default listen address:
http://127.0.0.1:8080
- Verify the service:
curl -s "http://127.0.0.1:8080/api/telemetry"
curl -s "http://127.0.0.1:8080/v1/models" \
-H "Authorization: Bearer your-api-key-here"Use config.json in the project root:
{
"api_key": "your-api-key-here",
"token": "",
"cookies": "",
"tokens": null,
"proxy": "",
"gemini_url": "",
"gemini_home_url": "",
"port": 8080,
"log_file": "",
"log_level": "info",
"note": [
"Auto-generated config"
]
}api_keyProtects this local service. Clients must sendAuthorization: Bearer <api_key>.tokenManualSNlM0efallback token for Gemini web when automatic fetch fails.cookiesGemini web cookie string. Recommended when anonymous access is unstable or the environment requires sign-in state.tokensReserved field. Currently unused.proxyExplicit proxy such ashttp://127.0.0.1:7890. The app also respectsHTTP_PROXY,HTTPS_PROXY, andALL_PROXY.gemini_urlOverride for the Gemini generation endpoint in reverse-proxy setups.gemini_home_urlOverride for the Gemini homepage used for token discovery.portLocal listen port, default8080.log_fileLog output path. Empty means stdout.log_leveldebug,info,warn, orerror.noteFree-form note strings surfaced by/api/telemetryand the WebUI.
The process checks config.json every 5 seconds and reloads it automatically when the file changes. You do not need to restart the service after editing the config.
- The service tries to fetch the anonymous Gemini token (
SNlM0e) from the Gemini homepage by default. - You can often run without
cookies, but adding them improves stability.
When automatic fetch fails:
- Open
https://gemini.google.com/or your configuredgemini_home_url. - Open browser devtools.
- Search for
SNlM0einElements,Sources, orNetwork. - Copy the value after
"SNlM0e":"...". - Put it into
config.json:
{
"token": "SNlM0e-example-value"
}If your environment needs a logged-in browser session or you see protection pages:
- Open Gemini in a working browser session.
- Open devtools and find the cookie store.
- Copy the full cookie string:
SID=...; APISID=...; SAPISID=...; ...
- Put it into
config.json:
{
"cookies": "SID=...; APISID=...; SAPISID=...; ..."
}Do not commit real cookies to a public repository.
curl -s "http://127.0.0.1:8080/api/telemetry"curl -s "http://127.0.0.1:8080/v1/models" \
-H "Authorization: Bearer your-api-key-here"curl -s "http://127.0.0.1:8080/v1/chat/completions" \
-H "Authorization: Bearer your-api-key-here" \
-H "Content-Type: application/json" \
-H "X-Session-ID: demo-session" \
-d '{
"model": "gemini-3-flash",
"stream": false,
"messages": [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Please introduce yourself."}
]
}'curl -N "http://127.0.0.1:8080/v1/chat/completions" \
-H "Authorization: Bearer your-api-key-here" \
-H "Content-Type: application/json" \
-H "X-Session-ID: demo-session" \
-d '{
"model": "gemini-3-flash",
"stream": true,
"messages": [
{"role": "user", "content": "Introduce yourself in three sentences."}
]
}'- Keep
X-Session-IDstable for the same user or conversation. - The service will reuse Gemini-side session context for better multi-turn continuity.
- Gemini access fails or times out:
Check
proxyfirst, then system proxy environment variables. - HTML, captcha, or protection pages appear:
Try cookies, change IP/proxy, and enable
debuglogging. - Conversation continuity is broken:
Reuse the same
X-Session-ID. - Automatic token fetch fails:
Manually set
token, or verify thatcookiesare valid and not expired.
GET /renders the dashboardGET /helprenders the manual page- Theme and language preferences are stored in local storage
- Default language is English, with a Chinese switch available in the UI