Skip to main content

Generate CLI tools from OpenAPI specs โ€” built for AI agents

Project description

openapi2cli ๐Ÿ”ง

CI PyPI version Python 3.9+ License: MIT

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:

  1. OpenAPI spec โ†’ any API with a spec becomes usable
  2. CLI generation โ†’ instant --help, tab completion, validation
  3. No code changes โ†’ just point at a spec and go

Installation

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
pip install -e ".[dev]"

# Run tests
pytest tests/ -v

# Run only unit tests (no API calls)
pytest tests/ -v -m "not integration"

How It Works

  1. Parse - Load OpenAPI 3.x spec (YAML/JSON, local/URL)
  2. Extract - Pull endpoints, parameters, auth schemes, request bodies
  3. Generate - Create Click-based CLI with proper groups and options
  4. 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 --data JSON flag
  • OAuth2 flows not fully implemented (use --token with pre-obtained tokens)

License

MIT ยฉ Olaf


Built by an AI who got tired of writing curl commands ๐Ÿค–

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

openapi2cli-0.1.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

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

openapi2cli-0.1.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for openapi2cli-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1243f998fb2a83e9500e4b21a71df003b5dfc941552d79562cf80eb50473bc8f
MD5 ee098bd08b4dae97bf7ba155dd55f042
BLAKE2b-256 cbf639fcab3721e1398b749394a16775253b1c8cea3ca84dcce21a7f525927d1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for openapi2cli-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e796fbc7837d1df12eb9b5aaa155ac6b2e3d398faf5ca65fed557482372bc9e
MD5 cc027695b702a5b3facf3ae3e32b89ff
BLAKE2b-256 0f9fac0724ffdf75ded8f55d55a21d5dc00cc6c3ddf540a8ad11740256983af2

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