OpenAPI to CLI
@awlevin
npx machina-cli add skill @awlevin/openapi2cli --openclawOpenAPI to CLI
Generate command-line tools from OpenAPI/Swagger specs. Perfect for AI agents that need to interact with APIs without writing curl commands.
Quick Start
# generate a CLI from any OpenAPI spec
uvx openapi2cli generate https://api.example.com/openapi.json --output my-api
# use the generated CLI
python my-api.py users list
python my-api.py users get --id 123
python my-api.py posts create --title "Hello" --body "World"
Features
- Auto-generates CLI from OpenAPI 3.x specs
- Supports auth: API keys, Bearer tokens, Basic auth
- Rich help:
--helpon any command shows params - JSON output: Structured responses for parsing
- Dry-run mode: See the request without sending
Usage
# from URL
uvx openapi2cli generate https://api.example.com/openapi.json -o my-cli
# from local file
uvx openapi2cli generate ./spec.yaml -o my-cli
# with base URL override
uvx openapi2cli generate ./spec.json -o my-cli --base-url https://api.prod.com
Generated CLI
# set auth via env
export MY_CLI_API_KEY="sk-..."
# or via flag
python my-cli.py --api-key "sk-..." users list
# see available commands
python my-cli.py --help
# see command options
python my-cli.py users create --help
Example: GitHub API
uvx openapi2cli generate https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json -o github-cli
python github-cli.py repos list --owner octocat
Why?
AI agents work better with CLIs than raw HTTP:
- Discoverable commands via
--help - Tab completion friendly
- No need to construct JSON payloads
- Easy to chain with pipes
Links
Overview
OpenAPI to CLI generates command-line interfaces directly from OpenAPI/Swagger specs, letting AI agents interact with APIs without crafting curl commands. The generated CLIs support authentication, rich help, JSON outputs, and a dry-run mode to streamline automation.
How This Skill Works
The tool parses the OpenAPI specification and generates a Python-based CLI (via uvx) with endpoints exposed as subcommands. It includes built-in auth support (API keys, Bearer tokens, Basic auth), informative --help output, and JSON-formatted responses, with an option to override the base URL for different environments.
When to Use It
- When you need a CLI for an API described by OpenAPI/Swagger.
- When you want to avoid writing curl commands and payload handling.
- When you require built-in authentication support (API keys, Bearer tokens, Basic auth).
- When you need structured JSON responses for easy automation and parsing.
- When you want to preview requests with a dry-run before sending.
Quick Start
- Step 1: uvx openapi2cli generate https://api.example.com/openapi.json --output my-api
- Step 2: python my-api.py users list
- Step 3: python my-api.py --help
Best Practices
- Start with a small, representative endpoint to validate the generated CLI workflow.
- Test the base URL override to ensure commands point to the correct environment.
- Use --help to discover available commands and parameter details.
- Store API keys securely (environment variables or secret managers) and pass them via env or flags.
- Regenerate the CLI whenever the OpenAPI spec is updated to keep capabilities in sync.
Example Use Cases
- Generate a GitHub API CLI and run repos list --owner octocat: uvx openapi2cli generate https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json -o github-cli; python github-cli.py repos list --owner octocat
- Authenticate via environment variable: export MY_CLI_API_KEY="sk-..." and run a command like python my-cli.py --api-key "sk-..." users list
- Override base URL for a staging API: uvx openapi2cli generate ./spec.json -o my-cli --base-url https://api.staging.example.com
- Generate from a local OpenAPI spec file and list resources: uvx openapi2cli generate ./openapi.yaml -o local-cli; python local-cli.py resources list
- Dry-run to preview requests before sending: python my-cli.py users create --title 'Hello' --body 'World' --dry-run