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/completionsendpoint 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
- Authentication – You must supply the two cookies that a logged‑in Gemini session creates (
__Secure-1PSIDand__Secure-1PSIDTS). The README explains how to extract them from the browser’s dev tools. - FastAPI + Uvicorn – The server runs a FastAPI app (
main.py). Requests to/v1/modelsreturn a list of available Gemini models; POST to/v1/chat/completionsforwards the payload to the Gemini web endpoint and returns the response in OpenAI format. - Custom model definitions – An optional
custom_models.yamlcan override or add model entries, letting you change the request headers (x-goog-ext‑…‑jspb) that Gemini expects. - Image proxy – When Gemini returns a generated image URL, the server can proxy the image through
/gemini‑proxy/imageso the image is accessible behind firewalls or reverse proxies. - Admin panel – Accessible at
/admin. The panel is protected by anAPI_KEYyou 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 -dafter copying.env.exampleto.envand filling in the cookie values. - Directly – Install dependencies (
uvorpip), activate a virtualenv, thenuvicorn main:app --reload. - Environment variables control optional behaviours:
TEMPORARY_CHATdisables features like “thinking” or image generation.AUTO_DELETE_CHATremoves the conversation from Gemini after the response.PUBLIC_BASE_URLis 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‑webapilibrary for the low‑level request handling. - Image‑watermark removal code is borrowed from the
journey‑ad/gemini-watermark-removerandallenk/GeminiWatermarkToolprojects.
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