Skip to content

Commit 74a74a1

Browse files
committed
Sync open source content 🐝 (from 4f2d055b02e50fd662d26cd4b0141073227b31d8)
1 parent 72ed332 commit 74a74a1

File tree

5 files changed

+558
-193
lines changed

5 files changed

+558
-193
lines changed

_meta.global.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,12 @@ const meta = {
581581
title: "CLI Generation",
582582
display: "children",
583583
items: {
584-
index: {
585-
title: "Overview",
586-
},
587584
"create-cli": {
588585
title: "Generate CLI",
589586
},
587+
index: {
588+
title: "Overview",
589+
},
590590
"customize-cli": {
591591
title: "Customize CLI",
592592
},

docs/cli-generation/create-cli.mdx

Lines changed: 184 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import { Callout, Table } from "@/mdx/components";
77

88
# Generate a CLI from an OpenAPI document
99

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.
10+
<Callout type="warning" title="Alpha · Early Access">
11+
CLI generation is currently in **alpha** and available exclusively to **early
12+
access** customers. Features and configuration may change. [Contact
13+
us](/book-demo) for access.
1414
</Callout>
1515

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.
16+
Speakeasy generates a fully functional command-line interface from an OpenAPI specification. The CLI is written in Go using [Cobra](https://cobra.dev/) and wraps a generated Go SDK, providing per-operation commands, built-in authentication, multiple output formats, shell completions, and cross-platform distribution tooling.
1717

1818
## Prerequisites
1919

2020
- The [Speakeasy CLI](/docs/speakeasy-reference/cli/getting-started)
21-
- An [Enterprise plan](https://www.speakeasy.com/pricing)
21+
- Early access to CLI generation. [Contact sales](/book-demo) for access.
2222
- An API spec in a supported format:
2323

2424
<Table
@@ -45,25 +45,24 @@ speakeasy quickstart --target cli
4545

4646
The interactive flow prompts for three configuration values:
4747

48-
4948
<Table
5049
data={[
5150
{
5251
field: "`packageName`",
5352
description:
54-
"The Go module path for your CLI (e.g., `github.com/my-company/my-api-cli`).",
53+
"The Go module path for your CLI (for example, `github.com/my-company/my-api-cli`).",
5554
example: "`github.com/acme/petstore-cli`",
5655
},
5756
{
5857
field: "`cliName`",
5958
description:
60-
"The binary name users will type to run the CLI. Also used for the config directory (`~/.config/<cliName>/`).",
59+
"The binary name users will type to run the CLI. Also used for the config directory (`~/.config/<cliName>/`). Defaults to `cli` if not set.",
6160
example: "`petstore`",
6261
},
6362
{
6463
field: "`envVarPrefix`",
6564
description:
66-
"Prefix for environment variables. The CLI will check for variables like `<PREFIX>_API_KEY`.",
65+
"Prefix for environment variables. The CLI will check for variables like `<PREFIX>_API_KEY`. Defaults to `CLI` if not set.",
6766
example: "`PETSTORE`",
6867
},
6968
]}
@@ -76,23 +75,20 @@ The interactive flow prompts for three configuration values:
7675

7776
### 2. Review the generated output
7877

79-
After generation, you'll have a complete Go project:
78+
After generation, the output is a complete Go project:
8079

8180
```
8281
├── cmd/
8382
│ ├── <cliName>/main.go # Binary entrypoint
8483
│ └── gendocs/main.go # Cobra documentation generator
8584
├── 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
85+
│ ├── cli/ # Cobra command files (root, auth, configure, per-operation)
9286
│ ├── client/ # SDK client wrapper and diagnostics
9387
│ ├── config/ # Config file and OS keychain management
9488
│ ├── flagutil/ # Flag registration and request building
95-
│ ├── output/ # Output formatting (pretty, JSON, YAML, table, TOON)
89+
│ ├── output/ # Output formatting and agent-mode behavior
90+
│ ├── usage/ # Grouped help and machine-readable usage schema
91+
│ ├── explorer/ # Interactive command explorer (when enabled)
9692
│ └── sdk/ # Auto-generated Go SDK
9793
├── scripts/
9894
│ ├── install.sh # Linux/macOS install script
@@ -111,103 +107,228 @@ go build -o petstore ./cmd/petstore
111107

112108
### 4. Configure authentication
113109

114-
Run the interactive setup wizard to configure API credentials:
110+
Run the interactive setup wizard to configure API credentials and global settings:
115111

116112
```bash
117-
./my-cli configure
113+
./petstore configure
118114
```
119115

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`.
116+
When 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`.
117+
118+
If interactive auth is enabled and the API has global security, generated CLIs also expose an `auth` command group:
119+
120+
```bash
121+
./petstore auth login
122+
./petstore auth whoami
123+
./petstore auth logout
124+
```
121125

122-
## Key features
126+
## How generated CLIs behave
123127

124128
### Per-operation commands
125129

126130
Every API operation becomes a CLI command. Operations are grouped by tags, with smart stutter removal to keep command names clean:
127131

128132
```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"
133+
# If the API has a "users" tag with a "list-users" operation:
134+
petstore users list
131135

132136
# View all available commands:
133-
my-cli --help
137+
petstore --help
134138
```
135139

136-
### Multiple output formats
140+
### Request input options
141+
142+
Generated CLIs support multiple request input styles with predictable precedence:
137143

138-
Control output format with the `--output-format` flag (or `-o` for short):
144+
- Individual flags (highest priority)
145+
- `--body` JSON
146+
- stdin JSON (lowest priority)
139147

140148
```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)
149+
# Individual flags
150+
petstore users create --name "Alice" --email "alice@example.com"
151+
152+
# Whole request body JSON
153+
petstore users create --body '{"name":"Alice","email":"alice@example.com"}'
154+
155+
# Stdin piping
156+
cat payload.json | petstore users create
146157
```
147158

148-
### jq filtering
159+
Individual flags override values supplied through `--body` or stdin, which makes the CLI work well for both scripts and ad hoc usage.
149160

150-
Filter and transform output with built-in jq support:
161+
### Bytes and base64 request input
162+
163+
Request fields typed as bytes are exposed as flags that accept three forms:
151164

152165
```bash
153-
my-cli users list --jq '.[] | {name: .name, email: .email}'
166+
# Read bytes from disk
167+
petstore files upload --contents file:./avatar.png
168+
169+
# Decode base64 first
170+
petstore files upload --contents b64:SGVsbG8=
171+
172+
# Use raw string bytes directly
173+
petstore files upload --contents hello
154174
```
155175

156-
### Flexible request input
176+
Supported base64 forms include padded and unpadded standard base64 plus URL-safe base64 variants.
177+
178+
### Output formats
157179

158-
Provide request data through individual flags, a JSON body, or stdin:
180+
Control output format with the `--output-format` flag (or `-o`):
159181

160182
```bash
161-
# Individual flags
162-
my-cli users create --name "Alice" --email "alice@example.com"
183+
petstore users list -o pretty
184+
petstore users list -o json
185+
petstore users list -o yaml
186+
petstore users list -o table
187+
petstore users list -o toon
188+
```
163189

164-
# JSON body
165-
my-cli users create --body '{"name": "Alice", "email": "alice@example.com"}'
190+
- `pretty` is the default for human terminal use
191+
- `toon` is optimized for compact, line-oriented agent consumption
192+
- `--jq` applies a jq expression and produces JSON output
166193

167-
# Stdin piping
168-
echo '{"name": "Alice"}' | my-cli users create
194+
```bash
195+
petstore users list --jq '.[] | {name: .name, email: .email}'
196+
```
197+
198+
### Binary responses
199+
200+
Binary or file-like responses get binary-safe handling:
201+
202+
```bash
203+
# Save directly to disk
204+
petstore files download --id file_123 --output-file ./report.pdf
205+
206+
# Print as base64 instead of raw bytes
207+
petstore files download --id file_123 --output-b64
169208
```
170209

171-
### Authentication
210+
For 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.
172211

173-
The CLI resolves credentials with a 4-tier priority:
212+
### Response headers
174213

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`
214+
Generated CLIs can optionally include HTTP response headers in output:
179215

180-
### Pagination
216+
```bash
217+
petstore users get --id user_123 --include-headers -o json
218+
```
219+
220+
This is useful for surfacing pagination headers, rate-limit metadata, or request tracing identifiers.
221+
222+
### Pagination and streaming
181223

182-
Automatically page through paginated endpoints:
224+
When an operation is marked as paginated, the CLI adds `--all` and `--max-pages`:
183225

184226
```bash
185-
my-cli users list --all # Fetch all pages
186-
my-cli users list --all --max-pages 5 # Limit to 5 pages
227+
petstore users list --all
228+
petstore users list --all --max-pages 5
229+
```
230+
231+
Streaming endpoints are also supported:
232+
233+
- **SSE** (`text/event-stream`)
234+
- **JSONL / NDJSON** (`application/jsonl`, `application/x-ndjson`)
235+
236+
These are emitted incrementally rather than buffered.
237+
238+
### Retries, timeout, and custom headers
239+
240+
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:
241+
242+
```bash
243+
petstore users list --timeout 30s
244+
petstore users list --no-retries
245+
petstore users list --retry-max-elapsed-time 10s
246+
petstore users list --header "X-Request-ID: abc-123"
187247
```
188248

189249
### Diagnostics
190250

191-
Debug API calls without making real requests:
251+
The CLI includes built-in diagnostics that are safe for scripts and CI:
252+
253+
```bash
254+
petstore users list --dry-run
255+
petstore users list --debug
256+
```
257+
258+
- `--dry-run` shows the request that would be sent without making a network call
259+
- `--debug` logs request/response diagnostics to stderr
260+
- both paths automatically redact common secret-bearing headers and payload fields
261+
262+
### Interactive mode
263+
264+
Interactive mode is enabled by default in the generator and improves the human terminal experience:
265+
266+
- auto-prompting for unresolved required fields
267+
- an `explore` TUI for browsing and launching commands
268+
- interactive `configure` flows
269+
- interactive `auth login` flows when auth is available and interactive auth is enabled
192270

193271
```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
272+
petstore explore
273+
petstore users create
274+
petstore users create --no-interactive
196275
```
197276

277+
Interactive prompting and explorer auto-launch only happen when stdin and stdout are TTYs.
278+
279+
### Agent mode
280+
281+
Generated CLIs also support an agent-aware runtime mode optimized for AI coding assistants and non-human terminal drivers.
282+
283+
```bash
284+
petstore users list --agent-mode
285+
petstore users list --agent-mode=false
286+
```
287+
288+
Agent 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.
289+
290+
When agent mode is active:
291+
292+
- the default output format becomes `toon`
293+
- errors become structured and machine-readable
294+
- interactive surfaces are suppressed or blocked
295+
- explorer auto-launch is disabled
296+
297+
### Machine-readable usage schema
298+
299+
Generated CLIs provide both standard Cobra help and a machine-readable usage schema:
300+
301+
```bash
302+
petstore users create --help
303+
petstore users create --usage
304+
```
305+
306+
`--usage` emits a KDL representation of the selected command, including flags, defaults, help text, aliases, and configuration metadata.
307+
198308
### Shell completions
199309

200-
Generate shell completions for your CLI:
310+
Generated CLIs support Cobra’s built-in completion command:
201311

202312
```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
313+
petstore completion bash
314+
petstore completion zsh
315+
petstore completion fish
316+
petstore completion powershell
207317
```
208318

319+
Source the generated output directly in a shell session or install completion scripts as part of the CLI setup.
320+
321+
## Authentication and configuration precedence
322+
323+
The generated CLI resolves values in a predictable order:
324+
325+
- **Security credentials**: flags → environment variables → OS keychain → config file
326+
- **Global parameters**: flags → environment variables → config file
327+
328+
That means users can mix persistent configuration with per-command overrides without changing the generated code.
329+
209330
## Next steps
210331

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
332+
- [**Customize a CLI**](/docs/cli-generation/customize-cli) — Configure command naming, environment variables, interactive features, and release artifacts
333+
- [**Distribute a CLI**](/docs/cli-generation/distribute-cli) — Set up GoReleaser, install scripts, and release automation
213334
- [**Configuration reference**](/docs/speakeasy-reference/generation/cli-config) — Full `gen.yaml` reference for the `cli` target

0 commit comments

Comments
 (0)