Skip to main content

OpenAI-compatible API over Claude Code CLI

Project description

CLI2API

OpenAI-compatible API over Claude Code CLI.

Why CLI2API?

Claude Code CLI is powerful but not all tools support it directly. CLI2API bridges this gap by exposing Claude Code as an OpenAI-compatible API.

Use Cases:

  • IDE Integration — Connect Kilo Code, Roo Code, Cursor, or other editors that support OpenAI API
  • Custom Applications — Build apps using familiar OpenAI SDK instead of spawning CLI processes
  • Team Sharing — Run one CLI2API server and share Claude access across your team
  • Tool Compatibility — Use Claude with any tool that supports OpenAI API format (LangChain, AutoGPT, etc.)

Requirements

Installation

Quick Start

# Clone the repository
git clone https://github.com/anoxis/CLI2API.git
cd CLI2API

# Install dependencies
pip install -e .

# Start server
./cli2api.sh

Add to PATH (Recommended)

For easy access from anywhere:

# Add to your shell profile (~/.bashrc, ~/.zshrc, etc.)
export PATH="$PATH:/path/to/CLI2API"

# Then run from anywhere
cli2api.sh

Running After Installation

Once installed, you can start the server in several ways:

# Using the script (recommended)
./cli2api.sh

# Using Python module
python -m cli2api

# Using uvicorn directly
uvicorn cli2api.main:app --host 0.0.0.0 --port 8000

Docker

Docker requires the repository to be cloned first:

# Clone and build
git clone https://github.com/anoxis/CLI2API.git
cd CLI2API
docker build -t cli2api .

# Run with Claude CLI mounted from host
docker run -p 8000:8000 \
  -v $(which claude):/usr/local/bin/claude:ro \
  -e CLI2API_CLAUDE_CLI_PATH=/usr/local/bin/claude \
  cli2api

Or use docker-compose:

# Edit docker-compose.yaml to mount your Claude CLI path
docker-compose up -d

Development

pip install -e ".[dev]"
pytest tests/ -v

IDE Integration

Kilo Code / Roo Code

  1. Start CLI2API server: ./cli2api.sh
  2. Open extension settings
  3. Add custom provider:
    • Provider Name: CLI2API (or any name)
    • API Base URL: http://localhost:8000/v1
    • API Key: not-needed (any non-empty value)
    • Model: sonnet (or opus, haiku)

Cursor / Continue

  1. Start CLI2API server
  2. Settings → Models → Add Model
  3. Configure:
    • API Base: http://localhost:8000/v1
    • API Key: any-value
    • Model: sonnet

Generic OpenAI-compatible client

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="sonnet",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Usage

Test with curl

# Health check
curl http://localhost:8000/health

# List models
curl http://localhost:8000/v1/models

# Chat completion
curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "sonnet", "messages": [{"role": "user", "content": "Hello"}]}'

# Streaming
curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "sonnet", "messages": [{"role": "user", "content": "Hello"}], "stream": true}'

Configuration

Environment variables (prefix CLI2API_):

Variable Default Description
HOST 0.0.0.0 Server host
PORT 8000 Server port
CLAUDE_CLI_PATH auto-detect Path to Claude CLI executable
DEFAULT_TIMEOUT 300 CLI execution timeout (seconds)
DEFAULT_MODEL sonnet Default model
CLAUDE_MODELS sonnet,opus,haiku Available models (comma-separated)
LOG_LEVEL INFO Logging level
LOG_JSON false JSON format logging
DEBUG false Debug mode

Copy .env.example to .env for local configuration.

Available Models

Model ID Description
sonnet Claude Sonnet (default)
opus Claude Opus
haiku Claude Haiku

API Endpoints

Endpoint Description
GET /health Health check
GET / API info
GET /v1/models List available models
GET /v1/models/{id} Get model info
POST /v1/chat/completions Chat completions (OpenAI compatible)
POST /v1/responses Responses API (OpenAI compatible)

Security Notice

CLI2API does not implement authentication. It is designed for local use only.

  • Do not expose to the internet without additional security measures
  • Use behind a reverse proxy with authentication if network access is needed
  • The API inherits permissions from the Claude CLI authentication on the host

License

MIT

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

cli2api-0.1.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

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

cli2api-0.1.0-py3-none-any.whl (28.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cli2api-0.1.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cli2api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 629b91db229da2bfffb0d3cb12b30278651a03f8e72fc4c2f3ae475e3c2aaada
MD5 81eddc3564f674b6fad5ae486c78aece
BLAKE2b-256 c430fc6a26aad16434cae858f0b1d0e67deccd74a541aa4af7d38e722da8359c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cli2api-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cli2api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11d40118f035e50d4458a4b3ee85a690f26f29ed65906ac1f3db906125c23630
MD5 5d7f8be180abadc7c9462a9540ec9dcb
BLAKE2b-256 be1bee879935003cd8ab23f147367bb4cc5d6613a61295825ea02a66d9409475

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