|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | + <title>Turn an OpenAPI Spec Into a CLI People Can Actually Use</title> |
| 7 | + <meta name="description" content="OpenAPI specs are not only for SDKs and docs. They can generate human-facing Python CLIs with typed flags and nested request bodies flattened into command options."> |
| 8 | + <style> |
| 9 | + body { |
| 10 | + font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; |
| 11 | + line-height: 1.65; |
| 12 | + margin: 0; |
| 13 | + background: #f7f7f4; |
| 14 | + color: #161616; |
| 15 | + } |
| 16 | + main { |
| 17 | + max-width: 820px; |
| 18 | + margin: 0 auto; |
| 19 | + padding: 48px 20px 72px; |
| 20 | + } |
| 21 | + h1 { |
| 22 | + font-size: clamp(2rem, 5vw, 3.8rem); |
| 23 | + line-height: 1.05; |
| 24 | + letter-spacing: 0; |
| 25 | + } |
| 26 | + h2 { |
| 27 | + margin-top: 42px; |
| 28 | + } |
| 29 | + p, li { |
| 30 | + font-size: 1.05rem; |
| 31 | + } |
| 32 | + .back { |
| 33 | + margin-bottom: 32px; |
| 34 | + } |
| 35 | + a { |
| 36 | + color: #0f766e; |
| 37 | + text-underline-offset: 0.18em; |
| 38 | + } |
| 39 | + pre { |
| 40 | + background: #202124; |
| 41 | + color: #f8f8f2; |
| 42 | + overflow-x: auto; |
| 43 | + padding: 18px; |
| 44 | + border-radius: 8px; |
| 45 | + } |
| 46 | + code { |
| 47 | + font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; |
| 48 | + } |
| 49 | + @media (prefers-color-scheme: dark) { |
| 50 | + body { |
| 51 | + background: #111; |
| 52 | + color: #f3f3f0; |
| 53 | + } |
| 54 | + a { |
| 55 | + color: #5eead4; |
| 56 | + } |
| 57 | + pre { |
| 58 | + background: #050505; |
| 59 | + } |
| 60 | + } |
| 61 | + </style> |
| 62 | +</head> |
| 63 | +<body> |
| 64 | + <main> |
| 65 | + <p class="back"><a href="../index.html">Back to openapi-cli-gen</a></p> |
| 66 | + <h1>Turn an OpenAPI Spec Into a CLI People Can Actually Use</h1> |
| 67 | + <p>Most teams already have more API structure than they use.</p> |
| 68 | + <p>If you have a FastAPI app, a Swagger spec, or any OpenAPI 3.x document, the spec already knows your paths, methods, query parameters, path parameters, request bodies, auth schemes, enums, and validation rules.</p> |
| 69 | + <p>And yet, a lot of internal API workflows still end up as copied <code>curl</code> commands:</p> |
| 70 | + <pre><code>curl -X POST "$API_URL/users" \ |
| 71 | + -H "Authorization: Bearer $TOKEN" \ |
| 72 | + -H "Content-Type: application/json" \ |
| 73 | + -d '{"name":"Jane","email":"jane@example.com","address":{"city":"NYC","state":"NY"}}'</code></pre> |
| 74 | + <p>That works, but it is not a great interface. It is easy to mistype. It is hard to discover. It asks humans to write JSON even when the schema already knows the fields.</p> |
| 75 | + <p>I built <a href="https://github.com/shivaam/openapi-cli-gen">openapi-cli-gen</a> to explore a narrower idea: what if an OpenAPI spec could become a human-facing command line app?</p> |
| 76 | + |
| 77 | + <h2>The Basic Idea</h2> |
| 78 | + <p>Instead of making users paste JSON, generate commands like this:</p> |
| 79 | + <pre><code>mycli users create \ |
| 80 | + --name Jane \ |
| 81 | + --address.city NYC \ |
| 82 | + --address.state NY</code></pre> |
| 83 | + <p>The OpenAPI spec provides the structure. The generated CLI exposes that structure as command groups, options, environment variables, and output formats.</p> |
| 84 | + |
| 85 | + <h2>Why Not Just Use OpenAPI Generator?</h2> |
| 86 | + <p>OpenAPI Generator is excellent when you need SDKs, server stubs, models, or language-specific client libraries. That is not the same job.</p> |
| 87 | + <p>The distinction is:</p> |
| 88 | + <pre><code>SDK generator: produce code developers import. |
| 89 | +CLI generator: produce commands humans and scripts run.</code></pre> |
| 90 | + |
| 91 | + <h2>FastAPI Is A Natural Fit</h2> |
| 92 | + <p>FastAPI publishes an OpenAPI spec at <code>/openapi.json</code> by default. That means a running FastAPI app can become a CLI without adding a separate API description file:</p> |
| 93 | + <pre><code>openapi-cli-gen generate \ |
| 94 | + --spec http://localhost:8000/openapi.json \ |
| 95 | + --name internal-admin</code></pre> |
| 96 | + |
| 97 | + <h2>Nested Request Bodies Are The Pain Point</h2> |
| 98 | + <p>The easy part of an API CLI is path and query parameters. The annoying part is request bodies.</p> |
| 99 | + <pre><code>internal-admin users create \ |
| 100 | + --name Jane \ |
| 101 | + --email jane@example.com \ |
| 102 | + --address.city NYC \ |
| 103 | + --address.state NY</code></pre> |
| 104 | + <p>For complex cases, JSON fallback is still available:</p> |
| 105 | + <pre><code>internal-admin users create \ |
| 106 | + --address '{"city":"NYC","state":"NY"}'</code></pre> |
| 107 | + |
| 108 | + <h2>Try It</h2> |
| 109 | + <pre><code>pipx install openapi-cli-gen |
| 110 | + |
| 111 | +openapi-cli-gen inspect \ |
| 112 | + --spec https://petstore3.swagger.io/api/v3/openapi.json</code></pre> |
| 113 | + <p>Project: <a href="https://github.com/shivaam/openapi-cli-gen">https://github.com/shivaam/openapi-cli-gen</a></p> |
| 114 | + <p>I am especially interested in real OpenAPI specs that make generated CLIs awkward: unusual auth, nested bodies, arrays of objects, multipart uploads, unions, or very large schemas.</p> |
| 115 | + </main> |
| 116 | +</body> |
| 117 | +</html> |
0 commit comments