Skip to content

A scalable proxy for the Gemini API that handles API key rotation, polling, and load balancing. Perfect for Next.js, Cloudflare Workers, and serverless environments

License

Notifications You must be signed in to change notification settings

ccwav/gemini-proxy

Β 
Β 

Repository files navigation

Gemini Proxy

License: MIT PRs Welcome Issues

Gemini Proxy is a powerful, open-source toolkit for managing and proxying requests to Google's Gemini API. It provides a robust set of features for developers and organizations to monitor, control, and scale their usage of the Gemini API.

πŸ“‹ Table of Contents

πŸš€ Getting Started
βš™οΈ Configuration & Usage
🌐 Deployment
πŸ”§ Technical Details
πŸ“š Community & Links

πŸš€ Introduction

Gemini Proxy is a comprehensive solution that allows you to:

  • πŸ”‘ Manage API Keys: Securely store and rotate multiple Google Gemini API keys.
  • ⚑ Load Balance: Distribute requests across multiple API keys for optimal performance.
  • πŸ“Š Monitor Usage: Track API usage, costs, and performance metrics.
  • πŸ›‘οΈ Control Access: Manage proxy keys and access permissions.
  • πŸ“ Log Requests: Detailed logging of all API requests and responses.
  • 🌍 Deploy Anywhere: Support for multiple deployment platforms.

✨ Core Features

πŸ”‘ API Key Management

  • Secure storage of multiple Google Gemini API keys.
  • Intelligent key rotation and load balancing.
  • Usage tracking and analytics per key.
  • Enable/disable keys without downtime.

⚑ Performance & Scalability

  • Automatic request distribution across API keys.
  • Built-in retry mechanisms with exponential backoff.
  • Request caching and optimization.
  • Support for streaming responses.

πŸ“Š Monitoring & Analytics

  • Real-time request logging and analytics.
  • Performance metrics and cost tracking.
  • Error monitoring and alerting.
  • Usage dashboards and reports.

πŸ›‘οΈ Security & Access Control

  • Proxy API key management.
  • Request authentication and authorization.
  • Rate limiting and abuse prevention.
  • Secure environment variable handling.

🌍 Multi-Platform Support

  • Next.js Fullstack App (Recommended)
  • Standalone Node.js API Server
  • Vercel Edge Functions
  • Cloudflare Workers
  • Appwrite Functions

πŸ“‹ Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js (v18 or higher)
  • pnpm (v10 or higher)
  • Git
  • A Google AI Studio account for Gemini API keys.
  • A Supabase account for database and authentication.

πŸš€ Quick Start Guide

Recommended: Deploy the Web App (Fastest)

The fastest way to get started is by deploying the Next.js web application:

# 1. Clone the repository
git clone https://github.com/lehuygiang28/gemini-proxy.git
cd gemini-proxy

# 2. Install dependencies
pnpm install

# 3. Configure environment
cp apps/web/.env.example apps/web/.env.local
# Edit apps/web/.env.development with your values

# 4. Start development server
cd apps/web
pnpm dev

Alternative: Deploy to Vercel (Recommended for Production)

Deploy with Vercel

For manual setup, please refer to the apps/web/README.md.

βš™οΈ Environment Variables

Required Environment Variables

Variable Description
SUPABASE_URL Your Supabase project URL.
SUPABASE_SERVICE_ROLE_KEY Your Supabase service role key.

Optional Environment Variables

Variable Description Default
GOOGLE_GEMINI_API_BASE_URL Gemini API base URL. https://generativelanguage.googleapis.com/
GOOGLE_OPENAI_API_BASE_URL OpenAI-compatible API base URL. https://generativelanguage.googleapis.com/v1beta/openai/
PROXY_MAX_RETRIES Maximum retry attempts. -1
PROXY_LOGGING_ENABLED Enable request logging. true
PROXY_LOG_LEVEL Logging level. info
PROXY_LOADBALANCE_STRATEGY Proxy load balance strategy. (round_robin or sticky_until_error) round_robin

πŸ’» Usage Examples

Using with Google Generative AI SDK

import { GoogleGenAI } from '@google/genai';

const genAi = new GoogleGenAI({
    apiKey: 'your_proxy_api_key',
    httpOptions: {
        baseUrl: 'https://your-proxy-endpoint/api/gproxy/gemini',
    },
});

Using with OpenAI-Compatible Clients

import OpenAI from 'openai';

const openai = new OpenAI({
    apiKey: 'your_proxy_api_key',
    baseURL: 'https://your-proxy-endpoint/api/gproxy/openai',
});

Using with Vercel AI SDK

import { generateText } from 'ai';
import { createGoogleGenerativeAI } from '@ai-sdk/google';

const google = createGoogleGenerativeAI({
    apiKey: 'gproxy_test_12345',
    baseURL: 'https://your-proxy-endpoint/api/gproxy/gemini/v1beta',
});

🌐 Deployment Options

🎯 Recommended: Next.js Web App (Full-Stack with UI)

  • βœ… Complete Solution: Web interface + API proxy in one deployment.
  • βœ… User-Friendly Dashboard: Built-in UI for managing API keys, proxy keys, and monitoring.
  • βœ… Fastest Setup: Single deployment with all features.

⚑ Standalone API Server (API-Only)

  • βœ… Lightweight: Minimal resource usage, API-only service.
  • βœ… Customizable: Full control over deployment.
  • βœ… Scalable: Can be deployed anywhere.

πŸš€ Edge Functions (API-Only)

  • βœ… Global CDN: Deploy to edge locations worldwide.
  • βœ… Low Latency: Fastest response times.
  • βœ… Auto-scaling: Handles traffic spikes automatically.

πŸ› οΈ Platform-Specific Guides

πŸ—οΈ Architecture

Your App --> Gemini Proxy --> Google Gemini API
                 |
                 v
              Supabase (Database)

πŸ“ Project Structure

gemini-proxy/
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ web/      # Next.js web application (Recommended)
β”‚   └── api/      # Standalone Node.js API server
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ core/     # Core business logic
β”‚   β”œβ”€β”€ cli/      # Command-line tools
β”‚   └── ...       # Other packages
└── README.md

πŸ› οΈ Development

Local Development

# 1. Clone the repository
git clone https://github.com/lehuygiang28/gemini-proxy.git
cd gemini-proxy

# 2. Install dependencies
pnpm install

# 3. Start development servers
pnpm dev

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for more details.

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

πŸ”— Links


Made with ❀️ by lehuygiang28

About

A scalable proxy for the Gemini API that handles API key rotation, polling, and load balancing. Perfect for Next.js, Cloudflare Workers, and serverless environments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 95.1%
  • PLpgSQL 3.5%
  • JavaScript 1.1%
  • Other 0.3%