Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ open-gateway Public template

Open Gateway is an API gateway that lets you integrate multiple AI providers through a single, consistent endpoint. Switch between OpenAI, Anthropic, Google, Groq, and more without changing your application code.

Notifications You must be signed in to change notification settings

zakirkun/open-gateway

Repository files navigation

OpenGateway-Ai β€” Unified AI API Gateway

OpenGateway AI is an API gateway that lets you integrate multiple AI providers through a single, consistent endpoint. Switch between OpenAI, Anthropic, Google, Groq, and more without changing your application code.

πŸš€ Key Features

  • Multi-Provider Support: OpenAI, Anthropic, Google, Groq, and more
  • Unified Interface: One API shape for all providers (OpenAI-compatible)
  • Streaming Support: Real‑time streaming for chat completions
  • Smart Routing: Load balancing and failover (provider‑aware)
  • Cost Optimization: Choose models by cost and performance
  • Enhanced Authentication:
    • JWT with refresh tokens (httpOnly cookie or token in body)
    • Email verification
    • Password reset
    • Secure API key generation and management
  • Rate Limiting: Tier‑based usage protection
  • Analytics & Monitoring: Comprehensive usage and cost analytics
  • Modern UI/UX:
    • Refined landing page and dashboard
    • Auth pages (login/register/forgot/reset) redesigned
    • Authenticated Playground page (moved out from landing)
  • Usage Tracking: Monitor tokens, latency, and costs

✨ What's New (Chat Experience)

  • Modern Chat UI (Next.js 14 + Tailwind CSS): ChatGPT-like layout with polished navbar, sidebar rooms, and message area.
  • Database-backed Chat Rooms & Messages: Persist rooms and messages in PostgreSQL via Prisma.
  • Dynamic Model Selection per Room: No fixed modelId at room creation; user picks model per room (stored via localStorage).
  • State Management with TanStack Query: Hooks for models, rooms, and messages with proper caching and loading states.
  • Real-time Messaging (WebSockets with Hono/Bun): Join/leave rooms, typing indicators, and broadcast new messages.
  • Message Search: Local and server-side search with highlighted results.
  • File Attachments: Upload and send multiple attachments with messages; rich preview in UI.
  • CORS & Auth Improvements: Correct preflight handling, credentialed requests, and wider allowed headers/methods.

πŸ—οΈ Architecture

Backend

  • Framework: Hono + Bun (Node-compatible)
  • Database: PostgreSQL via Prisma ORM
  • Cache: Redis
  • Auth: JWT + Refresh tokens + API Keys + Optional 2FA
  • Rate Limiting: Redis-based
  • Realtime: WebSockets via hono/bun upgrade helper

Frontend

  • Framework: Next.js 14
  • Styling: TailwindCSS
  • State: TanStack Query
  • UI: Custom components + Heroicons
  • Hooks: useModels, useChatRooms, useRoomModelSelection, useWebSocket, useDebounce

πŸ“¦ Installation

Prerequisites

  • Bun runtime
  • PostgreSQL
  • Redis
  • Node.js 18+ (for frontend)

Setup Development Environment

  1. Clone repository
git clone https://github.com/zakirkun/OpenGateway-ai.git
cd OpenGateway-ai
  1. Install dependencies
bun install
  1. Setup environment variables
# Backend
cp packages/backend/env.example packages/backend/.env
# Edit packages/backend/.env with your settings

# Frontend
cp packages/frontend/.env.example packages/frontend/.env.local
# Edit packages/frontend/.env.local with your settings
  1. Setup database
cd packages/backend
bunx prisma db push
bunx prisma generate
  1. Start development servers
# Terminal 1 - Backend
cd packages/backend
bun run dev

# Terminal 2 - Frontend
cd packages/frontend
bun run dev

Docker Setup

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

πŸ”§ Configuration

Environment Variables

Backend (.env)

# Database
DATABASE_URL="postgresql://username:password@localhost:5432/OpenGateway_ai"

# Redis
REDIS_URL="redis://localhost:6379"

# JWT
JWT_SECRET="your-super-secret-jwt-key"

# AI Provider API Keys
OPENAI_API_KEY="sk-your-openai-key"
ANTHROPIC_API_KEY="sk-ant-your-anthropic-key"
GOOGLE_API_KEY="your-google-api-key"
GROQ_API_KEY="gsk_your-groq-key"

# Server
PORT=3001
NODE_ENV="development"
# CORS
# Comma-separated list of allowed origins in dev
CORS_ORIGIN="http://localhost:3000,http://127.0.0.1:3000"

Frontend (.env.local)

NEXT_PUBLIC_API_URL="http://localhost:3001"

πŸ“š API Documentation (summary)

Authentication

Requests to protected endpoints require an Authorization header:

Authorization: Bearer YOUR_API_KEY

Chat Completions

Non-streaming

curl -X POST http://localhost:3001/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [
      {"role": "user", "content": "Hello, world!"}
    ],
    "max_tokens": 1000,
    "temperature": 0.7,
    "stream": false
  }'

Streaming

curl -X POST http://localhost:3001/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-3-5-sonnet-20241022",
    "messages": [
      {"role": "user", "content": "Tell me a story"}
    ],
    "stream": true
  }'

Image Generation

curl -X POST http://localhost:3001/v1/images/generations \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/dall-e-3",
    "prompt": "A beautiful sunset over mountains",
    "n": 1,
    "size": "1024x1024"
  }'

Audio Transcription

curl -X POST http://localhost:3001/v1/audio/transcriptions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "[email protected]" \
  -F "model=openai/whisper-1"

List Models

curl -X GET http://localhost:3001/v1/models \
  -H "Authorization: Bearer YOUR_API_KEY"

