- Home
- Documentation
- CLI Generation
- Generate CLI
Generate a CLI from an OpenAPI Document
Speakeasy generates a fully functional command-line interface from an OpenAPI specification. The CLI is written in Go using Cobra and wraps a generated Go SDK, providing per-operation commands, built-in authentication, multiple output formats, shell completions, and cross-platform distribution tooling.
Prerequisites
Section titled “Prerequisites”- The Speakeasy CLI
- An API spec in a supported format:
Quickstart
Section titled “Quickstart”1. Create a CLI
Section titled “1. Create a CLI”Run the Speakeasy quickstart command and select the CLI target:
speakeasy quickstart --target cliThe interactive flow prompts for three configuration values:
2. Review the generated output
Section titled “2. Review the generated output”After generation, the output is a complete Go project:
├── cmd/│ ├── <cliName>/main.go # Binary entrypoint│ └── gendocs/main.go # Cobra documentation generator├── internal/│ ├── cli/ # Cobra command files (root, auth, configure, per-operation)│ ├── client/ # SDK client wrapper and diagnostics│ ├── config/ # Config file and OS keychain management│ ├── flagutil/ # Flag registration and request building│ ├── output/ # Output formatting and agent-mode behavior│ ├── usage/ # Grouped help and machine-readable usage schema│ ├── explorer/ # Interactive command explorer (when enabled)│ └── sdk/ # Auto-generated Go SDK├── scripts/│ ├── install.sh # Linux/macOS install script│ └── install.ps1 # Windows install script├── .goreleaser.yaml # Cross-platform binary builds├── go.mod└── README.md3. Build and run
Section titled “3. Build and run”go build -o petstore ./cmd/petstore./petstore --help4. Configure authentication
Section titled “4. Configure authentication”Run the interactive setup wizard to configure API credentials and global settings:
./petstore configureWhen available, secrets are stored in the OS keychain (macOS Keychain, Windows Credential Manager, or Linux Secret Service / keyring-compatible backends). Non-secret configuration is stored in ~/.config/<cliName>/config.yaml.
If interactive auth is enabled and the API has global security, generated CLIs also expose an auth command group:
./petstore auth login./petstore auth whoami./petstore auth logoutHow generated CLIs behave
Section titled “How generated CLIs behave”Per-operation commands
Section titled “Per-operation commands”Every API operation becomes a CLI command. Operations are grouped by tags, with smart stutter removal to keep command names clean:
# If the API has a "users" tag with a "list-users" operation:petstore users list
# View all available commands:petstore --helpRequest input options
Section titled “Request input options”Generated CLIs support multiple request input styles with predictable precedence:
- Individual flags (highest priority)
--bodyJSON- stdin JSON (lowest priority)
# Individual flagspetstore users create --name "Alice" --email "alice@example.com"
# Whole request body JSONpetstore users create --body '{"name":"Alice","email":"alice@example.com"}'
# Stdin pipingcat payload.json | petstore users createIndividual flags override values supplied through --body or stdin, which makes the CLI work well for both scripts and ad hoc usage.
Bytes and base64 request input
Section titled “Bytes and base64 request input”Request fields typed as bytes are exposed as flags that accept three forms:
# Read bytes from diskpetstore files upload --contents file:./avatar.png
# Decode base64 firstpetstore files upload --contents b64:SGVsbG8=
# Use raw string bytes directlypetstore files upload --contents helloSupported base64 forms include padded and unpadded standard base64 plus URL-safe base64 variants.
Output formats
Section titled “Output formats”Control output format with the --output-format flag (or -o):
petstore users list -o prettypetstore users list -o jsonpetstore users list -o yamlpetstore users list -o tablepetstore users list -o toonprettyis the default for human terminal usetoonis optimized for compact, line-oriented agent consumption--jqapplies a jq expression and produces JSON output
petstore users list --jq '.[] | {name: .name, email: .email}'Binary responses
Section titled “Binary responses”Binary or file-like responses get binary-safe handling:
# Save directly to diskpetstore files download --id file_123 --output-file ./report.pdf
# Print as base64 instead of raw bytespetstore files download --id file_123 --output-b64For binary-only operations, the CLI blocks writing raw binary directly to an interactive terminal and instructs the user to use --output-file, --output-b64, or piping.
Response headers
Section titled “Response headers”Generated CLIs can optionally include HTTP response headers in output:
petstore users get --id user_123 --include-headers -o jsonThis is useful for surfacing pagination headers, rate-limit metadata, or request tracing identifiers.
Pagination and streaming
Section titled “Pagination and streaming”When an operation is marked as paginated, the CLI adds --all and --max-pages:
petstore users list --allpetstore users list --all --max-pages 5Streaming endpoints are also supported:
- SSE (
text/event-stream) - JSONL / NDJSON (
application/jsonl,application/x-ndjson)
These are emitted incrementally rather than buffered.
Retries, timeout, and custom headers
Section titled “Retries, timeout, and custom headers”Generated CLIs include runtime controls that map to the underlying SDK behavior. Retry flags are available when retry support is enabled for the generated target/spec:
petstore users list --timeout 30spetstore users list --no-retriespetstore users list --retry-max-elapsed-time 10spetstore users list --header "X-Request-ID: abc-123"Diagnostics
Section titled “Diagnostics”The CLI includes built-in diagnostics that are safe for scripts and CI:
petstore users list --dry-runpetstore users list --debug--dry-runshows the request that would be sent without making a network call--debuglogs request/response diagnostics to stderr- both paths automatically redact common secret-bearing headers and payload fields
Interactive mode
Section titled “Interactive mode”Interactive mode is enabled by default in the generator and improves the human terminal experience:
- auto-prompting for unresolved required fields
- an
exploreTUI for browsing and launching commands - interactive
configureflows - interactive
auth loginflows when auth is available and interactive auth is enabled
petstore explorepetstore users createpetstore users create --no-interactiveInteractive prompting and explorer auto-launch only happen when stdin and stdout are TTYs.
Agent mode
Section titled “Agent mode”Generated CLIs also support an agent-aware runtime mode optimized for AI coding assistants and non-human terminal drivers.
petstore users list --agent-modepetstore users list --agent-mode=falseAgent mode can also auto-enable when the CLI detects well-known agent environments such as Claude Code, Cursor, Codex, Aider, Cline, Windsurf, GitHub Copilot, Amazon Q, Gemini Code Assist, and Cody.
When agent mode is active:
- the default output format becomes
toon - errors become structured and machine-readable
- interactive surfaces are suppressed or blocked
- explorer auto-launch is disabled
Machine-readable usage schema
Section titled “Machine-readable usage schema”Generated CLIs provide both standard Cobra help and a machine-readable usage schema:
petstore users create --helppetstore users create --usage--usage emits a KDL representation of the selected command, including flags, defaults, help text, aliases, and configuration metadata.
Shell completions
Section titled “Shell completions”Generated CLIs support Cobra’s built-in completion command:
petstore completion bashpetstore completion zshpetstore completion fishpetstore completion powershellSource the generated output directly in a shell session or install completion scripts as part of the CLI setup.
Authentication and configuration precedence
Section titled “Authentication and configuration precedence”The generated CLI resolves values in a predictable order:
- Security credentials: flags → environment variables → OS keychain → config file
- Global parameters: flags → environment variables → config file
That means users can mix persistent configuration with per-command overrides without changing the generated code.
Next steps
Section titled “Next steps”- Customize a CLI — Configure command naming, environment variables, interactive features, and release artifacts
- Distribute a CLI — Set up GoReleaser, install scripts, and release automation
- Configuration reference — Full
gen.yamlreference for theclitarget