zhiyu1998/Gemi2Api-Server

HanaokaYuzu / Gemini-API 的服务端简单实现,仓库自动更新上游,支持gemini-3.6-flash、gemini-3.5-flash-lite 等

Gemi2Api‑Server – a lightweight self‑hosted proxy for Google Gemini

What it is

  • A small FastAPI service that forwards requests to the unofficial Google Gemini web API (the same API used by the Gemini‑API project). It mimics the OpenAI /v1/chat/completions endpoint so existing clients can talk to Gemini without using Google’s official SDK.
  • Includes a built‑in admin UI where you can view the service status, edit the required Gemini cookies, change the API key, and test chats.

Why it exists

  • Google Gemini does not provide a public, paid API (as of the time of writing). The only way to call it programmatically is to reverse‑engineer the web UI and send the same HTTP requests a browser would send. This repo packages that reverse‑engineered client (gemini‑webapi) into a reusable server.
  • By exposing a standard OpenAI‑compatible endpoint, developers can drop‑in Gemini as a drop‑in replacement for ChatGPT‑style services.

How it works

  1. Authentication – You must supply the two cookies that a logged‑in Gemini session creates (__Secure-1PSID and __Secure-1PSIDTS). The README explains how to extract them from the browser’s dev tools.
  2. FastAPI + Uvicorn – The server runs a FastAPI app (main.py). Requests to /v1/models return a list of available Gemini models; POST to /v1/chat/completions forwards the payload to the Gemini web endpoint and returns the response in OpenAI format.
  3. Custom model definitions – An optional custom_models.yaml can override or add model entries, letting you change the request headers (x-goog-ext‑…‑jspb) that Gemini expects.
  4. Image proxy – When Gemini returns a generated image URL, the server can proxy the image through /gemini‑proxy/image so the image is accessible behind firewalls or reverse proxies.
  5. Admin panel – Accessible at /admin. The panel is protected by an API_KEY you set in .env. It lets you:
    • See the health of the Gemini connection.
    • Update the cookie values without restarting.
    • Adjust rate‑limit settings.
    • View live logs and run quick chat tests.

Running it

  • Docker (recommended)docker-compose up -d after copying .env.example to .env and filling in the cookie values.
  • Directly – Install dependencies (uv or pip), activate a virtualenv, then uvicorn main:app --reload.
  • Environment variables control optional behaviours:
    • TEMPORARY_CHAT disables features like “thinking” or image generation.
    • AUTO_DELETE_CHAT removes the conversation from Gemini after the response.
    • PUBLIC_BASE_URL is needed for correct image proxy URLs when behind a reverse proxy.

Use cases

  • Developers who want to experiment with Gemini in their own applications without relying on a third‑party hosted service.
  • Teams building internal tools that already speak the OpenAI API and want to switch to Gemini for cost or capability reasons.
  • Anyone needing a quick demo of Gemini’s chat and image generation capabilities on Render, HuggingFace Spaces, or a self‑hosted server.

Limitations

  • It relies on user‑provided cookies; if Google rotates or invalidates them, the service stops until you refresh the cookies.
  • Because it uses the undocumented web API, it may break without notice.
  • The demo deployments (Render, HuggingFace) ship with a placeholder password and no Gemini cookies, so they only show the admin UI.

Acknowledgements

  • Uses the gemini‑webapi library for the low‑level request handling.
  • Image‑watermark removal code is borrowed from the journey‑ad/gemini-watermark-remover and allenk/GeminiWatermarkTool projects.

TL;DR: Gemi2Api‑Server is a FastAPI‑based proxy that lets you call Google Gemini as if it were an OpenAI‑compatible API. You provide your own Gemini login cookies, optionally customize model headers, and run it locally or in a container. It’s a practical bridge for developers who want to use Gemini’s chat and image generation without an official API.

Related

  • Project
  • Project
  • Project
  • Project
  • Project