Notes on model objects:

  • Response uses owned_by and display_name at the top level, e.g.
    {
      "id": "openai/gpt-4o",
      "owned_by": "OpenAI",
      "display_name": "GPT-4o"
    }

Chat Rooms & Messages (App features)

  • Server exposes REST endpoints for rooms and messages (see API.md), including:
    • Create/update/delete/archive rooms
    • List messages with optional search query param
    • Create messages with optional attachments
    • Global message search endpoint

WebSocket Events

Powered by hono/bun upgrade helper. Events include:

  • join_room, leave_room for presence
  • typing indicator per user/room
  • new_message broadcast to room members

🎯 Supported Models (examples)

OpenAI

  • openai/gpt-4o - GPT-4o
  • openai/gpt-4o-mini - GPT-4o Mini
  • openai/gpt-3.5-turbo - GPT-3.5 Turbo

Anthropic

  • anthropic/claude-3-5-sonnet-20241022 - Claude 3.5 Sonnet
  • anthropic/claude-3-5-haiku-20241022 - Claude 3.5 Haiku
  • anthropic/claude-3-opus-20240229 - Claude 3 Opus

Google

  • google/gemini-1.5-pro - Gemini 1.5 Pro
  • google/gemini-1.5-flash - Gemini 1.5 Flash

Groq

  • groq/llama-3.1-70b-versatile - Llama 3.1 70B
  • groq/llama-3.1-8b-instant - Llama 3.1 8B
  • groq/mixtral-8x7b-32768 - Mixtral 8x7B

πŸ“Š Analytics & Monitoring

OpenGateway AI provides a comprehensive analytics dashboard:

  • Usage Statistics: Total requests, tokens, dan biaya
  • Performance Metrics: Latency dan error rates
  • Provider Comparison: Perbandingan performa antar provider
  • Cost Analysis: Analisis biaya per provider dan model
  • Real-time Monitoring: Monitoring real-time untuk semua request

πŸ”’ Security

  • API Key Authentication: Secure API key system
  • Rate Limiting: Abuse & DDoS protection
  • Input Validation: Strict schema validation
  • Error Handling: Safe errors without leaking sensitive data
  • CORS Protection: Tight CORS configuration

πŸš€ Deployment

Production Deployment

  1. Setup production environment
# Set environment variables
export NODE_ENV=production
export DATABASE_URL="your-production-db-url"
export REDIS_URL="your-production-redis-url"
# ... other production variables
  1. Build and deploy
# Build backend
cd packages/backend
bun run build

# Build frontend
cd packages/frontend
bun run build

# Deploy using your preferred method (Docker, PM2, etc.)

Docker Production

# Build production images
docker-compose -f docker-compose.prod.yml build

# Deploy
docker-compose -f docker-compose.prod.yml up -d

🀝 Contributing

  1. Fork repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

πŸ“„ License

Distributed under the MIT License. See LICENSE for more information.

πŸ” New Authentication Features (recent)

Two-Factor Authentication

# Setup 2FA
POST /auth/2fa/setup

# Verify and enable
POST /auth/2fa/verify
{
  "token": "123456"
}

# Check status
GET /auth/2fa/status

# Disable 2FA
POST /auth/2fa/disable
{
  "token": "123456"
}

Enhanced Login with 2FA

# Login (may require 2FA)
POST /auth/login
{
  "email": "[email protected]",
  "password": "password123",
  "twoFactorCode": "123456"  # Optional, required if 2FA enabled
}

Email Notifications

  • Email verification on registration
  • Password reset emails
  • 2FA setup notifications
  • Usage alerts
  • Security notifications
  • Welcome emails

πŸ“§ Email Configuration

Configure SMTP dalam .env:

SMTP_HOST="smtp.gmail.com"
SMTP_PORT=587
SMTP_USER="[email protected]"
SMTP_PASS="your-app-password"
FROM_EMAIL="noreply@your-host"
FROM_NAME="OpenGateway AI"

πŸ”” Platform Notifications

Webhook Integration

WEBHOOK_URL="https://your-app.com/webhooks/OpenGateway"
WEBHOOK_SECRET="your-webhook-secret"

Slack Integration

SLACK_WEBHOOK_URL="https://hooks.slack.com/services/..."

Discord Integration

DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..."

πŸ“± Dashboard Features

User Management

  • Profile management
  • Subscription tier information
  • Usage monitoring
  • Email verification status

API Key Management

  • Create/revoke API keys
  • Usage tracking per key
  • Last used timestamps
  • Secure key display
  • New: Copy button and creation modal feedback

2FA Management

  • Enable/disable 2FA
  • QR code generation
  • Backup codes management
  • Security status

Analytics Dashboard

  • Real-time usage stats
  • Cost analysis
  • Provider performance
  • Historical data

Chat Page (New)

  • Modern navbar with profile dropdown
  • Sidebar with rooms (active/archive, title edit, delete)
  • Dynamic per-room model selection
  • Message list with streaming, typing indicator
  • File attachments (upload + preview)
  • Local and server-side search
  • Responsive and mobile-friendly

TODO:

  • Issue Streaming on Chat Interface
  • Need Enhance the Chat Components

πŸ“ž Support

πŸ™ Acknowledgments

  • OpenAI for powerful API
  • Anthropic for Claude models
  • Google for Gemini models
  • Meta for Llama models
  • Groq for fast inference
  • Hono team for an excellent framework
  • Next.js team for an amazing React framework

About

Open Gateway is an API gateway that lets you integrate multiple AI providers through a single, consistent endpoint. Switch between OpenAI, Anthropic, Google, Groq, and more without changing your application code.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages