A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities.
Project description
🐾 NyaProxy - Universal API Proxy
🌈 Introduction
Your purr-fect Swiss Army Knife for API Proxy Management~
NyaProxy is a versatile API proxy that goes beyond just OpenAI APIs! Whether you're working with AI models (OpenAI, Gemini, Anthropic), image generation services, or any other REST API that needs robust key management - NyaProxy has your back.
The possibilities are limited only by your imagination! Use NyaProxy to:
- Balance loads across multiple API keys
- Create resilient API systems with automatic failover
- Build cost-effective solutions by optimizing key usage
- Secure your API keys behind a proxy layer
- Monitor and analyze your API usage in real-time
🌟 Core Capabilities
| Feature | Description | Config Reference |
|---|---|---|
| 🔄 Token Rotation | Automatic key cycling across multiple providers | variables.keys |
| ⚖️ Load Balancing | 5 strategies: Round Robin, Random, Least Request, Fastest Response, Weighted | load_balancing_strategy |
| 🚦 Rate Limiting | Granular controls per endpoint/key with smart queuing | rate_limit |
| 🕵️ Request Masking | Dynamic header substitution across multiple identity providers | headers + variables |
| 📊 Real-time Metrics | Interactive dashboard with request analytics and system health | dashboard |
| 🔧 Body Substitution | Dynamic JSON payload transformation using JSONPath | request_body_substitution |
| 🔄 Simulated Streaming | Stream chunked responses for better UX | simulated_streaming |
📥 Quick Start
One-Click Deployment (No Fuss, No Muss!)
Pick your favorite platform and let's go!
|
Deploy to Render |
Deploy to Railway |
Local Deployment (For the DIY Enthusiasts!)
Prerequisites
- Python 3.8 or higher
- Docker (optional, for containerized deployment)
Installation
1. Install from PyPI (the easiest way!)
pip install nya-proxy
2. Run NyaProxy
This creates a basic configuration file in your current folder:
nyaproxy
...or provide your own config file:
nyaproxy --config config.yaml
3. Verify Your Setup
Visit http://localhost:8080/config to access the configuration UI.
Note: Please secure your master API key for safety!
Check out http://localhost:8080/dashboard for the snazzy management dashboard with all your API traffic visualizations.
Install from Source
# Clone the repository
git clone https://github.com/Nya-Foundation/nyaproxy.git
cd nyaproxy
# Install dependencies
pip install -e .
# Run NyaProxy
nyaproxy
Docker
docker run -d \
-p 8080:8080 \
# -v ${PWD}/config.yaml:/app/config.yaml \
# -v nya-proxy-logs:/app/logs \
k3scat/nya-proxy:latest
Configuration
Configuration reference can be found under Configs folder folder
# Basic config.yaml example for Gemini API
nya_proxy:
host: 0.0.0.0
port: 8080
api_key:
logging:
enabled: true
level: info
log_file: app.log
proxy:
enabled: false
address: socks5://username:password@proxy.example.com:1080
dashboard:
enabled: true
queue:
enabled: true
max_size: 200
expiry_seconds: 300
# Default settings for the all apis if not specified
default_settings:
key_variable: keys
load_balancing_strategy: round_robin
rate_limit:
endpoint_rate_limit: 10/s
key_rate_limit: 10/m
rate_limit_paths:
- "*"
retry:
enabled: true
mode: key_rotation
attempts: 3
retry_after_seconds: 1
retry_request_methods: [ POST, GET, PUT, DELETE, PATCH, OPTIONS ]
retry_status_codes: [ 429, 500, 502, 503, 504 ]
timeouts:
request_timeout_seconds: 300
# Simulated streaming settings
simulated_streaming:
enabled: false
delay_seconds: 0.2 # Delay between chunks in seconds
init_delay_seconds: 0.5 # Initial delay before starting streaming in seconds
chunk_size_bytes: 256 # Size of each chunk in bytes
apply_to: ["application/json", "application/xml", "text/plain", "image/png", "image/jpeg"] # Response content types to apply simulated streaming to
# API configurations, each API can have its own settings, but will inherit from default_settings if not specified
apis:
gemini:
name: Google Gemini API
endpoint: https://generativelanguage.googleapis.com/v1beta/openai
aliases:
- /gemini
key_variable: keys
headers:
Authorization: 'Bearer ${{keys}}'
variables:
keys:
- your_gemini_key_1
- your_gemini_key_2
- your_gemini_key_3
load_balancing_strategy: least_requests
rate_limit:
# For Gemini, the rate limits (gemini-2.5-pro-exp-03-25) for each key are 5 RPM and 25 RPD
# Ideally, the endpoint rate limit should be n x Per-Key-RPD, where n is the number of keys
endpoint_rate_limit: 75/d
key_rate_limit: 5/m
# Rate limit paths are optional, but you can configure which paths to apply the rate limits to (regex supported), default is all paths "*"
rate_limit_paths:
- "/chat/*"
- "/images/*"
# [Advanced] Request body substitution settings, do not enable unless you know what you are doing
request_body_substitution:
enabled: true
# Substitution rules for request body with JMEPath
rules: # JMEPath rules for request body substitution
- name: "Remove frequency_penalty" # if frequency_penalty is present, remove it from the request body since Gemini does not support it
operation: remove
path: "frequency_penalty"
conditions:
- field: "frequency_penalty"
operator: "exists"
- name: "Remove presence_penalty" # if presence_penalty is present, remove it from the request body since Gemini does not support it
operation: remove
path: "presence_penalty"
conditions:
- field: "presence_penalty"
operator: "exists"
# feel free to add more APIs here, just follow the same structure as above
📡 Service Endpoints
| Service | Endpoint | Description |
|---|---|---|
| API Proxy | http://localhost:8080/api/<endpoint_name> |
Main proxy endpoint for API requests |
| Dashboard | http://localhost:8080/dashboard |
Real-time metrics and monitoring |
| Config UI | http://localhost:8080/config |
Visual configuration interface |
Note: Replace 8080 with your configured port if different
🔧 API Configuration
OpenAI-Compatible APIs (Gemini, Anthropic, etc)
gemini:
name: Google Gemini API
endpoint: https://generativelanguage.googleapis.com/v1beta/openai
aliases:
- /gemini
key_variable: keys
headers:
Authorization: 'Bearer ${{keys}}'
variables:
keys:
- your_gemini_key_1
- your_gemini_key_2
load_balancing_strategy: least_requests
rate_limit:
endpoint_rate_limit: 75/d # Total endpoint limit
key_rate_limit: 5/m # Per-key limit
rate_limit_paths:
- "/chat/*" # Apply limits to specific paths
- "/images/*"
Generic REST APIs
novelai:
name: NovelAI API
endpoint: https://image.novelai.net
aliases:
- /novelai
key_variable: tokens
headers:
Authorization: 'Bearer ${{tokens}}'
variables:
tokens:
- your_novelai_token_1
- your_novelai_token_2
load_balancing_strategy: round_robin
rate_limit:
endpoint_rate_limit: 10/s
key_rate_limit: 2/s
🖥️ Management Interfaces
Real-time Metrics Dashboard
Monitor at http://localhost:8080/dashboard:
- Request volumes and response times
- Rate limit status and queue depth
- Key usage and performance metrics
- Error rates and status codes
Visual Configuration Interface
Manage at http://localhost:8080/config:
- Live configuration editing
- Syntax validation
- Variable management
- Rate limit adjustments
- Auto reload on save
Advanced Features
Request Body Substitution
Dynamically transform JSON payloads using JMESPath expressions to add, replace, or remove fields:
request_body_substitution:
enabled: true
rules:
- name: "Default to GPT-4"
operation: set
path: "model"
value: "gpt-4"
conditions:
- field: "model"
operator: "exists"
For detailed configuration options and examples, see the Request Body Substitution Guide.
🛡️ Reference Architecture
graph TD
A[Client] --> B[Nginx]
B --> C[NyaProxyAuth]
C --> D[NyaProxyApp]
D --> E[API Providers]
F[Monitoring] --> D
🌌 Future Roadmap
graph LR
A[Q1 2025] --> B[📡 Documentation Enhancement ]
A --> C[🔄 Border Test Coverage ]
B --> D[📈 API Key Usage/Balance Tracking]
C --> E[📊 UI/UX Enhancement ]
F[Q2 2025] --> G[🧩 Plugin System]
F --> H[🔍 Custom Metrics API]
❤️ Community
Need enterprise support? Contact k3scat@gmail.com
📈 Project Growth
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nya_proxy-0.0.8.tar.gz.
File metadata
- Download URL: nya_proxy-0.0.8.tar.gz
- Upload date:
- Size: 85.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bfcaeeef7a8fba5634309d0fb2114091094242b30ec70d9ac14c2a823a77242
|
|
| MD5 |
bf935df704c46a67966a16ea1fc79d67
|
|
| BLAKE2b-256 |
7936114483a729b6d58be34f0ae91965489c1b4fdc541df9e18a16137dd9012c
|
Provenance
The following attestation bundles were made for nya_proxy-0.0.8.tar.gz:
Publisher:
publish.yml on Nya-Foundation/NyaProxy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nya_proxy-0.0.8.tar.gz -
Subject digest:
7bfcaeeef7a8fba5634309d0fb2114091094242b30ec70d9ac14c2a823a77242 - Sigstore transparency entry: 204600681
- Sigstore integration time:
-
Permalink:
Nya-Foundation/NyaProxy@2f5290bf9ba1a8fee69a5c496c4d74ad3f955bc2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Nya-Foundation
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2f5290bf9ba1a8fee69a5c496c4d74ad3f955bc2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file nya_proxy-0.0.8-py3-none-any.whl.
File metadata
- Download URL: nya_proxy-0.0.8-py3-none-any.whl
- Upload date:
- Size: 92.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb6fac9864d96613ba0c0db1df794225a8b6a8163ba565904968f4f955571773
|
|
| MD5 |
fb9ec55c89b4b9a3cb10c5c8801d8d06
|
|
| BLAKE2b-256 |
d2de6ed4da71989446ca65634faccd7b66871a3a4fe1bfe9d91d509e189a841c
|
Provenance
The following attestation bundles were made for nya_proxy-0.0.8-py3-none-any.whl:
Publisher:
publish.yml on Nya-Foundation/NyaProxy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nya_proxy-0.0.8-py3-none-any.whl -
Subject digest:
fb6fac9864d96613ba0c0db1df794225a8b6a8163ba565904968f4f955571773 - Sigstore transparency entry: 204600682
- Sigstore integration time:
-
Permalink:
Nya-Foundation/NyaProxy@2f5290bf9ba1a8fee69a5c496c4d74ad3f955bc2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Nya-Foundation
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2f5290bf9ba1a8fee69a5c496c4d74ad3f955bc2 -
Trigger Event:
push
-
Statement type: