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 (pip)

pip install cli2api
cli2api

Server starts at http://localhost:8000

From Source

git clone https://github.com/anoxis/CLI2API.git
cd CLI2API
pip install -e .
cli2api

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
  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.2.2.tar.gz (46.7 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.2.2-py3-none-any.whl (38.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cli2api-0.2.2.tar.gz
Algorithm Hash digest
SHA256 31a9e825fb159cf6175a8272c33c4d196c74b7626a3af61480836ff1efad2233
MD5 a9ef500e6fb8aace2810ae354b9a409f
BLAKE2b-256 a8ecbb8badc82148703e50e63c607efb5ea94cf95486f7054fdfdbd6814b3e36

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cli2api-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 38.8 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.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8c800e735441aa8827ca7fdfdd413daf46577b5edabab4eb2d5ce3fe5bc8f305
MD5 d064010a8a3f730fe8a4279c2fd4a4f8
BLAKE2b-256 f389e78383cdba5ef5c96d1ec97f2785d71175ad150c203be4cf1844e680ee28

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