A shared API key pool server and client for managing and using API keys efficiently. This package supports both command-line interface (CLI) and programmatic (SDK) usage.
- Shared API Key Pool: Centralize API keys for multiple users
- User Management: Register, login, and manage API keys per user
- Dual Interface: Use via CLI commands or Python SDK
- Load Balancing: Automatic rotation across available API keys
- Easy Installation: Pip-installable package
# Clone the repository
git clone <your-repo-url>
cd api_farm
# Install in development mode
pip install -e .pip install api-farm# Start the server (prints IP address for client connection)
api-farm-server
# Or with custom host/port
api-farm-server --host 0.0.0.0 --port 8081The server will display connection information:
============================================================
API Farm Server Starting...
============================================================
Server running on:
- Local: http://localhost:8081
- Network: http://192.168.1.100:8081
To connect clients, set the environment variable:
export API_FARM_SERVER_URL=http://192.168.1.100:8081
============================================================
Required: Set the server URL as an environment variable:
# Use the URL displayed by the server
export API_FARM_SERVER_URL=http://localhost:8081# Register a new user
api-farm register myuser mypassword
# Login
api-farm login myuser mypassword
# Add an API key
api-farm add-key "your-api-key-here"
# Import keys from a file
api-farm add-key --file api.json
# List your keys
api-farm list-keys
# Remove a key
api-farm remove-key "your-api-key-here"
# Logout
api-farm logoutThe CLI requires the API_FARM_SERVER_URL environment variable to be set.
Available Commands:
python -m api_farm register <username> <password>- Register a new userpython -m api_farm login <username> <password>- Login and save authentication tokenpython -m api_farm logout- Logout and remove tokenpython -m api_farm add-key <api_key> [--base-url <url>]- Add an API key to your poolpython -m api_farm list-keys- List all your API keyspython -m api_farm remove-key <api_key>- Remove an API key from your poolpython -m api_farm chat "[{\"role\":\"user\", \"content\":\"Hello!\"}]"and it succeeded.python -m api_farm batch-chat -f batch_messages.jsonwith a test file and it succeeded.
Server Commands:
api-farm-server [--host HOST] [--port PORT]- Start the API Farm server
Import and use the client in your Python code:
from api_farm import APIPoolClient
import os
# Method 1: Use environment variable
os.environ['API_FARM_SERVER_URL'] = 'http://localhost:8081'
client = APIPoolClient()
# Method 2: Pass server URL explicitly
client = APIPoolClient(server_url='http://localhost:8081')
# Register a user
response = client.register('username', 'password')
print(response)
# Login
token = client.login('username', 'password')
# Add API key
client.add_key('your-api-key', base_url='https://integrate.api.nvidia.com/v1')
# Import keys from file
client.add_keys_from_file("api.json")
# List keys
keys = client.list_keys()
print(keys)
# Make a chat completion request (uses shared pool)
response = client.chat_completions(
model='meta/llama-3.1-8b-instruct',
messages=[{'role': 'user', 'content': 'Hello!'}],
max_tokens=100
)
print(response)
# Remove a key
client.remove_key('your-api-key')
# Logout
client.logout()You can also start the server programmatically:
from api_farm.server import app
import uvicorn
# Start the server
uvicorn.run(app, host="0.0.0.0", port=8081)API_FARM_SERVER_URL(Required for clients): The URL of the API Farm server- Example:
http://localhost:8081 - Set before using CLI or SDK
- Example:
- Server (
api_farm.server): FastAPI-based server managing users and API keys - Client SDK (
api_farm.client_sdk): Python client for programmatic access - CLI (
api_farm.cli): Command-line interface for interactive use
python -m pytest test/# Install build tools
pip install build
# Build distribution packages
python -m build
# Install from wheel
pip install dist/api_farm-*.whlMIT License