Generate CLI tools from OpenAPI specs โ built for AI agents
Project description
openapi2cli ๐ง
Generate CLI tools from OpenAPI specs. Built for AI agents who need to interact with APIs.
# Generate a CLI from any OpenAPI spec
$ openapi2cli generate https://httpbin.org/spec.json --name httpbin
# Use it immediately
$ ./httpbin_cli.py get --output json
{
"url": "https://httpbin.org/get",
"headers": { ... }
}
Why?
AI agents are great at executing CLI commands. They're less great at crafting HTTP requests from memory. This tool bridges the gap:
- OpenAPI spec โ any API with a spec becomes usable
- CLI generation โ instant
--help, tab completion, validation - No code changes โ just point at a spec and go
Installation
One-liner
curl -fsSL https://raw.githubusercontent.com/Olafs-World/openapi2cli/main/install.sh | bash
Quick run (no install)
uvx openapi2cli generate https://petstore3.swagger.io/api/v3/openapi.json --name petstore
Install as CLI tool
uv tool install openapi2cli
openapi2cli generate https://httpbin.org/spec.json --name httpbin
Add to a project
uv add openapi2cli
Install as OpenClaw skill
clawhub install openapi2cli
Or with pip:
pip install openapi2cli
Quick Start
Generate a CLI
# From a URL
openapi2cli generate https://petstore3.swagger.io/api/v3/openapi.json --name petstore
# From a local file
openapi2cli generate ./api-spec.yaml --name myapi --output myapi
Use the Generated CLI
# See available commands
./petstore --help
# List pets
./petstore pet find-by-status --status available
# Add a pet (with auth)
export PETSTORE_API_KEY=your-key
./petstore pet add --name "Fluffy" --status available
# JSON output for scripting
./petstore pet get --pet-id 123 --output json | jq '.name'
Inspect a Spec
# See what's in a spec without generating
openapi2cli inspect https://httpbin.org/spec.json
Features
| Feature | Description |
|---|---|
| ๐ Auto-discovery | Parses OpenAPI 3.x specs (YAML or JSON) |
| ๐ท๏ธ Smart grouping | Commands grouped by API tags |
| ๐ Auth support | API keys, Bearer tokens, env vars |
| ๐ Output formats | JSON, table, or raw |
| โก Fast generation | Single command, instant CLI |
| ๐ค Agent-friendly | Self-documenting with --help |
Configuration
Authentication
Generated CLIs support multiple auth methods:
# Via environment variable (recommended)
export PETSTORE_API_KEY=your-key
./petstore pet list
# Via CLI option
./petstore --api-key your-key pet list
# Bearer token
export PETSTORE_TOKEN=your-bearer-token
./petstore pet list
The env var prefix is derived from the CLI name (uppercase, underscores).
Base URL Override
# Use a different API server
./petstore --base-url https://staging.petstore.io/api pet list
Output Formats
# JSON (default, good for piping)
./petstore pet list --output json
# Table (human-readable, requires rich)
./petstore pet list --output table
# Raw (API response as-is)
./petstore pet list --output raw
Generated CLI Structure
For a spec with tags pet, store, user:
petstore
โโโ pet
โ โโโ add # POST /pet
โ โโโ get # GET /pet/{petId}
โ โโโ update # PUT /pet
โ โโโ delete # DELETE /pet/{petId}
โ โโโ find-by-status
โโโ store
โ โโโ order
โ โโโ inventory
โโโ user
โโโ create
โโโ login
โโโ logout
API Reference
openapi2cli generate
openapi2cli generate SPEC --name NAME [--output PATH] [--stdout]
Arguments:
SPEC OpenAPI spec (file path or URL)
Options:
-n, --name CLI name (required)
-o, --output Output file path (default: {name}_cli.py)
--stdout Print to stdout instead of file
openapi2cli inspect
openapi2cli inspect SPEC
Arguments:
SPEC OpenAPI spec (file path or URL)
Python API
from openapi2cli import OpenAPIParser, CLIGenerator
# Parse a spec
parser = OpenAPIParser()
spec = parser.parse("https://api.example.com/openapi.json")
print(f"API: {spec.title}")
print(f"Endpoints: {len(spec.endpoints)}")
# Generate CLI
generator = CLIGenerator()
cli = generator.generate(spec, name="example")
# Save to file
cli.save("example_cli.py")
# Or get the code
code = cli.to_python()
Development
# Clone
git clone https://github.com/Olafs-World/openapi2cli.git
cd openapi2cli
# Install dev dependencies
uv sync --extra dev
# Run tests
uv run pytest tests/ -v
# Run only unit tests (no API calls)
uv run pytest tests/ -v -m "not integration"
How It Works
- Parse - Load OpenAPI 3.x spec (YAML/JSON, local/URL)
- Extract - Pull endpoints, parameters, auth schemes, request bodies
- Generate - Create Click-based CLI with proper groups and options
- Output - Save as standalone Python script (executable)
The generated CLI uses requests for HTTP and optionally rich for pretty output.
Limitations
- OpenAPI 3.x only (not Swagger 2.0)
- No file upload support yet
- Complex nested request bodies may need
--dataJSON flag - OAuth2 flows not fully implemented (use
--tokenwith pre-obtained tokens)
Links
License
MIT ยฉ Olaf
Built by an AI who got tired of writing curl commands ๐ค
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 Distribution
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 openapi2cli-0.1.1.tar.gz.
File metadata
- Download URL: openapi2cli-0.1.1.tar.gz
- Upload date:
- Size: 789.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d89c381c9cec0b07a83bb7b347d45a49284c93216f775d5159597dbdb7948041
|
|
| MD5 |
7fca7625ebb4d193ea22a2b2a94eac11
|
|
| BLAKE2b-256 |
4873b906c735b01c7fb11baeb52bf42e455482832f4e1c4a3a4f22f9a048e1db
|
File details
Details for the file openapi2cli-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openapi2cli-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86d01bebdb749d1218dc17cb3a9133e2d8dd500af948fc080d29541e1c7c7a3d
|
|
| MD5 |
1ceac9f91b1b415c056046e19da2b46f
|
|
| BLAKE2b-256 |
d49ff0e95195365b2c89b65572ddddd15c3fb3a503cde0dc3f9949cb62f89dc0
|