A high-performance, TypeScript-based reverse proxy for the Google Gemini API, designed to bypass regional network restrictions and provide stable access for LLM applications.
- TypeScript Support: Full type safety and modern developer experience.
- SSE Support: Handles Server-Sent Events (streaming) for real-time AI responses.
- CORS Enabled: Configured to allow cross-origin requests.
- Header Sanitization: Properly re-writes
Hostand removes Cloudflare-specific headers to ensure compatibility with Google's API servers.
- Node.js installed.
- Cloudflare Account and a registered domain (optional but recommended).
Install the Wrangler CLI and deploy the project:
# Install Wrangler globally
npm install -g wrangler
# Login to your Cloudflare account
wrangler login
# Deploy the project
wrangler deploy
The core logic maps incoming requests to generativelanguage.googleapis.com while maintaining the original path and query parameters.
To use this proxy in your Python project with langchain-google-genai, update your LLM initialization. Replace YOUR_PROXY_DOMAIN with your actual Worker URL.
from langchain_google_genai import ChatGoogleGenerativeAI
from pydantic.types import SecretStr
def get_llm():
# copy https://github.com/langchain-ai/langchain-google/issues/783
proxy_url = "https://gemini-proxy.yourdomain.com/v1beta"
api_key = "YOUR_GEMINI_API_KEY"
llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash",
api_key=SecretStr(apiKey),
client_options={
"api_endpoint": proxy_url
},
transport="rest",
)
return llmIf you encounter a 403 Forbidden error:
- Disable Bot Fight Mode: In the Cloudflare Dashboard, go to
Security->Botsand turn off Bot Fight Mode. - WAF Rules: Ensure your IP or the request pattern isn't being blocked by Cloudflare Managed Rules.
- SSL Settings: Set your Cloudflare SSL/TLS encryption mode to Full or Full (strict).
- Endpoint Path: Ensure you include the version prefix (e.g.,
/v1beta) in yourbase_urlif the SDK doesn't append it automatically.
MIT License. Feel free to use and modify for your own projects.