Skip to main content

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 user
  • python -m api_farm login <username> <password> - Login and save authentication token
  • python -m api_farm logout - Logout and remove token
  • python -m api_farm add-key <api_key> [--base-url <url>] - Add an API key to your pool
  • python -m api_farm list-keys - List all your API keys
  • python -m api_farm remove-key <api_key> - Remove an API key from your pool
  • python -m api_farm chat "[{\"role\":\"user\", \"content\":\"Hello!\"}]" and it succeeded.
  • python -m api_farm batch-chat -f batch_messages.json with 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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

api_farm-0.1.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

api_farm-0.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file api_farm-0.1.0.tar.gz.

File metadata

  • Download URL: api_farm-0.1.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for api_farm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 db7ae568a68f7850a94309fae0edc1b10eecb2eb46d0ddbe1ff6fc5c829de278
MD5 9889a410b94073b40e738df9e90ff740
BLAKE2b-256 784ed81c6658bae398079bbed1ab6624f335297c16fa35228a9cca1f785e8ace

See more details on using hashes here.

File details

Details for the file api_farm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: api_farm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for api_farm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 549dd5b4cb650c9abb51764975ec4c1b15be4167a610fc19b6a8520f919aa0a2
MD5 e549985a6b51ebd1e9418dbac4865c26
BLAKE2b-256 f5fe021388c446cd5074d717687ff90b0c2e6bd1b06f9b7a09d435291efb5f6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page