A smart AI gateway for managing multiple LLM provider keys in one place with OpenAI-compatible interface, automatic key rotation, cost tracking, and rate limiting.
- AES-256-GCM encryption for all provider keys
- Independent project keys for each application
- Decryption only in temporary memory
- Auto-detect provider from key prefix (gsk_, sk-or-, AIza, etc.)
- Verify each key before saving
- Auto-scan available models for each provider
- Automatic key rotation on failure
- Retry support with cooldown periods
- Fallback between providers
- Budget tracking per project
- Cost calculation per request
- Alerts when budget exceeded
- RPM (Requests Per Minute)
- TPM (Tokens Per Minute)
- Customizable limits per project
- Detailed logs for each request
- Usage and performance tracking
- Integrated admin interface
- Project overview and architecture
- Implemented updates, verification, deployment, and known limitations
- Planned upgrades
- Node.js 18 or later
- npm or yarn
- Clone the repository:
git clone https://github.com/amanj940/key-proxy.git
cd key-proxy- Install dependencies:
npm install- Generate encryption key:
openssl rand -hex 32- Create
.envfile:
MASTER_KEY=<64-char-hex-key-from-step-3>
DB_PATH=./data/key-proxy.db
PORT=8787
HOST=127.0.0.1- Start the server:
npm startOr:
node server.jsOpen your browser at:
http://127.0.0.1:8787/admin
- In the admin panel, paste provider keys in the "Add Provider Keys" section
- The system will auto-detect the provider and verify the key
- You can paste a complete
.envfile or multiple keys
- Enter project name
- (Optional) Set budget in USD
- (Optional) Set RPM and TPM limits
- Click "Create Key"
- Copy the key immediately (it won't appear again)
# Set environment variables
export OPENAI_BASE_URL=http://127.0.0.1:8787/v1
export OPENAI_API_KEY=sk-kp-<your-project-key>import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'http://127.0.0.1:8787/v1',
apiKey: 'sk-kp-<your-project-key>'
});
const response = await client.chat.completions.create({
model: 'auto', // Will auto-select best model
messages: [{ role: 'user', content: 'Hello!' }]
});const response = await client.chat.completions.create({
model: 'groq/llama-3.3-70b-versatile', // provider/model
messages: [{ role: 'user', content: 'Hello!' }]
});- Groq
- OpenRouter
- Google Gemini
- Cerebras
- Mistral
- SambaNova
- NVIDIA
- XAI
- Fireworks
- HuggingFace
// When creating a project key
await fetch('http://127.0.0.1:8787/admin/api/clients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'my-project',
budget: 100.00 // $100 max spend
})
});await fetch('http://127.0.0.1:8787/admin/api/clients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'my-project',
rpmLimit: 100, // 100 requests per minute
tpmLimit: 10000 // 10000 tokens per minute
})
});# Create project key
POST /admin/api/clients
Body: { name, budget?, rpmLimit?, tpmLimit? }
# Delete project key
DELETE /admin/api/clients/:id
# Update limits
PUT /admin/api/clients/:id
Body: { budget?, rpmLimit?, tpmLimit? }
# View spend logs
GET /admin/api/clients/:id/spend?limit=100&offset=0# Add provider
PUT /admin/api/providers/:name
Body: { baseUrl, defaultModel? }
# Delete provider
DELETE /admin/api/providers/:name
# Add keys
POST /admin/api/providers/:name/keys
Body: { keys: "key1,key2,key3" }
# Scan models
POST /admin/api/providers/:name/rescanThe project uses SQLite for the database:
- Location:
./data/key-proxy.db - Tables:
providers: Provider informationkeys: Provider keys (encrypted)models: Available modelsclients: Project keysspend_logs: Usage logs
# Backup database
cp data/key-proxy.db data/key-proxy-backup-$(date +%Y%m%d).db# Run tests
npm test
# Run without watch mode
npm run test:run- Use a reverse proxy (nginx, Apache)
- Enable HTTPS
- Set a strong MASTER_KEY
- Use regular database backups
- Monitor usage logs
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://127.0.0.1:8787;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Error: MASTER_KEY must be 64 hex charsSolution: Make sure you generated a 64 hex char key:
openssl rand -hex 32Error: listen EADDRINUSE: address already in useSolution:
# Find the process
lsof -ti:8787
# Kill it
kill <pid>If you encounter database errors:
# Stop the server
# Remove WAL files
rm data/key-proxy.db-wal data/key-proxy.db-shm
# RestartMIT
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
For support and questions, open an issue on GitHub.