api2cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseapi2cli
api2cli
Turn any REST API into a standardized, agent-ready CLI.
Always use to run commands (no install needed).
npx api2cli将任意REST API转换为标准化的、支持Agent的CLI。
始终使用运行命令(无需安装)。
npx api2cliPrerequisites
前置条件
Before anything else, check if is installed. It is required to build the generated CLIs.
bunbash
bun --version || curl -fsSL https://bun.sh/install | bashAlways run this check first - do not skip it.
开始操作前,请先检查是否安装了。构建生成的CLI需要用到它。
bunbash
bun --version || curl -fsSL https://bun.sh/install | bash请务必先执行此项检查,不要跳过。
Workflow
工作流
Step 1: Discover the API
步骤1:探索API
Before creating anything, gather API information:
- Find the API docs URL or OpenAPI spec (use web search if needed)
- Identify: base URL, auth type (bearer/api-key/basic/custom), auth header name
- List all resources and their endpoints (GET/POST/PATCH/DELETE)
- Note any pagination, rate limiting, or special headers
开始创建前,先收集API相关信息:
- 找到API文档地址或OpenAPI规范(必要时可使用网页搜索)
- 确认:基础URL、认证类型(bearer/api-key/basic/custom)、认证头名称
- 列出所有资源及其对应的端点(GET/POST/PATCH/DELETE)
- 记录所有分页、速率限制或特殊头信息
Step 2: Create the scaffold
步骤2:创建脚手架
bash
npx api2cli create <app> \
--base-url https://api.example.com \
--auth-type bearer \
--auth-header AuthorizationSee references/commands.md for all commands and flags.
api2cliThis creates with: HTTP client (retry/backoff), auth module, multi-format output, example resource.
~/.cli/<app>-cli/bash
npx api2cli create <app> \
--base-url https://api.example.com \
--auth-type bearer \
--auth-header Authorization查看references/commands.md了解所有命令和参数。
api2cli该命令会创建目录,包含:HTTP客户端(支持重试/退避)、认证模块、多格式输出、示例资源。
~/.cli/<app>-cli/Step 3: Implement resources
步骤3:实现资源
For each API resource, create .
~/.cli/<app>-cli/src/resources/<resource>.tsSee references/resource-patterns.md for the full CRUD template and conventions.
Key rules:
- One file per resource (drafts.ts, links.ts, accounts.ts)
- CRUD commands: list, get, create, update, delete
- Every command has flag and help examples
--json - Use for HTTP
client.get/post/patch/delete() - Use for formatted response
output() - Use for errors
handleError()
Register each resource in :
~/.cli/<app>-cli/src/index.tstypescript
import { draftsResource } from "./resources/drafts.js";
program.addCommand(draftsResource);针对每个API资源,创建文件。
~/.cli/<app>-cli/src/resources/<resource>.ts查看references/resource-patterns.md获取完整的CRUD模板和开发规范。
核心规则:
- 每个资源对应一个文件(如drafts.ts、links.ts、accounts.ts)
- CRUD命令:list、get、create、update、delete
- 每个命令都支持参数和帮助示例
--json - 使用发起HTTP请求
client.get/post/patch/delete() - 使用处理格式化响应
output() - 使用处理错误
handleError()
在中注册每个资源:
~/.cli/<app>-cli/src/index.tstypescript
import { draftsResource } from "./resources/drafts.js";
program.addCommand(draftsResource);Step 4: Build, link, and test
步骤4:构建、链接与测试
bash
npx api2cli bundle <app>
npx api2cli link <app>
export PATH="$HOME/.local/bin:$PATH"
<app>-cli --help
<app>-cli <resource> list --jsonapi2cli linkexport PATH="$HOME/.local/bin:$PATH"<app>-clisource ~/.zshrcbash
npx api2cli bundle <app>
npx api2cli link <app>
export PATH="$HOME/.local/bin:$PATH"
<app>-cli --help
<app>-cli <resource> list --jsonapi2cli linkexport PATH="$HOME/.local/bin:$PATH"<app>-clisource ~/.zshrcStep 5: Update the AgentSkill and README
步骤5:更新AgentSkill和README
The scaffold already includes and with placeholder sections.
skills/<app>-cli/SKILL.mdREADME.mdAfter implementing resources, update these files:
- Edit - fill in
~/.cli/<app>-cli/skills/<app>-cli/SKILL.mdand{{RESOURCES_LIST}}with actual resource names and commands{{RESOURCES_HELP}} - Edit - fill in
~/.cli/<app>-cli/README.mdand{{RESOURCES_HELP}}{{GITHUB_REPO}}
See references/skill-generation.md for the template and rules.
The skill lives inside the repo so when pushed to GitHub, anyone can install it:
bash
npx skills add <github-user>/<app>-cli脚手架默认已包含带占位符的和文件。
skills/<app>-cli/SKILL.mdREADME.md实现资源后,请更新这些文件:
- 编辑- 用实际的资源名称和命令填充
~/.cli/<app>-cli/skills/<app>-cli/SKILL.md和{{RESOURCES_LIST}}占位符{{RESOURCES_HELP}} - 编辑- 填充
~/.cli/<app>-cli/README.md和{{RESOURCES_HELP}}占位符{{GITHUB_REPO}}
查看references/skill-generation.md获取模板和规则说明。
Skill文件存放在仓库中,推送到GitHub后,任何人都可以安装使用:
bash
npx skills add <github-user>/<app>-cliGenerated CLI Conventions
生成CLI的规范
All generated CLIs follow these exact patterns:
<app>-cli <resource> <action> [flags]
<app>-cli auth set|show|remove|testAlways use when calling CLI commands programmatically. The table output is for humans only - it wraps and is unreadable by AI. returns structured data:
--json--jsonjson
{ "ok": true, "data": [...], "meta": { "total": 42 } }Other flags: , , ,
--format <text|json|csv|yaml>--verbose--no-color--no-headerExit codes: 0 = success, 1 = API error, 2 = usage error
Tokens: (chmod 600)
~/.config/tokens/<app>-cli.txt所有生成的CLI都严格遵循以下模式:
<app>-cli <resource> <action> [flags]
<app>-cli auth set|show|remove|test以编程方式调用CLI命令时请始终使用参数。表格输出仅面向人类阅读,会自动换行,AI无法解析。会返回结构化数据:
--json--jsonjson
{ "ok": true, "data": [...], "meta": { "total": 42 } }其他参数:、、、
--format <text|json|csv|yaml>--verbose--no-color--no-header退出码:0 = 成功,1 = API错误,2 = 使用错误
Token存储路径:(权限为chmod 600)
~/.config/tokens/<app>-cli.txt