A shared API key pool server and client for managing and using API keys
Project description
API Farm
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.
Features
- 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
Installation
From Source (Development)
# Clone the repository
git clone <your-repo-url>
cd api_farm
# Install in development mode
pip install -e .
From PyPI (When Published)
pip install api-farm
Quick Start
1. Start the Server
# 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 8081
The 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
============================================================
2. Configure Client Environment
Required: Set the server URL as an environment variable:
# Use the URL displayed by the server
export API_FARM_SERVER_URL=http://localhost:8081
3. Use the CLI
# 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 logout
Usage
Command Line Interface (CLI)
The 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
Python SDK
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()
Programmatic Server Usage
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)
Environment Variables
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:
Architecture
- 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
Development
Running Tests
python -m pytest test/
Building the Package
# Install build tools
pip install build
# Build distribution packages
python -m build
# Install from wheel
pip install dist/api_farm-*.whl
License
MIT License
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 Distributions
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 api_farm-0.1.0.1-py3-none-any.whl.
File metadata
- Download URL: api_farm-0.1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ffe7b802a03dc28bced481de5daec6b37633cd2424823444b3824911e179001
|
|
| MD5 |
db5d9df0f037c3c06b10b79934c28150
|
|
| BLAKE2b-256 |
e6149851604848c671cf0d22371e7aefc6f9fce9c7e3cf9a9b83e6bd819c840d
|