|
| 1 | +--- |
| 2 | +title: "Generate a CLI from an OpenAPI Document" |
| 3 | +description: "Generate a production-ready command-line interface from an OpenAPI document using Speakeasy." |
| 4 | +--- |
| 5 | + |
| 6 | +import { Callout, Table } from "@/mdx/components"; |
| 7 | + |
| 8 | +# Generate a CLI from an OpenAPI document |
| 9 | + |
| 10 | +<Callout type="warning" title="Alpha · Enterprise Only"> |
| 11 | + CLI generation is currently in **alpha** and available exclusively to |
| 12 | + **Enterprise** customers. Features and configuration may change. [Contact |
| 13 | + us](https://www.speakeasy.com/pricing) for access. |
| 14 | +</Callout> |
| 15 | + |
| 16 | +Speakeasy generates a fully functional command-line interface from your OpenAPI specification. The CLI is written in Go using [Cobra](https://cobra.dev/) and wraps a generated Go SDK, providing a per-operation command structure with built-in authentication, multiple output formats, and cross-platform distribution tooling. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- The [Speakeasy CLI](/docs/speakeasy-reference/cli/getting-started) |
| 21 | +- An [Enterprise plan](https://www.speakeasy.com/pricing) |
| 22 | +- An API spec in a supported format: |
| 23 | + |
| 24 | +<Table |
| 25 | + data={[ |
| 26 | + { format: "OpenAPI 3.0", supported: "✅" }, |
| 27 | + { format: "OpenAPI 3.1", supported: "✅" }, |
| 28 | + { format: "JSON Schema", supported: "✅" }, |
| 29 | + ]} |
| 30 | + columns={[ |
| 31 | + { key: "format", header: "Spec format" }, |
| 32 | + { key: "supported", header: "Supported" }, |
| 33 | + ]} |
| 34 | +/> |
| 35 | + |
| 36 | +## Quickstart |
| 37 | + |
| 38 | +### 1. Create a CLI |
| 39 | + |
| 40 | +Run the Speakeasy quickstart command and select the CLI target: |
| 41 | + |
| 42 | +```bash |
| 43 | +speakeasy quickstart --target cli |
| 44 | +``` |
| 45 | + |
| 46 | +The interactive flow prompts for three configuration values: |
| 47 | + |
| 48 | + |
| 49 | +<Table |
| 50 | + data={[ |
| 51 | + { |
| 52 | + field: "`packageName`", |
| 53 | + description: |
| 54 | + "The Go module path for your CLI (e.g., `github.com/my-company/my-api-cli`).", |
| 55 | + example: "`github.com/acme/petstore-cli`", |
| 56 | + }, |
| 57 | + { |
| 58 | + field: "`cliName`", |
| 59 | + description: |
| 60 | + "The binary name users will type to run the CLI. Also used for the config directory (`~/.config/<cliName>/`).", |
| 61 | + example: "`petstore`", |
| 62 | + }, |
| 63 | + { |
| 64 | + field: "`envVarPrefix`", |
| 65 | + description: |
| 66 | + "Prefix for environment variables. The CLI will check for variables like `<PREFIX>_API_KEY`.", |
| 67 | + example: "`PETSTORE`", |
| 68 | + }, |
| 69 | + ]} |
| 70 | + columns={[ |
| 71 | + { key: "field", header: "Field" }, |
| 72 | + { key: "description", header: "Description" }, |
| 73 | + { key: "example", header: "Example" }, |
| 74 | + ]} |
| 75 | +/> |
| 76 | + |
| 77 | +### 2. Review the generated output |
| 78 | + |
| 79 | +After generation, you'll have a complete Go project: |
| 80 | + |
| 81 | +``` |
| 82 | +├── cmd/ |
| 83 | +│ ├── <cliName>/main.go # Binary entrypoint |
| 84 | +│ └── gendocs/main.go # Cobra documentation generator |
| 85 | +├── internal/ |
| 86 | +│ ├── cli/ # Cobra command files (one per operation) |
| 87 | +│ │ ├── root.go # Root command with global flags |
| 88 | +│ │ ├── configure.go # Interactive setup wizard |
| 89 | +│ │ ├── whoami.go # Auth status checker |
| 90 | +│ │ ├── version.go # Version info |
| 91 | +│ │ └── ... # Per-operation commands |
| 92 | +│ ├── client/ # SDK client wrapper and diagnostics |
| 93 | +│ ├── config/ # Config file and OS keychain management |
| 94 | +│ ├── flagutil/ # Flag registration and request building |
| 95 | +│ ├── output/ # Output formatting (pretty, JSON, YAML, table, TOON) |
| 96 | +│ └── sdk/ # Auto-generated Go SDK |
| 97 | +├── scripts/ |
| 98 | +│ ├── install.sh # Linux/macOS install script |
| 99 | +│ └── install.ps1 # Windows install script |
| 100 | +├── .goreleaser.yaml # Cross-platform binary builds |
| 101 | +├── go.mod |
| 102 | +└── README.md |
| 103 | +``` |
| 104 | + |
| 105 | +### 3. Build and run |
| 106 | + |
| 107 | +```bash |
| 108 | +go build -o petstore ./cmd/petstore |
| 109 | +./petstore --help |
| 110 | +``` |
| 111 | + |
| 112 | +### 4. Configure authentication |
| 113 | + |
| 114 | +Run the interactive setup wizard to configure API credentials: |
| 115 | + |
| 116 | +```bash |
| 117 | +./my-cli configure |
| 118 | +``` |
| 119 | + |
| 120 | +This stores credentials securely in the OS keychain (macOS Keychain, Windows Credential Manager, or Linux Secret Service) and generates a config file at `~/.config/<cliName>/config.yaml`. |
| 121 | + |
| 122 | +## Key features |
| 123 | + |
| 124 | +### Per-operation commands |
| 125 | + |
| 126 | +Every API operation becomes a CLI command. Operations are grouped by tags, with smart stutter removal to keep command names clean: |
| 127 | + |
| 128 | +```bash |
| 129 | +# If your API has a "users" tag with a "list-users" operation: |
| 130 | +my-cli users list # "-users" suffix is removed from "list-users" |
| 131 | + |
| 132 | +# View all available commands: |
| 133 | +my-cli --help |
| 134 | +``` |
| 135 | + |
| 136 | +### Multiple output formats |
| 137 | + |
| 138 | +Control output format with the `--output-format` flag (or `-o` for short): |
| 139 | + |
| 140 | +```bash |
| 141 | +my-cli users list -o json # JSON output |
| 142 | +my-cli users list -o yaml # YAML output |
| 143 | +my-cli users list -o table # Tabular output |
| 144 | +my-cli users list -o pretty # Colored key-value output (default) |
| 145 | +my-cli users list -o toon # Token-oriented output (for LLM consumption) |
| 146 | +``` |
| 147 | + |
| 148 | +### jq filtering |
| 149 | + |
| 150 | +Filter and transform output with built-in jq support: |
| 151 | + |
| 152 | +```bash |
| 153 | +my-cli users list --jq '.[] | {name: .name, email: .email}' |
| 154 | +``` |
| 155 | + |
| 156 | +### Flexible request input |
| 157 | + |
| 158 | +Provide request data through individual flags, a JSON body, or stdin: |
| 159 | + |
| 160 | +```bash |
| 161 | +# Individual flags |
| 162 | +my-cli users create --name "Alice" --email "alice@example.com" |
| 163 | + |
| 164 | +# JSON body |
| 165 | +my-cli users create --body '{"name": "Alice", "email": "alice@example.com"}' |
| 166 | + |
| 167 | +# Stdin piping |
| 168 | +echo '{"name": "Alice"}' | my-cli users create |
| 169 | +``` |
| 170 | + |
| 171 | +### Authentication |
| 172 | + |
| 173 | +The CLI resolves credentials with a 4-tier priority: |
| 174 | + |
| 175 | +- **Flags** — `--api-key`, `--bearer-token`, etc. |
| 176 | +- **Environment variables** — `<PREFIX>_API_KEY`, `<PREFIX>_BEARER_TOKEN`, etc. |
| 177 | +- **OS keychain** — stored via `my-cli configure` |
| 178 | +- **Config file** — `~/.config/<cliName>/config.yaml` |
| 179 | + |
| 180 | +### Pagination |
| 181 | + |
| 182 | +Automatically page through paginated endpoints: |
| 183 | + |
| 184 | +```bash |
| 185 | +my-cli users list --all # Fetch all pages |
| 186 | +my-cli users list --all --max-pages 5 # Limit to 5 pages |
| 187 | +``` |
| 188 | + |
| 189 | +### Diagnostics |
| 190 | + |
| 191 | +Debug API calls without making real requests: |
| 192 | + |
| 193 | +```bash |
| 194 | +my-cli users list --dry-run # Show the request without sending it |
| 195 | +my-cli users list --debug # Show full request/response with secret redaction |
| 196 | +``` |
| 197 | + |
| 198 | +### Shell completions |
| 199 | + |
| 200 | +Generate shell completions for your CLI: |
| 201 | + |
| 202 | +```bash |
| 203 | +my-cli completion bash # Bash |
| 204 | +my-cli completion zsh # Zsh |
| 205 | +my-cli completion fish # Fish |
| 206 | +my-cli completion powershell # PowerShell |
| 207 | +``` |
| 208 | + |
| 209 | +## Next steps |
| 210 | + |
| 211 | +- [**Customize your CLI**](/docs/cli-generation/customize-cli) — Configure command naming, environment variables, and custom code |
| 212 | +- [**Distribute your CLI**](/docs/cli-generation/distribute-cli) — Set up GoReleaser, install scripts, and release automation |
| 213 | +- [**Configuration reference**](/docs/speakeasy-reference/generation/cli-config) — Full `gen.yaml` reference for the `cli` target |
0 commit comments